Skip to content

DC Motor Control using L298N Motor Driver & ESP32

3 min read 13,335 views
DC Motor Control using L298N Motor Driver & ESP32

Overview

In the realm of robotics, automation, and electronic projects, controlling DC motors efficiently and effectively is a fundamental requirement. The combination of an L298N motor driver and an ESP32 microcontroller presents a powerful solution for precise DC motor control in various applications

In today's tutorial, we are going to control "DC Motor" using an L298N Motor Driver & ESP32 Microcontroller.

PCBWay offers high-quality PCB prototyping and assembly at an affordable price, starting at just $5 for 5 PCBs. With fast turnaround, great customer support, and easy online ordering, it's a top choice for hobbyists and professionals alike.

L298N Motor Driver

The L298N motor driver is a dual H-bridge motor driver IC that allows bidirectional control of DC motors. It can drive two DC motors or a single stepper motor and provides a convenient interface for controlling motor speed and direction using a microcontroller.

https://youtu.be/SEfYIT7NG7E

Hardware Components

You'll need the following hardware components to get started:

ComponentsValue / ModelQty
ESP32DOIT DEVKIT V1 Board1
Motor DriverL298N1
Power source4x 1.5 AA batteries or Bench power supply1
Jumper Wires-1
DC Motor9V, 12V1

Pinouts

L298N-Motor-Driver-Pinout-1

ESP32 Pinout

ESP32-Pinout

Steps-by-Step Guide

(1) Setting up Arduino IDE

Download Arduino IDE Software from its official site. Here is a step-by-step guide on "How to install Arduino IDE".

(2) ESP32 in Arduino IDE

There’s an add-on that allows you to program the ESP32 using the Arduino IDE. Here is a step-by-step guide on "How to Install ESP32 on Arduino IDE".

(3) Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

ESP32L298NExternal Power
-+12v+12v
GNDGNDGND
GPIO27Input 1
GPIO26Input 2
GPIO14Enable

(5) Uploading Code

Now copy the following code and upload it to Arduino IDE Software.

// Motor A
int motor1Pin1 = 27; 
int motor1Pin2 = 26; 
int enable1Pin = 14; 

// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

void setup() {
  // sets the pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  
  // configure LED PWM functionalitites
  ledcSetup(pwmChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(enable1Pin, pwmChannel);

  Serial.begin(115200);

  // testing
  Serial.print("Testing DC Motor...");
}

void loop() {
  // Move the DC motor forward at maximum speed
  Serial.println("Moving Forward");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor backwards at maximum speed
  Serial.println("Moving Backwards");
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor forward with increasing speed
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  while (dutyCycle <= 255){
    ledcWrite(pwmChannel, dutyCycle);   
    Serial.print("Forward with duty cycle: ");
    Serial.println(dutyCycle);
    dutyCycle = dutyCycle + 5;
    delay(500);
  }
  dutyCycle = 200;
}

How code works

This code sets up and controls a DC motor (Motor A) using an ESP32 microcontroller. It defines the pins for motor control (motor1Pin1, motor1Pin2, enable1Pin) and sets PWM properties for speed control. The setup function initializes the pins and LED PWM functionalities, while the loop function sequentially moves the motor forward at maximum speed, stops it, moves it backward, stops it again, and then gradually increases the speed in the forward direction using PWM. The code also includes serial communication for testing and monitoring the motor's behavior.

Applications

  • Robotics projects requiring precise DC motor control.
  • Automated systems such as conveyor belts and robotic arms.
  • Home automation projects for controlling window blinds or door locks.
  • CNC machines for controlling spindle motors or positioning systems.
  • DIY projects like RC cars, drones, or robotic vehicles.

Conclusion

The code snippet demonstrates how to control a DC motor using an ESP32 microcontroller and PWM techniques. With this setup, users can achieve bidirectional motor control, adjust motor speed dynamically, and integrate it into various electronic projects and automation systems with ease.

Share this project

Discussion

Be the first to comment

More projects you might like

More in ESP32 Projects & Tutorials