Search Code

How to Control DC Motors with an ESP32 and L298N Module over Wi-Fi

How to Control DC Motors with an ESP32 and L298N Module over Wi-Fi

Control Two DC Motors Over Wi-Fi with ESP32 and L298N

This tutorial demonstrates how to build a powerful and responsive system to control two DC motors using an ESP32 microcontroller and an L298N motor driver module. You will be able to start, stop, change the speed, and reverse the direction of each motor independently through a web page hosted by the ESP32. This web interface can be accessed from any device with a web browser, like a phone or computer, as long as it's connected to the same Wi-Fi network.


Components Overview

  • ESP32: A powerful microcontroller with built-in Wi-Fi, making it perfect for web-based control projects.
  • L298N Motor Driver: A versatile and affordable dual H-bridge motor driver capable of controlling two DC motors. It can handle a wide voltage range, up to 35 volts, making it suitable for most hobbyist motors used in smart cars and robotics.
  • Robojax Library: To simplify the code, we use a custom Robojax library designed for the L298N driver that works with the ESP32.

Wiring Guide

The wiring for this project connects the ESP32 to the L298N driver, which in turn connects to the motors and a power source. The full wiring is explained in the video starting at 05:05.

Power Connections

  • External Power: Connect your motor power supply (e.g., 7.2V to 35V) to the L298N's screw terminals. The positive wire goes to the `+12V` (or VMS) terminal, and the negative wire goes to the `GND` terminal.
  • Powering the ESP32: The L298N module has an onboard 5V voltage regulator. Once you provide external power, this regulator generates a stable 5V, which is available at the `+5V` terminal. You can use this to power your ESP32 by connecting a wire from the L298N's `+5V` terminal to the ESP32's `5V` (or Vin) pin. This eliminates the need for a separate power supply for the microcontroller.
  • Common Ground: It is essential to have a common ground. Connect a wire from the L298N's `GND` terminal to a `GND` pin on the ESP32.

Control Pin Connections

Connect the six control pins from the L298N to the ESP32 as follows. These pins control the speed and direction of the two motors.

  • Motor 1 (L298N Side A):
    • `ENA` connects to ESP32 pin 19 (Speed Control)
    • `IN1` connects to ESP32 pin 18 (Direction Control)
    • `IN2` connects to ESP32 pin 5 (Direction Control)
  • Motor 2 (L298N Side B):
    • `ENB` connects to ESP32 pin 4 (Speed Control)
    • `IN3` connects to ESP32 pin 17 (Direction Control)
    • `IN4` connects to ESP32 pin 16 (Direction Control)

Arduino IDE & Library Setup

Before uploading the code, you need to prepare your Arduino IDE to work with the ESP32. The setup process is detailed in the video at 07:41.

The link to use in the "preferences" of the Arduino IDE for the ESP32 board is:
https://dl.espressif.com/dl/package_esp32_index.json Watch the video for instructions.

  1. Add ESP32 Board URL: Go to File > Preferences. In the "Additional Boards Manager URLs" field, you need to paste the official JSON URL for ESP32 boards.
  2. Install ESP32 Boards: Open the Boards Manager (Tools > Board > Boards Manager), search for "esp32", and install the package provided by Espressif Systems.
  3. Select Board & Port: Go to Tools > Board and select a suitable board like "ESP32 Rover Module". Then, go to Tools > Port and select the COM port that your ESP32 is connected to. You can find the correct port number in your computer's Device Manager.
  4. Install Robojax Library: You must download the Robojax L298N library, which will be available as a .zip file. In the Arduino IDE, go to Sketch > Include Library > Add .ZIP Library... and select the downloaded file to install it.

User-Customizable Code Settings

The provided code is designed to be easy to use. You only need to configure a few parameters at the top of the file to match your setup and preferences. This is explained starting at 12:20.

Wi-Fi Credentials

