How to control a DC motor with L298N driver and Arduino Uno

Dear friends, welcome to another Arduino tutorial. In this video we are going to learn how to control a DC motor using any Arduino board. We are going to use a small and inexpensive module that features the L298N motor driver chip. There is a lot to cover so let’s get started.

Where to get the parts

If you want to build your first robot you will eventually need to learn how to control a DC motor. One of the easiest and inexpensive ways to control a DC motor is the L298N motor driver. This board can control both the speed and the spinning direction of two DC motors. Let’s see how to use it with Arduino.

🛒 Arduino Uno ▶ http://educ8s.tv/part/ArduinoUno

🛒 L298N Driver ▶ http://educ8s.tv/part/L298N

🛒 2 DC Motors ▶ http://educ8s.tv/part/DCmotors

🛒 Battery Holder ▶ http://educ8s.tv/part/5AAHolder

🛒 Wires ▶ http://educ8s.tv/part/Wires

Connecting the Parts

First, let’s learn how to control one motor.

The first step is to connect the motor to the motor controller board. I am using a DC motor which has an operating voltage of 3-12V and my power supply has an output voltage of 7.5V. Be careful, if your power supply voltage exceeds the maximum operating voltage of your motor you are going to damage it. The connections I present in this tutorial work with a power supply voltage of up to 12V.

Let’s now connect the motor. We have to connect the red wire of the motor to the OUT1 screw terminal of the motor controller board. Then we have to connect the black wire of the motor to the OUT2 screw terminal of the board. It can be the other way around, it doesn’t really matter.

Now let’s connect the Arduino Uno board. We only need to connect 3 wires to control this motor.

Using a male to female wire, we connect Digital Pin 5 of the Arduino Uno to the ENA pin of the motor driver board. If there is a jumper wire at this pin, we have to remove it. We are going to use this pin to control the speed of the motor, so this pin must support PWM. If you look closely to the Arduino board you can see this ~ symbol. This symbol means that this pin supports PWM. 

If we want to control the rotation speed of a DC motor, like this one, we have to control the voltage it receives. The higher the voltage, the higher the speed it rotates. This DC motor has an operating voltage of 3-12V, so if we connect this motor to a 12V power supply it will rotate as fast as it can but if we exceed this voltage we will destroy it. 

So if we want to control its speed we have to control the input voltage we apply to it.  A very common technique for doing so is PWM and that’s why we have to use a PWM pin of the Arduino Uno. This digital pin, will send pulses instead of a steady HIGH signal to the motor, so by adjusting the time the pulses stay HIGH, we can adjust the average voltage the motor receives, so we can control its speed! Save

Next we connect Digital Pin 6 of the Arduino Uno to the IN1 pin of the motor board and Digital Pin 7 of the Arduino Uno to the IN2 pin of the motor board. These two pins control the spinning direction of the motor. 

If we want to control the spinning direction of the motor all we have to do is change the polarity of its input voltage. How can we achieve that? We can achieve that using an H-Bridge circuit. Luckily for us, the L298N chip contains two H-Bridges so it can control 2 DC motors at once. 

Now let’s connect power to the controller board. As a power supply I am going to use 5 AA batteries which output 7.5V. So, I connect the red wire of the battery holder to the 12V input screw terminal of the board. Check below the board to identify which terminal is the +12V input. Next I connect the black wire of the battery holder to the GND screw terminal. The LED of the board lights up; the board is receiving power! 

Now we have to power the Arduino board. The motor controller board has a 5V regulator on board, this little chip here. So, if the input voltage

from the power supply is below 12V and over 5V it will output 5V at this terminal. So we are going to connect this output pin to the Vin of the Arduino Uno, and we are going to connect the common ground of the motor controller board to the GND pin of the Arduino Uno board. That’s it. The Arduino Uno board and the motor controller board are both powered by this 7.5V power supply. Let’s now go to the computer to see how to program the Arduino Uno to control the motor using the Arduino IDE.

Software

The code we need is very straight forward. We don’t need any libraries at all. At first we declare the pins we have connected to the motor controller board. Next we declare that these pins are going to be outputs. If we want the motor to rotate clockwise we have to send HIGH to the IN1 input of the board and LOW to the IN2 input of the board. That’s what we do in these two lines of code. If we don’t set the speed of the motor, it won’t rotate at all. So, we have to send a PWM signal to ENA pin of the motor controller board. We do so in this line here. This value is the speed of the motor. We can enter any value from 0 – 255 where 255 is the max speed and 0 obviously the lowest speed, zero. In this line we have entered 255 so the motor will run at full speed. In the next line we pause the execution of the program for 2 seconds so, the motor will rotate clockwise for 2 seconds.

In the next 3 lines we stop the motor by sending a LOW signal on both IN1 and IN2 inputs of the motor controller board. We again wait for 2 seconds before executing the next commands. Next we send a LOW signal to the IN1 input and a HIGH signal to the IN2 input so the motor now rotates counterclockwise at full speed for two seconds.  Cool huh! Controlling the motor is very easy.

If we want to reduce the rotation speed of the motor we simply have to set a lower value to this command. In this example, I set the value at 127, so the motor should rotate at half speed. We press the upload button and after a few seconds the Arduino runs our code and the motor starts rotating!

Let’s watch this example for a few seconds…

As you can see, the motor at first rotates clockwise at full speed, then it stops and starts rotating counterclockwise for another two seconds. It then starts rotating clockwise again at half speed, stops for two seconds and then starts rotating counterclockwise at half speed. Cool, we now know how to control a DC motor with Arduino.

Conclusion

If we want to control 2 motors at the same time we follow the same procedure. We have to connect another 3 wires to the Arduino board. We connect Digital Pin 8 to the IN3 input of the motor controller board and Digital Pin 9 to IN4. Lastly we have to connect Digital Pin 10 of the Arduino board to the ENB pin of the board which controls the speed of the motor. Digital Pin 10 of the Arduino board also supports PWM so we can use it to control the speed of the second motor. We then connect the red wire of the second motor to the OUT3 pin of the board, and the black wire to the OUT4 pin of the board. 

The code to control the second motor is identical to the code we used to control the first motor. All we have to do is to declare the new pins, and use them to control the motor as we did with the first one. If we want to rotate the motor clockwise we have to send a HIGH signal to the IN3 input and a LOW signal to the IN4 input.  If we set the IN3 LOW and the IN4 HIGH the motor will rotate counterclockwise.

If we run this script we can see that we can control two motors at the same and it is pretty easy. As always you can find the code of the project in the video description. 

This is all you need to know in order to control two dc motors with Arduino. With this knowledge you can now  build your own robot easily! Add some sensors, experiment with the code and have fun! I can’t wait to see what you build! Please, post your robot projects ideas in the comments section below. I am going to use a lot this motor controller board in my future robot projects so stay tuned!

Thank you very much for watching this video I hope it was useful! Stay safe and I’ll see you in the next one!

Thank you very much!