DEV Community

Cover image for ESP32 - DC Motor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at esp32io.com

ESP32 - DC Motor

A DC motor draws far more current than an ESP32 pin can safely provide, so you can't just wire it straight to a GPIO. This tutorial wires a DC motor through an L298N driver module and controls both its speed and direction from ESP32 code using PWM, the same approach used in robots and small cars.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 - DC Motor

What you need

Wiring

ESP32 - DC Motor wiring diagram

Before wiring anything, remove the three jumpers on the L298N module. The motor's two leads (black negative, red positive) connect to the driver's OUT1 and OUT2 terminals, which belong to motor A. From the ESP32 side, a digital output pin connects to the driver's ENA pin to carry a PWM signal for speed, while two more digital pins connect to IN1 and IN2 to set direction. The driver sits between the ESP32 and the motor's own power supply, so the motor's higher voltage never touches the ESP32 directly.

How it works

A DC motor's speed and direction both depend on how power reaches its two wires. Reversing polarity reverses direction, and a PWM signal with a bigger duty cycle spins the motor faster. The L298N driver handles this: it protects the ESP32 from the motor's voltage, flips polarity on OUT1/OUT2 based on the IN1/IN2 signals to set direction, and amplifies the ESP32's PWM signal in current and voltage to actually drive the motor at the requested speed. Motor A is controlled through IN1, IN2 and ENA; a second motor, B, uses IN3, IN4 and ENB on the same module.

Code

The sketch sets pin modes for the motor's enable and direction pins, then uses analogWrite() on ENA with a value from 0 to 255 to control speed, where 255 is full speed and 0 stops the motor. digitalWrite() on IN1 and IN2 sets the motor's direction, with one pattern spinning it clockwise and the opposite pattern reversing it. The example ramps the speed up, flips direction, ramps speed down again, then stops the motor either by setting speed to 0 or by setting IN1 and IN2 to the same logic level.

esp32-dc-motor.ino

/*
 * This ESP32 code is created by esp32io.com
 *
 * This ESP32 code is released in the public domain
 *
 * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-dc-motor
 */

#define PIN_IN1  19 // ESP32 pin GPIO19 connected to the IN1 pin L298N
#define PIN_IN2  18 // ESP32 pin GPIO18 connected to the IN2 pin L298N
#define PIN_ENA  17 // ESP32 pin GPIO17 connected to the EN1 pin L298N

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pins as outputs.
  pinMode(PIN_IN1, OUTPUT);
  pinMode(PIN_IN2, OUTPUT);
  pinMode(PIN_ENA, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(PIN_IN1, HIGH); // control the motor's direction in clockwise
  digitalWrite(PIN_IN2, LOW);  // control the motor's direction in clockwise

  for (int speed = 0; speed <= 255; speed++) {
    analogWrite(PIN_ENA, speed); // speed up
    delay(10);
  }

  delay(2000); // rotate at maximum speed 2 seconds in clockwise direction

  // change direction
  digitalWrite(PIN_IN1, LOW);   // control the motor's direction in anti-clockwise
  digitalWrite(PIN_IN2, HIGH);  // control the motor's direction in anti-clockwise

  delay(2000); // rotate at maximum speed for 2 seconds in anti-clockwise direction

  for (int speed = 255; speed >= 0; speed--) {
    analogWrite(PIN_ENA, speed); // speed down
    delay(10);
  }

  delay(2000); // stop motor 2 second
}
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://esp32io.com/tutorials/esp32-dc-motor

Top comments (0)