Example 7: DC Motor Control
Example 7: DC Motor Control
Experiment Overview
Control the start/stop and direction of a DC motor via GPIO output combined with a motor driver module (e.g., L298N, L9110). ESP32 GPIO cannot directly drive a motor, a driver chip is needed to amplify the current.
Hardware Wiring
| Board Label | GPIO Pin | Connected Device |
|---|---|---|
| IN1 | GPIO15 | L298N/L9110 Input 1 (forward) |
| IN2 | GPIO2 | L298N/L9110 Input 2 (reverse) |
| ENA | GPIO0 | L298N Enable (PWM speed control) |
L298N module requires external power (6-12V), logic power at 3.3V/5V.
JSON Configuration Example
{
"peripherals": [
{
"id": "motor_in1",
"name": "Motor-Forward",
"type": 12,
"enabled": false,
"pins": [15],
"params": { "initialState": 0 }
},
{
"id": "motor_in2",
"name": "Motor-Reverse",
"type": 12,
"enabled": false,
"pins": [2],
"params": { "initialState": 0 }
},
{
"id": "motor_speed",
"name": "Motor-Speed",
"type": 17,
"enabled": false,
"pins": [0],
"params": {
"pwmChannel": 2,
"pwmFrequency": 1000,
"pwmResolution": 8,
"defaultDuty": 0
}
}
]
}Peripheral Execution Linkage
Scenario 1: Motor Forward (Platform Trigger)
Function: Control motor forward via cloud platform command, speed 200/255 (~78%)
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:
Motor Forward - 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 Match Pattern Match command Comparison Value {"cmd":"forward"}Forward command
Step 3: Configure Actions (3 actions needed)
Action 1: Set forward direction
- Click Add Action button
- Fill in:
- Action Type: Select HIGH Level
- Target Peripheral: Select
motor_in1
Action 2: Set reverse direction LOW
- Click Add Action button again
- Fill in:
- Action Type: Select LOW Level
- Target Peripheral: Select
motor_in2
Action 3: Set PWM speed
Click Add Action button a third time
Fill in:
- Action Type: Select Set PWM
- Target Peripheral: Select
motor_speed - Action Params:
200(duty cycle, 0-255)
Click Save button
Execution Flow:
Receive forward command
↓
IN1 = HIGH (forward direction)
↓
IN2 = LOW
↓
ENA = 200/255 (78% speed)
↓
Motor runs forwardScenario 2: Motor Stop (Platform Trigger)
Function: Stop motor via cloud platform command
Web Interface Configuration Steps
Step 1: Create Rule
- Click New Rule
- Fill in basic configuration:
- Rule Name:
Motor Stop - Report Data: ✅ Enable
- Enabled: ✅ Enable
- Rule Name:
Step 2: Configure Trigger
- Click Add Trigger button
- Fill in:
- Trigger Type: Select Platform Trigger
- Operator: Select Match Pattern
- Comparison Value:
{"cmd":"stop"}
Step 3: Configure Actions (3 actions needed)
Action 1: LOW Level →
motor_in1Action 2: LOW Level →
motor_in2Action 3: Set PWM →
motor_speed, param0Click Save button
Motor Control Truth Table
| IN1 | IN2 | ENA(PWM) | State |
|---|---|---|---|
| HIGH | LOW | 0-255 | Forward (speed by PWM) |
| LOW | HIGH | 0-255 | Reverse (speed by PWM) |
| LOW | LOW | 0 | Coast stop |
| HIGH | HIGH | 0-255 | Brake (quick stop) |
Notes
- External Power: Motor needs independent power supply, do not power from ESP32's 3.3V
- PWM Speed Control: Duty cycle 0~255 (8bit), higher value = faster speed
- Brake Mode: IN1=IN2=1 for braking, IN1=IN2=0 for coast stop
- Back EMF: Motor's reverse voltage at power-off may damage driver chip, ensure module has protection diodes
- GPIO0: When used as ENA, pay attention to boot timing to avoid affecting ESP32 startup
