How to Control the Speed of a DC Motor with an ESP32 and L298N Module

How to Control the Speed of a DC Motor with an ESP32 and L298N Module

Controlling DC motors can be a straightforward task with the right components and understanding. In this tutorial, we will explore how to control two DC motors using the ESP32 microcontroller and the L298N motor driver module. By the end of this guide, you will be able to adjust the speed and direction of your motors based on PWM signals from the ESP32, allowing for versatile applications in robotics and automation.

ESP32_L298N_DC_main

The video accompanying this tutorial provides a visual demonstration of the entire process, including wiring and coding (in video at 00:00).

Hardware Explained

L298N Motor Controller Module
L298N Motor Controller MOdule

To achieve motor control, we will be using the ESP32 and the L298N motor driver module. The ESP32 is a powerful microcontroller with Wi-Fi and Bluetooth capabilities, making it ideal for IoT projects. The L298N is an H-bridge motor driver that allows us to control the direction and speed of the motors by using PWM signals. The L298N module features two H-bridge circuits, which means we can control two motors independently. Each H-bridge allows for control over the direction of the motor (clockwise or counterclockwise) and the speed by varying the PWM signal. By connecting the enable and input pins of the L298N to the ESP32, we can manipulate the motors effectively.

Datasheet Details

Manufacturer STMicroelectronics
Part number L298N
Logic/IO voltage 5 V
Supply voltage 5–46 V (VS)
Output current (per channel) 2 A max/channel (abs. max)
Peak current (per channel) 2.5 A
PWM frequency guidance 1 kHz - 15 kHz
Input logic thresholds 2.5 V (high), 1.5 V (low)
Voltage drop / RDS(on) / saturation 1.8 V at 2 A
Thermal limits 150 °C
Package 15-pin Multiwatt
Notes / variants Dual H-bridge motor driver

 

  • Ensure the motor's voltage rating matches the L298N's supply voltage.
  • Use adequate heat sinking for continuous operation at high currents.
  • Connect grounds of all components to avoid floating references.
  • Use a PWM frequency suitable for the application (1-15 kHz).
  • Be cautious of maximum current ratings to prevent overheating.
  • Test motor direction before finalizing wiring to avoid damage.
  • Consider decoupling capacitors on the power supply for stability.

Wiring Instructions

ESP32_L298N_wiring

To wire the L298N to the ESP32, start by connecting the two DC motors to the output terminals of the L298N. Connect Motor 1 to the terminals labeled as OUT1 and OUT2, and Motor 2 to OUT3 and OUT4. The polarity of the motors does not matter as the L298N will handle the direction. Next, connect your external power supply to the VMS and GND terminals on the L298N. Ensure that the voltage is appropriate for your motors, typically between 5V and 46V. The 5V terminal on the L298N can be used to power the ESP32 if needed, but ensure the current requirements are met. For the control pins, connect ENA to GPIO 19, IN1 to GPIO 18, and IN2 to GPIO 5 for Motor 1. For Motor 2, connect IN3 to GPIO 17, IN4 to GPIO 16, and ENB to GPIO 4. Finally, connect the grounds of the ESP32 and L298N together to ensure a common reference.

Code Examples & Walkthrough

The following code snippets illustrate how to control the motors using the L298N library designed for the ESP32. The library simplifies the process of sending commands to the motors. First, the necessary library is included, and motor parameters are defined:


#include 
#define ENA 19
#define IN1 18
#define IN2 5
#define IN3 17
#define IN4 16
#define ENB 4
Robojax_L298N_DC_motor robot(IN1, IN2, ENA, CHA, IN3, IN4, ENB, CHB);

In this snippet, we define the control pins for both motors. The library instance robot is created, which will be used to control the motors throughout the program. Next, we set up the serial communication and initialize the robot instance in the setup() function:


void setup() {
  Serial.begin(115200);
  robot.begin();
}

This initializes the serial communication for debugging and prepares the motor control library for operation. Finally, the main control logic is placed in the loop() function, where we can rotate the motors and adjust their speed:


void loop() {
  robot.rotate(motor1, 80, CW); // run motor1 at 80% speed in CW direction
  delay(3000); // wait for 3 seconds
  robot.brake(1); // brake motor1
  delay(2000); // wait for 2 seconds
}

This excerpt demonstrates how to rotate Motor 1 at 80% speed in a clockwise direction for three seconds before braking. The full code includes additional logic for Motor 2 and varying speeds, which can be seen in the complete program loaded below the article.

Demonstration / What to Expect

When executed, the motors should respond to the PWM signals sent from the ESP32. You will see Motor 1 rotating clockwise at 80% speed, followed by a brake action. The program also includes logic to manage speed variations gradually, which can be tested by observing the motor's acceleration from 0% to 100% (in video at 05:00). Common pitfalls include not connecting grounds properly, which can lead to erratic behavior, and exceeding the current ratings of the L298N, causing it to overheat. Make sure to monitor the motor's behavior and adjust parameters as necessary.

Video Timestamps

  • 00:46 Introduction
  • 04:07 Wiring ESP32 with L298N
  • 06:10 Preparing Arduino IDE to work with ESP32 boards
  • 08:15 Arduino Code explained
  • 15:00 Demonstration of single motor control
  • 16:00 Demonstration of controlling 2 DC motors
  • 17:00 Demonstration of Powering of ESP32 from L298N and external power

Изображения

L298N Motor Controller Module
L298N Motor Controller MOdule
ESP32_L298N_DC_main
ESP32_L298N_DC_main
ESP32_L298N_wiring
ESP32_L298N_wiring
278-Library Example for L298N Module to control DC motors
Язык: C++
Скопировано!

Вещи, которые могут вам понадобиться

Файлы📁

Библиотеки Arduino (zip)

Файл Fritzing