Search Code

Using an L298N Module to Control Two DC Motors with a Library

Using an L298N Module to Control Two DC Motors with a Library

Using an L298N Module to Control Two DC Motors with a Library

In this tutorial, we will learn how to control two DC motors using the L298N motor driver module. This module allows for precise control over the speed and direction of the motors, making it ideal for various robotic applications. We will walk through the necessary hardware, wiring, and code to get everything up and running smoothly.

By the end of this tutorial, you will be able to implement motor control using the provided library, which simplifies the process significantly. To see the entire process in action, make sure to check the accompanying video (in video at 00:00).

Hardware Explained

The L298N module is a dual full-bridge motor controller that allows you to control the speed and direction of two DC motors. Each motor is controlled using two inputs for direction and one enable pin for speed control through PWM (Pulse Width Modulation). The module can handle voltages up to 35V and currents up to 2.5A per motor, making it suitable for a variety of applications.

Each motor's direction is controlled by the input pins IN1 and IN2 for one motor, and IN3 and IN4 for the other. The enable pins ENA and ENB allow for speed control via PWM signals. The L298N also has built-in diodes to protect against back EMF generated by the motors, ensuring safe operation.

Datasheet Details

ManufacturerSTMicroelectronics
Part numberL298N
Logic/IO voltage5 V
Supply voltage7–35 V (VS)
Output current (per channel)2 A max/channel (continuous)
Peak current (per channel)3 A max/channel
PWM frequency guidance5–20 kHz
Input logic thresholds0.8 V (high) / 2.0 V (low)
Voltage drop / RDS(on) / saturation1.8 V at 2 A
Thermal limits150 °C
PackageMultiwatt15
Notes / variantsDual full-bridge driver

  • Ensure proper heat sinking for reliable operation.
  • Use PWM on the enable pins for speed control.
  • Do not exceed the voltage or current specifications.
  • Double-check wiring to avoid short circuits.
  • Common pitfalls include floating inputs; ensure all pins are connected properly.

Wiring Instructions

ESP32_L298N_wiring

To wire the L298N to your Arduino and connect the DC motors, follow these steps:

Start by connecting the power supply to the L298N module. Connect the positive terminal of your battery to the +12V pin and the negative terminal to the GND pin. Next, connect your first motor to the OUT1 and OUT2 terminals, and the second motor to OUT3 and OUT4.

For the control pins, connect ENA (pin 3) to a PWM-enabled pin on your Arduino. Connect IN1 (pin 2) and IN2 (pin 4) to digital pins on the Arduino for the first motor. For the second motor, connect IN3 (pin 7) and IN4 (pin 8), and connect ENB (pin 9) to another PWM-enabled pin. Finally, connect the 5V output from the L298N to the Arduino's 5V input to power the Arduino when not connected to a computer.

Code Examples & Walkthrough

In the code, we first include the L298N library and define the pins used for the motors. The identifiers IN1, IN2, ENA are defined for motor one, while IN3, IN4, ENB are for motor two. The constants CCW and CW are defined for counterclockwise and clockwise rotation, respectively.

#include 
#define IN1 2
#define IN2 4
#define ENA 3 // this pin must be PWM enabled pin
#define IN3 7
#define IN4 8
#define ENB 9 // this pin must be PWM enabled pin

This excerpt shows the initial setup of the library and pin definitions. The use of PWM for speed control is crucial, as indicated by the comments in the code.

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

In the setup() function, we initialize the serial communication and the motor control library. This prepares the system to control the motors effectively.

void loop() {
  motor.rotate(motor1, 60, CW);//run motor1 at 60% speed in CW direction
  motor.rotate(motor2, 60, CCW);//run motor2 at 60% speed in CCW direction  
  delay(3000);
}

The main loop demonstrates how to control the motors by calling the rotate() function. Here, both motors are set to run at 60% speed for three seconds. This is a straightforward example of how the library simplifies motor control.

Demonstration / What to Expect

Upon completing the wiring and uploading the code, you should see both motors rotate according to the specified speeds and directions. Common pitfalls include ensuring that the power supply matches the motor specifications and that all connections are secure. If the motors do not operate as expected, check for floating inputs or incorrect wiring (in video at 00:00).

Video Timestamps

  • 00:00 Start
  • 01:28 Introduction
  • 02:06 Hardware Explained
  • 09:10 Wiring explained
  • 11:43 Code Explained
  • 18:42 L298N 2 motor demonstration
  • 20:40 Larger motor demonstration
  • 22:10 Connecting motor into parallel mode
  • 25:12 Parallel mode demonstration

Images

L298N Motor Controller Module
L298N Motor Controller MOdule
L298N Motor Controller Module
L298N Motor Controller MOdule
L298N Motor Controller Module
L298N Motor Controller MOdule
ESP32_L298N_wiring
ESP32_L298N_wiring
249-Code to control two DC motors using an L298N module
Language: C++
/*
 * Library Example for L298N Module to control DC motors
 * 
 * This code is to control two DC motors. 
 * 
 * Written by Ahmad Shamshiri on October 1, 2019 at 21:26 
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 * Need wiring diagram for this code: 
 * 
 * Watch video instructions for this code: https://youtu.be/wn5D7j1ybxY
 * 
 * Get this code and other Arduino codes from Robojax.com.
 * Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
 * all in one place. 

 * If you found this tutorial helpful, please support me so I can continue creating 
 * content like this. 

 * 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/>.
 */

#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 2
#define IN2 4
#define ENA 3 // this pin must be PWM enabled pin

// motor 2 settings
#define IN3 7
#define IN4 8
#define ENB 9 // this pin must be PWM enabled pin

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

// use the line below for single motor
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA, true);

// use the line below for two motors
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, IN2, IN3, ENB, true);

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

}

void loop() {
  
  motor.demo(1);
  motor.demo(2);  
  motor.rotate(motor1, 60, CW);//run motor1 at 60% speed in CW direction
  motor.rotate(motor2, 60, CCW);//run motor2 at 60% speed in CCW direction  
  delay(3000);

  motor.brake(1);
  motor.brake(2);
  delay(2000);


  motor.rotate(motor1, 100, CW);//run motor1 at 60% speed in CW direction
  motor.rotate(motor2, 100, CCW);//run motor2 at 60% speed in CCW direction  
  delay(3000);

  motor.brake(1);
  motor.brake(2);
  delay(2000);  

  for(int i=0; i<=100; i++)
  {
    motor.rotate(motor1, i, CW);// turn motor1 with i% speed in CW direction (whatever is i) 
    delay(100);
  }
  delay(2000);
  
  motor.brake(1);
  motor.brake(2);
  delay(2000);  

  motor.rotate(motor1, 70, CCW);//run motor1 at 70% speed in CCW direction
  motor.rotate(motor2, 5, CW);//run motor2 at 50% speed in CW direction  
  delay(4000);

  motor.brake(3);
  delay(2000);    
  
}

Things you might need

Resources & references

Files📁

Fritzing File

Other files