ESP32 with DC Motor and L298N Motor Driver: Control Speed and Direction Easily

The ESP32 microcontroller is a versatile and powerful tool for DIY electronics projects, and when paired with the L298N motor driver, it becomes a robust solution for controlling DC motors. In this guide, we’ll walk you through the process of controlling the speed and direction of a DC motor using the ESP32 and L298N motor driver. Whether you’re building a robot, a conveyor belt, or any other project that requires motor control, this tutorial will provide you with the essential knowledge to get started.

Why Use the L298N Motor Driver?

The L298N motor driver is a dual H-bridge motor driver that allows you to control the speed and direction of two DC motors simultaneously. It’s widely used in robotics and automation projects due to its ease of use and compatibility with microcontrollers like the ESP32. The L298N can handle motors with voltages ranging from 5V to 35V and currents up to 2A, making it suitable for a variety of motors.

Components Needed

To follow along with this tutorial, you’ll need the following components:

  • ESP32 Development Board
  • L298N Motor Driver Module
  • DC Motor
  • Power Supply (for the motor)
  • Jumper Wires
  • Breadboard (optional)

Wiring the Components

Proper wiring is crucial for the successful operation of your motor control system. Follow these steps to connect your ESP32, L298N motor driver, and DC motor:

  1. Connect the ESP32 to the L298N:

    • IN1 to GPIO 14
    • IN2 to GPIO 27
    • IN3 to GPIO 26 (if using a second motor)
    • IN4 to GPIO 25 (if using a second motor)
    • ENA to GPIO 12 (for PWM speed control)
    • ENB to GPIO 13 (for PWM speed control of the second motor)
  2. Connect the DC Motor to the L298N:

    • Connect one motor to OUT1 and OUT2
    • If using a second motor, connect it to OUT3 and OUT4
  3. Power the L298N:

    • Connect a power supply (matching your motor’s voltage) to the 12V input on the L298N
    • Connect the ground (GND) to the ESP32 GND
  4. Power the ESP32:

    • Ensure your ESP32 is powered via USB or an appropriate power source.

Coding the ESP32 for Motor Control

Once the wiring is complete, it’s time to write the code that will control the motor’s speed and direction. You can use the Arduino IDE to program the ESP32. Here’s a basic example:

 
// Define motor control pins #define ENA 12 #define IN1 14 #define IN2 27 void setup() { // Set motor control pins as outputs pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); } void loop() { // Rotate motor in one direction digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, 200); // Control speed with PWM (0-255) delay(2000); // Run motor for 2 seconds // Rotate motor in the opposite direction digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, 150); // Control speed with PWM delay(2000); // Run motor for 2 seconds // Stop the motor digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); analogWrite(ENA, 0); delay(2000); // Motor stopped for 2 seconds }

Understanding the Code

  • ENA Pin

    : This pin controls the speed of the motor using PWM. The analogWrite() function allows you to adjust the speed from 0 (stopped) to 255 (full speed).
  • IN1 and IN2 Pins

    : These pins control the direction of the motor. Setting IN1 HIGH and IN2 LOW rotates the motor in one direction, while the reverse settings rotate it in the opposite direction.

Testing and Troubleshooting

After uploading the code to your ESP32, your DC motor should start rotating according to the defined directions and speeds. If the motor doesn’t behave as expected:

  • Check the wiring:

     Ensure all connections are secure and correctly aligned.
  • Adjust the PWM values:

     Experiment with different values in the analogWrite() function to achieve the desired speed.
  • Verify the power supply:

     Ensure the motor receives adequate power from the L298N.

Controlling the speed and direction of a DC motor using the ESP32 and L298N motor driver opens up endless possibilities for your projects. With the knowledge from this guide, you can now incorporate precise motor control into your robotics, automation, or other creative electronics endeavors.

 

Skip to content