You must change these two lines to match your local Wi-Fi network's name (SSID) and password. Remember that the SSID is case-sensitive.

const char *ssid = "YourWifiName";
const char *password = "YourWifiPassword";

Library Initialization (Debug Mode)

The code offers two options for initializing the library. For troubleshooting, you can enable debug mode, which prints detailed status information to the Serial Monitor. For normal operation, use the line without debugging to save resources. To switch, simply comment one line and uncomment the other.

// for two motors without debug information
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA, CHA, IN3, IN4, ENB, CHB);

// for two motors with debug information
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, CHA, IN3, IN4, ENB, CHB, true);

Motor Behavior Parameters

You can set the default behavior for each motor independently.

// MOTOR 1 SETTINGS
int       motor1Direction = CW; // Default direction: CW or CCW
const int motor1changeStep = 10; // Speed change per click (e.g., 10%)
int       motor1Speed = 40; // Initial speed when the page loads (0-100)
const int motor1MinimumSpeed = 20; // The minimum speed the motor will run at
const int motor1MaximumSpeed = 100; // The maximum speed the motor will run at
int       motor1StopState = HIGH; // Motor state on load: HIGH=Stopped, LOW=Running

// MOTOR 2 SETTINGS
int       motor2Direction = CW;
const int motor2changeStep = 10;
int       motor2Speed = 60;
const int motor2MinimumSpeed = 20;
const int motor2MaximumSpeed = 100;
int       motor2StopState = HIGH;

Live Project in Action

After uploading the code, open the Arduino Serial Monitor and press the reset button on the ESP32. The ESP32 will connect to your Wi-Fi and print its IP address.

Type this IP address into the browser of a phone or computer that is connected to the same Wi-Fi network. The control page will load, showing controls for both motors. You can use the buttons to increase or decrease speed, change direction, and start or stop each motor. The web interface will update in real-time to show the current status (speed, direction, and running/stopped state) of each motor.


Video Timestamps

  • 00:00 - Introduction & Final Project Demo
  • 02:02 - L298N Module Overview
  • 05:05 - Wiring Guide
  • 07:41 - Arduino IDE Setup for ESP32
  • 09:49 - Robojax Library Installation
  • 12:20 - Explanation of User-Customizable Code Settings
  • 22:46 - Live Project Demonstration

Images

