ESP32 - DC Motor

by DIYables in Circuits > Microcontrollers

14 Views, 0 Favorites, 0 Comments

ESP32 - DC Motor

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://esp32io.com/tutorials/esp32-dc-motor

This build shows how to control a DC motor's speed and direction from an ESP32 using an L298N motor driver module. The ESP32 can't drive the motor directly because of the voltage and current involved, so the L298N sits between them, protecting the ESP32 while amplifying the PWM signal and switching polarity to reverse the motor.

Supplies

Wiring

The DC motor has two wires, negative (black) and positive (red), which connect to the L298N's OUT1 and OUT2 terminals for motor A. The ESP32 connects to the L298N's IN1, IN2 and ENA pins: IN1/IN2 set direction, ENA carries the PWM speed signal. Power the L298N from the 5V power adapter rather than the ESP32. Before wiring anything, remove all three jumpers on the L298N module.

How It Works

The L298N takes three jobs off the ESP32: it isolates the ESP32 from the motor's higher voltage, it switches the polarity across the motor to set its direction based on the IN1/IN2 signals, and it amplifies the ESP32's PWM signal in current and voltage so it can actually turn the motor. Writing a PWM value to ENA with analogWrite() sets speed from 0 (stopped) to 255 (full speed). Setting IN1 and IN2 to opposite states spins the motor clockwise or anti-clockwise; setting them to the same level (both LOW or both HIGH) stops it, as does dropping the speed to 0.

Code

The sketch runs through the motor's full behavior: it ramps the speed up, reverses the motor's direction by flipping IN1 and IN2, ramps the speed back down, then stops the motor. Speed control uses analogWrite() on the ENA pin, direction control uses digitalWrite() on IN1 and IN2.

esp32-dc-motor.ino - the sketch from the tutorial, unchanged.

/*
* 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
}

Full Tutorial

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