Skip to content

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 LabelGPIO PinConnected Device
IN1GPIO15L298N/L9110 Input 1 (forward)
IN2GPIO2L298N/L9110 Input 2 (reverse)
ENAGPIO0L298N Enable (PWM speed control)

L298N module requires external power (6-12V), logic power at 3.3V/5V.

JSON Configuration Example ​

json
{
  "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

  1. Click Peripheral Execution in the left menu
  2. Click New Rule button
  3. Fill in basic configuration:
    • Rule Name: Motor Forward
    • Report Data: ✅ Enable
    • Enabled: ✅ Enable

Step 2: Configure Trigger

  1. Click Add Trigger button

  2. Fill in trigger configuration:

    FieldValueDescription
    Trigger TypeSelect Platform TriggerReceive cloud platform commands
    OperatorSelect Match PatternMatch command
    Comparison Value{"cmd":"forward"}Forward command

Step 3: Configure Actions (3 actions needed)

Action 1: Set forward direction

  1. Click Add Action button
  2. Fill in:
    • Action Type: Select HIGH Level
    • Target Peripheral: Select motor_in1

Action 2: Set reverse direction LOW

  1. Click Add Action button again
  2. Fill in:
    • Action Type: Select LOW Level
    • Target Peripheral: Select motor_in2

Action 3: Set PWM speed

  1. Click Add Action button a third time

  2. Fill in:

    • Action Type: Select Set PWM
    • Target Peripheral: Select motor_speed
    • Action Params: 200 (duty cycle, 0-255)
  3. Click Save button

Execution Flow:

Receive forward command
  ↓
IN1 = HIGH (forward direction)
  ↓
IN2 = LOW
  ↓
ENA = 200/255 (78% speed)
  ↓
Motor runs forward

Scenario 2: Motor Stop (Platform Trigger) ​

Function: Stop motor via cloud platform command

Web Interface Configuration Steps ​

Step 1: Create Rule

  1. Click New Rule
  2. Fill in basic configuration:
    • Rule Name: Motor Stop
    • Report Data: ✅ Enable
    • Enabled: ✅ Enable

Step 2: Configure Trigger

  1. Click Add Trigger button
  2. Fill in:
    • Trigger Type: Select Platform Trigger
    • Operator: Select Match Pattern
    • Comparison Value: {"cmd":"stop"}

Step 3: Configure Actions (3 actions needed)

  1. Action 1: LOW Level → motor_in1

  2. Action 2: LOW Level → motor_in2

  3. Action 3: Set PWM → motor_speed, param 0

  4. Click Save button

Motor Control Truth Table ​

IN1IN2ENA(PWM)State
HIGHLOW0-255Forward (speed by PWM)
LOWHIGH0-255Reverse (speed by PWM)
LOWLOW0Coast stop
HIGHHIGH0-255Brake (quick stop)

Notes ​

  1. External Power: Motor needs independent power supply, do not power from ESP32's 3.3V
  2. PWM Speed Control: Duty cycle 0~255 (8bit), higher value = faster speed
  3. Brake Mode: IN1=IN2=1 for braking, IN1=IN2=0 for coast stop
  4. Back EMF: Motor's reverse voltage at power-off may damage driver chip, ensure module has protection diodes
  5. GPIO0: When used as ENA, pay attention to boot timing to avoid affecting ESP32 startup

FastBee | fastbee.cn