DC motor with ESP32: main setup
DC Motor with ESP32: main setup
DC motor with ESP32: control screen
DC Motor with ESP32: Control Screen
DC motor with ESP32: Wiring to L298N
DC Motor with ESP32: Wiring to L298N
DC motor with ESP32: Code page and control screen view
DC Motor with ESP32: Code page and Control screen view
DC motor with ESP32: Demonstration
DC Motor with ESP32: Demonstration
DC motor with ESP32: Controlling a DC motor using a mobile phone
DC Motor with ESP32:Controlling DC Mover Using Mobile Phone
279-Arduino code to control DC Motors with ESP32 with L298N module over WiFi (two motors)
Language: C++
/*
 * To control DC Motors with ESP32 with L298N module over WiFi (two motors)
 * Motor is controlled using Robojax_L298N_DC_motor library
 * 
 * 
 * Watch video instruction for this code (with Wifi): https://youtu.be/Olq8NXgNySA
 * Watch video instruction for this code (without WiFi): https://youtu.be/gOMHU0Q8upA


 * Written by Ahmad Shamshiri on Dec 27, 2019
 * in Ajax, Ontario, Canada. www.robojax.com
 * 

 * Get this code and other Arduino codes from Robojax.com


or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
   Copyright (c) 2015, Majenko Technologies
   All rights reserved.

   Redistribution and use in source and binary forms, with or without modification,
   are permitted provided that the following conditions are met:

 * * Redistributions of source code must retain the above copyright notice, this
     list of conditions and the following disclaimer.

 * * Redistributions in binary form must reproduce the above copyright notice, this
     list of conditions and the following disclaimer in the documentation and/or
     other materials provided with the distribution.

 * * Neither the name of Majenko Technologies nor the names of its
     contributors may be used to endorse or promote products derived from
     this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define CHA 0
#define ENA 19 // this pin must be PWM enabled pin if Arduino board is used
#define IN1 18
#define IN2 5

// motor 2 settings
#define IN3 17
#define IN4 16
#define ENB 4// this pin must be PWM enabled pin if Arduino board is used
#define CHB 1

const int CCW = 2; // do not change
const int CW  = 1; // do not change

#define motor1 1 // do not change
#define motor2 2 // do not change

// for single motor
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA, CHA, true);  

// for two motors without debug information // Watch video instruction for this line: https://youtu.be/2JTMqURJTwg
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA, CHA, IN3, IN4, ENB, CHB);/

// for two motors with debug information
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, CHA, IN3, IN4, ENB, CHB, true);


int       motor1Direction = CW;//default direction of rotation
const int motor1changeStep = 10;// 10 is 10% every time button is pushed
int       motor1Speed = 40;// variable holding the light output value (initial value) 40 means 40%
const int motor1MinimumSpeed=20;
const int motor1MaximumSpeed=100;
int       motor1StopState=HIGH;//Stope state of motor (HIGH means STOP) and LOW means Start

int       motor2Direction = CW;//default direction of rotation
const int motor2changeStep = 10;// 10 is 10% every time button is pushed
int       motor2Speed = 60;// variable holding the light output value (initial value) 40 means 40%
const int motor2MinimumSpeed=20;
const int motor2MaximumSpeed=100;
int       motor2StopState=HIGH;//Stope state of motor (HIGH means STOP) and LOW means Start


#include "ESP32_L298N_DC_motor_wifi_page.h"


  
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char *ssid = "Robojax";
const char *password = "YouTube2019_o_";

WebServer server(80);

const int led = 13;

void handleRoot() {
String HTML_page = motorControlHeader_1; 

 HTML_page.concat(".bar1 {width: " + String(motor1Speed)  + "%;}\n");
 //HTML_page.concat(motor1Speed);  
 //HTML_page.concat("%;}");
 HTML_page.concat(".bar2 {width: " + String(motor2Speed) + "%;}\n");
  
 HTML_page.concat(motorControlHeader_2);
  
 HTML_page.concat(motor1Control_p1);
   
 if(motor1Direction ==CW)
 {
      if(motor1StopState ==HIGH)
      {
        HTML_page.concat("<strong>Stopped - CW at ");
      }else{
        HTML_page.concat("<strong>Running - CW at ");        
      }
 }else{
      if(motor1StopState ==HIGH)
      {
        HTML_page.concat("<strong>Stopped - CCW at ");
      }else{
        HTML_page.concat("<strong>Running - CCW at ");        
      }  
 }
 HTML_page.concat(motor1Speed);
 HTML_page.concat(motor1Control_p2);
 if(motor1StopState ==HIGH)
 {
   HTML_page.concat("m1START\">START");
 }else{
   HTML_page.concat("m1STOP\">STOP"); 
 }
 HTML_page.concat(motor1Control_p3); 

///motor 2 begins
 HTML_page.concat(motor2Control_p1);
   
 if(motor2Direction ==CW)
 {
      if(motor2StopState ==HIGH)
      {
        HTML_page.concat("<strong>Stopped - CW at ");
      }else{
        HTML_page.concat("<strong>Running - CW at ");        
      }
 }else{
      if(motor2StopState ==HIGH)
      {
        HTML_page.concat("<strong>Stopped - CCW at ");
      }else{
        HTML_page.concat("<strong>Running - CCW at ");        
      }  
 }
 HTML_page.concat(motor2Speed);
 HTML_page.concat(motor2Control_p2);
 if(motor2StopState ==HIGH)
 {
   HTML_page.concat("m2START\">START");
 }else{
   HTML_page.concat("m2STOP\">STOP"); 
 }
 HTML_page.concat(motor2Control_p3); 
 
 HTML_page.concat("</body>\n</html>");
 
  server.send(200, "text/html", HTML_page);
}

void handleNotFound() {
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }

  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void) {
  Serial.begin(115200);
  motor.begin();
  //L298N DC Motor by Robojax.com

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  
    
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());


  if (MDNS.begin("robojaxESP32")) {
    Serial.print("MDNS responder started at http://");
    Serial.println("robojaxESP32");
  }

  server.on("/", handleRoot);
  server.on("/speed", HTTP_GET, handleMotorSpeed);  
  server.on("/direction", HTTP_GET, handleMotorDirection); 
  server.on("/stop", HTTP_GET, handleMotorBrake);     
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started"); 
}

void loop(void) {
  server.handleClient();


  if(motor1StopState ==HIGH)
  {
    motor.brake(motor1);  
    
  }else{
     motor.rotate(motor1, motor1Speed, motor1Direction);//run motor1 at motor1Speed% speed in motor1Direction 
  }
  
  if(motor2StopState ==HIGH)
  {
    motor.brake(motor2);  
    
  }else{
     motor.rotate(motor2, motor2Speed, motor2Direction);//run motor2 at motor2Speed% speed in motor2Direction 
  }

  delay(100);  
}


/*
 * handleMotorSpeed()
 * Slows down or speeds up the motor
 * returns nothing
 * Written by Ahmad Shamshiri on Dec 27, 2019
 * www.Robojax.com
 */
