Example 5: Relay Control
Example 5: Relay Control
Experiment Overview
Control the on/off state of a relay via GPIO digital output, enabling switching of high-power devices (lights, motors, solenoid valves, etc.). Relays are the most commonly used actuators in IoT projects.
Hardware Wiring
| Board Label | GPIO Pin | Connected Device |
|---|---|---|
| RELAY | GPIO25 | Relay module (energize on HIGH) |
Relay modules typically have optocoupler isolation, HIGH level signal energizes the relay (NO contact closes).
JSON Configuration Example
{
"peripherals": [
{
"id": "relay_01",
"name": "Relay",
"type": 12,
"enabled": false,
"pins": [25],
"params": {
"initialState": 0
}
}
]
}Peripheral Execution Linkage
Scenario 1: Remote On/Off Control (Platform Trigger)
Function: Control relay on/off via cloud platform commands
Web Interface Configuration Steps
Step 1: Create Rule
- Click left menu Peripheral Configuration → Switch to Peripheral Execution Management tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Remote Relay Control - Report Data: ✅ Enable
- Enabled: ✅ Enable
- Rule Name:
Step 2: Configure Trigger
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Platform Trigger Receive cloud platform commands Operator Select Set Mode Received data used as action parameters
Step 3: Configure Action
Click Add Action button
Fill in:
- Action Type: Select HIGH Level or LOW Level
- Target Peripheral: Select
relay_01
Click Save button
Test Method:
Send via cloud platform:
1orHIGH→ Relay energizes0orLOW→ Relay de-energizes
Scenario 2: Timed Switch (Timer Trigger)
Function: Automatically start device at 8 AM every day
Web Interface Configuration Steps
Step 1: Create Rule
- Click New Rule
- Fill in basic configuration:
- Rule Name:
Relay Timed Control - Report Data: ✅ Enable
- Enabled: ✅ Enable
- Rule Name:
Step 2: Configure Trigger
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Timer Trigger Execute by time Timer Mode Select Daily Timer Fixed time every day Time Point 08:008:00 AM
Step 3: Configure Action
Click Add Action button
Fill in:
- Action Type: Select HIGH Level
- Target Peripheral: Select
relay_01
Click Save button
💡 Tip: You can create another rule to turn off the relay at 6 PM (18:00)
Notes
- Safety Voltage: When controlling mains voltage (220V) with relays, always prioritize safety and ensure proper isolation
- Back EMF: Relay coils generate reverse voltage when de-energized; modules typically have built-in flyback diodes
- Initial State: Recommend
initialState: 0, default off at power-on to prevent accidental energizing - Current Capability: ESP32 GPIO outputs 3.3V/12mA, requires transistor or module to drive relay
- Contact Rating: Standard relay contacts rated at 10A/250VAC, use larger relays for higher loads
