L298N Robot-How to drive DC motors with L298N and Arduino

Hello Youtube, this is Chen TheDesignMaker! Welcome to my channel of designing and making cool stuff, so if it’s your first time here, please subscribe for future awesome videos!

Today we are gonna learn how to use L298N and arduino to drive some motors and make this weird looking robot.

The L298N is a motor driver that can PWM control 2 DC motors or 1 stepper motor. It is widely used for robotic projects such as the one I’m working on right now, so don’t forget the check it out. The module is very affordable and link is in the description below.

So without further due, Let’s get started (Engine start sound)

Take a close look at this module, The biggest part with the heatsink on is the actual L298N Chip, the front terminal blocks are for the input and output of the power. The +12v is for driving the motor and the +5v is for the logics. Think it as a faucet with a two directional valve, the +12v terminal are the water running through the pipe, and the +5v is like the force controls the valve.

Two sets of terminal blocks on both sides are for the 2 DC motors with very straightforward connections. And at last there are 6 pins at front are used to control the motors.

How does this module control the motors? The 2 pins with jumpers on both ends are motor enable pins, leave the jumper on or connect to HIGH voltage level means full throttle. Get the jumper pins removed or connect to LOW logic level, the motor will be parked. If PWM is feed into these pins, you get yourself a speed control.

The rest of the 4 pins are like the gear shifter switching between Driving and Reversing. Refer to this chart to see how they work.

Let’s look at some examples, if we are driving these 6v motors, Power is needed for both the driving and logic, here I connected 4 AA batteries into the +12v and GND terminals, and also a jumper to connect the 12 the 5V.

A really quick way to test your module is using one of these male to female cable, since we have Motor A connected, just connect the IN1 and 5v, you’ll see the motor start running, move the pin to IN2, it starts to reverse.

If you are driving bigger 12v motors like this one, you can connect the motor the same way, with a simpler power connection. Just plug in your power source to the 12V and ground, here I’m using a stripped 12v power supply. The onboard 78M05 voltage regulator can step down anything between 7 to 12 v to 5 so that it not only provides the logic needed from the L298N, but also made the +5v terminal an output. We can use the same technic to test the motor.You can use the +5 terminal to power arduino as well.

For any motor needs more than 12v up to 24, it is recommended to remove this jumper pin to disable the onboard 78M05.

To control the motors with arduino, we’ll just connect the 6 controlling pins to arduino digital pins, make sure the ENA and ENB are connected to PWM pins for speed control later. Here we’re connecting the 6 pins to arduino digital pin 5 to 10.

Now we got everything connected. The 2 motors go into the motor terminals, thanks to the support from this awesome helping hand. 4AA batteries connect to the +12 and GND, also a jumper pin to bridge +12 and +5. Arduino is connected as well. One last thing, since I’m powering the Ardunio from USB with computer, it need to share ground with the batteries, just simply connect the GND on arduino and GND terminal on the module.

let’s start the arduino programing: starting from most straightforward code: The first step is to define all the pin numbers. During the setup, we define all the pin to OUTPUT modes, then assign them default values so they stay stopped. In the loop, we can just analogwrite a PWM value from 0 to 255 to control the speed. The bigger the value, the faster the motor runs. And we also digitalwrite IN1 and 2 to control the motor directions. Don’t forget to add a delay in the end. But, if you are going to control the motor a lot with different direction or speed such as driving a path. This would be too much work, you’ll have to repeat this paragraph over and over again. It’s not efficient at all. Here’s a simpler version, I defined a MOTOR CLASS here that holds all the pin information. It has a DRIVE method that takes one speed variable to drive the motor. If it’s from 0 to 255, the motor will drive forward. If it’s from 0 to -255 will drive backwards. Once we have the 2 motors instances defined before the setup, we can control the speed and direction with one line of code just using the DRIVE function. You can make your own functions by combining multiple motors too. The full code can be found in the description.

Hope this tutorial is helpful for your next project, let me know if you have any questions and what you’re gonna build with this! Thanks for watching and if you like my video, don’t forget to like and subscribe! See you next time!

Basic Code:

The basic code is the raw way to control your motors, it works but could be tidious controlling different actions. So please just use it to make sure your system is working and to understand how it works.

/* Arduino DC Motor L298N Module H-bridge DC Motor
By Chen The Design Maker chenthedesignmaker.com
*/

//Define all the connections maps to the L298N
#define enA 10
#define in1 9
#define in2 8

#define in3 7
#define in4 6
#define enB 5

void setup() {
// Setup pin mode
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Set initial direction and speed
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}