void handleMotorSpeed() {
  if(server.arg("do") == "m1slower" )
  {
    motor1Speed -=motor1changeStep;
    
      if(motor1Speed < motor1MinimumSpeed)
      {
        motor1Speed = motor1MinimumSpeed;
      }
  }else if(server.arg("do") == "m1faster")
  {
    motor1Speed +=motor1changeStep;   
     
      if(motor1Speed > motor1MaximumSpeed)
      {
        motor1Speed =motor1MaximumSpeed;
      } 
  }else if(server.arg("do") == "m2slower")
  {
    motor2Speed -=motor2changeStep;
    
      if(motor2Speed < motor2MinimumSpeed)
      {
        motor2Speed = motor2MinimumSpeed;
      }
  }else if(server.arg("do") == "m2faster")
  {
    motor2Speed +=motor2changeStep;   
     
      if(motor2Speed > motor2MaximumSpeed)
      {
        motor2Speed =motor2MaximumSpeed;
      } 
  }else{
    motor1Speed =0;   

  }

  handleRoot();
}//handleMotorSpeed() end

/*
 * handleMotorDirection()
 * changes the direction of the motor
 * returns nothing
 * Written by Ahmad Shamshiri on Dec 27, 2019
 * www.Robojax.com
 */
void handleMotorDirection() {
  if(server.arg("dir") == "m1CW")
  {
    motor1Direction =CW;

  }else if(server.arg("dir") == "m1CCW")
  {
    motor1Direction =CCW;

  }else if(server.arg("dir") == "m2CW")
  {
    motor2Direction =CW;

  }else{
    motor2Direction =CCW;   

  }

  handleRoot();
}//

/*
 * handleMotorBrake()
 * applies brake to the motor
 * returns nothing
 * Written by Ahmad Shamshiri on Dec 27, 2019
 * www.Robojax.com
 */
void handleMotorBrake() {
  if(server.arg("do") == "m1START")
  {  
      motor1StopState=LOW;
  }else if(server.arg("do") == "m1STOP")
  {  
      motor1StopState=HIGH;
  }else if(server.arg("do") == "m2START")
  {  
      motor2StopState=LOW;
  }else{
      motor2StopState=HIGH;    
  }
  handleRoot();
}//

Files📁

Other files