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.

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.

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!

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

Hi, I haven’t tested the L293D but after reading the date sheet here: http://www.ti.com/lit/ds/symlink/l293.pdf , I realized it achieved similar function with different connections. Please refer to page 11 for the details.

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