void loop() {
//Drive Motor Forward
analogWrite(enA, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

delay(2000);

//Drive Motor Backward
analogWrite(enA, 255);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

delay(2000);

}

Advanced Code:

In this advanced code, I created this Motor class that you can initiate in the beginning of the code, then you can just control it with one line of code with a single speed parameter. This makes it very easy to control multiple motors with multiple actions.

/* Arduino DC Motor L298N Module H-bridge DC Motor
By Chen The Design Maker chenthedesignmaker.com
*/

//Define all the connections maps to the L298N
#define enA 10
#define in1 9
#define in2 8

#define in3 7
#define in4 6
#define enB 5

class Motor{

int enablePin;
int directionPin1;
int directionPin2;
public:

//Method to define the motor pins
Motor(int ENPin,int dPin1,int dPin2){
enablePin = ENPin;
directionPin1 = dPin1;
directionPin2 = dPin2;
};

//Method to drive the motor 0~255 driving forward. -1~-255 driving backward
Drive(int speed){
if(speed>=0){
digitalWrite(directionPin1, LOW);
digitalWrite(directionPin2, HIGH);
}
else{
digitalWrite(directionPin1, HIGH);
digitalWrite(directionPin2, LOW);
speed = - speed;
}
analogWrite(enablePin, speed);
}
};
Motor leftMotor = Motor(enA, in1, in2);
Motor rightMotor = Motor(enB, in3, in4);

void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Set initial direction and speed
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}

void loop() {

leftMotor.Drive(255);
rightMotor.Drive(200);

delay(2000);

leftMotor.Drive(-150);
rightMotor.Drive(-255);

delay(2000);

leftMotor.Drive(150);
rightMotor.Drive(150);

delay(2000);

}

Parts list:

L298N Module:

Banggood:https://www.banggood.com/Wholesale-Dual-H-Bridge-DC-Stepper-Motor-Drive-Controller-Board-Module-Arduino-L298N-p-42826.html?p=JH2514288496201309O3

Amazon: http://amzn.to/2j8DWmT

3-6V gear DC motor:

Banggood: https://www.banggood.com/Plastic-Tire-Wheel-With-DC-3-6v-Gear-Motor-For-Arduino-Smart-Car-p-997113.html?p=JH2514288496201309O3

Amazon:http://amzn.to/2CGGcu6

12V DC motor:

Banggood:https://www.banggood.com/DC-12V-356065110220rpm-Worm-Gear-Box-Reduction-Motor-p-1197020.html?p=JH2514288496201309O3

Amazon:http://amzn.to/2AZzxhu

Helping hand:

Banggood: https://www.banggood.com/Multifunctional-Welding-Magnifier-LED-Helping-Hand-Soldering-Iron-Stand-Magnifying-Lens-Magnifier-Clamp-Tool-p-1061332.html?p=JH2514288496201309O3

Amazon: http://amzn.to/2olH6ZK

4AA Battery holder

Banggood:https://www.banggood.com/4-X-AA-Battery-Holder-Case-Enclosed-Box-OFFON-Switch-With-Wires-p-925732.html?p=JH2514288496201309O3

Amazon: http://amzn.to/2CFwesR

Male to female cable:

Banggood:https://www.banggood.com/3-IN-1-120pcs-10cm-Male-To-Female-Female-To-Female-Male-To-Male-Jumper-Cable-Dupont-Wire-For-Arduino-p-1054670.html?p=JH2514288496201309O3

Amazon:http://amzn.to/2AXgBzP

DISCLAIMER: This post contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!

6 thoughts on “L298N Robot-How to drive DC motors with L298N and Arduino”

  1. i have posted this on instructables:
    the dc motor, i assume it is a servo, my question is whether your setup with arduino(
    of course not nano….uno and up)+l298n with rds3115 ( its listed in banggood), and i
    expect i will have to modify the code you have professionally provided( no sarcasm
    meant);
    have a good day !!!
    surv

  2. Why do you use (with the 6V battery package and 6V motors) a jumper wire between the +12V and the +5V (for logics) ?

    For motor voltages < 12V the onboard jumper can connects to the onboard voltage regulator that provides 5V to the +5V (output) (for logics and Arduino.
    I think this is also the case with a 6V battery package.

    If you use (6V battery package) and a jumper wire between +12V and +5V (input) for logics, I think you have to remove the (voltage regulator) onboard jumper to prevent supply to the voltage regulator to output 5V to +5V to.

    I think you don't need the jumper wire between +12V and +5V if using a 6V battery package and use the onboard voltage regulator to supply 5V to the +5V (output) pin for logics and Arduino. This is also the option for other motor voltages < 12V.

    Is in your opinion a 6V battery package to +12V and a jumper wire between +12V and +5V (input ? or output ?) and leaving the voltage regulator onboard jumper installed no problem ?

    1. Hi Nieko,

      Good question, my current setup has no issue for sure. Based on my research, the on board regulator has a 2V drop since all the transistors inside. which means, a 6V battery pack, would only be 4V after the regulation. Which might not be sufficient for the arduino(Haven’t tested though.) So that’s why I have a jumper there, I don’t think the regulator jumper matters anyway.

      Chen

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar