Back to Tutorial
esp32 with l298n motor driver

Controlling DC Motors with ESP32 and L298N Motor Driver: A Beginner’s Guide

Introduction

The L298N is a popular H-bridge motor driver IC that can be used to control the direction and speed of DC motors. In this blog post, we’ll explore how to use an ESP32 microcontroller to drive a DC motor through an L298N.

What You’ll Need

Understanding the L298N

The L298N has three inputs for each motor:

  • Input 1 (IN1): Controls the direction of the motor (forward or reverse).
  • Input 2 (IN2): Controls the direction of the motor (forward or reverse).
  • Enable (EN): Enables or disables the motor.

To drive a motor forward, set IN1 high and IN2 low. To drive the motor backward, set IN1 low and IN2 high. To enable the motor, set EN high.

Connecting the Components

  1. Connect the 5V power supply to the VCC pin of the L298N.
  2. Connect the ground of the power supply to the GND pin of the L298N.
  3. Connect the positive terminal of the DC motor to the OUT1 pin of the L298N.
  4. Connect the negative terminal of the DC motor to the OUT2 pin of the L298N.
  5. Connect the IN1 and IN2 pins of the L298N to digital pins on the ESP32 (e.g., GPIO13 and GPIO14).
  6. Connect the EN pin of the L298N to a digital pin on the ESP32 (e.g., GPIO12).

ESP32 Code

Here’s a basic example of ESP32 code to control a DC motor using an L298N:

C++

#include<Arduino.h>

#define IN1 13
#define IN2 14
#define EN  12

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(EN, OUTPUT);
}

void loop() {
  // Drive motor forward
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(EN, HIGH);
  delay(2000);

  // Drive motor backward
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(EN, HIGH);
  delay(2000);

  // Stop motor
  digitalWrite(EN, LOW);
}

In this code:

  • #include <Arduino.h> includes the Arduino core library.
  • #define IN1 13, #define IN2 14, and #define EN 12 define the digital pins connected to the L298N.
  • digitalWrite(pin, value) function sets the specified pin to the given value (HIGH or LOW).

Uploading the Code

  1. Connect your ESP32 to your computer using a USB cable.
  2. Open the Arduino IDE and select the correct board and port.
  3. Upload the code to the ESP32.

Testing the Motor

Once the code is uploaded, you should see the DC motor rotate forward and backward. You can modify the delay times and the direction of the motor in the code to control its movement as needed.

Additional Considerations

  • Current Limiting: If you’re using a high-current motor, you may need to add resistors to the IN1 and IN2 pins of the L298N to limit the current flowing through them.
  • PWM Control: To control the speed of the motor, you can use pulse-width modulation (PWM) to vary the duty cycle of the signal to the EN pin.
  • Heat Sink: The L298N can generate heat when driving high-current motors. It’s recommended to use a heat sink to dissipate the heat.

By following these steps, you can successfully control a DC motor using an ESP32 and L298N. This combination can be used in various projects, such as robotics, automation, and hobby electronics.

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial