In this article, we shall be discussing how to use L298n with Arduino. We shall be learning how to control DC motors using an Arduino.
Furthermore, we shall be looking at some of the basic techniques used in controlling DC motors, and you will learn how you can control the L298n driver using an Arduino board. Mastering this application is vital; however, it can be relatively challenging for a newbie.
Overview
You can easily control a DC motor’s speed by controlling input voltage to your motor; usually, the PWM signal is the most common way in which you can achieve this.
The PWM signal motor control
Pulse Width Modulation, commonly referred to as PWM, is a technique that allows one to adjust the average value of voltage being transferred to an electrical device.
It achieves this through turning off and on power at an incredible speed. Usually, an average voltage will depend on the duty cycle or amount of time in which a signal is on vs. amount of time it is off within a single period.
In accordance with the size of your motor, you can connect your Arduino PWM output to the base of a transistor or gate of MOSFET and have full control of the speed of your motor by controlling PWM output. Lower power Arduino PWM signal will switch off and on the gate at MOSFET in which a high power motor is being driven.

The H-Bridge motor control
However, when it comes to controlling rotational direction, you will require the inverse direction of your current flow via the motor, and the H-Bridge is considered the most common method in which you can achieve this.
Usually, the H-bridge circuit is equipped with four switching elements, MOSFET or transistors, with a motor at the center, which forms an H-like configuration. When you activate two switches simultaneously, you will be able to change the direction of your current flow, allowing you to change your motor’s rotational direction.
When you combine the two methods, that is; H-bridge and PWM signal, you will be able to complete control over a DC motor. The best part is that there are several DC motor drives that are equipped with both of these features, one of them being the L298n.


L298n
This is a dual H-Bridge motor driver that has been designed to offer both direction and speed control of two motors simultaneously. The L298n motor driver is capable of driving DC motors with voltages between 5 volts and 35 volts along with a peak current of approximately 2 Amps.
L298n is equipped with two screw terminal blocks allowing you to attach two motors at the same time. That is motor A and motor B; in addition to that, there is an additional screw terminal block for connecting Ground Pin, VCC for motor along with a 5-volt pin that can act as an input or output. This will highly depend on the voltage being used at the motors VCC. The module is often equipped with a 5 volts regulator that can either be disabling or enabled using a jumper.
When the supply voltage of the motor is up to12 volts, you can enable your 5 volts regulator along with the 5 volts to act as an output, for instance, when powering an Arduino. However, when the motor voltage is more than 12 volts, you will have to disconnect the jumper since the voltage is too high and can cause damage to your onboard 5 volts regulator. In such a case, a 5-volt pin will serve as an input while connecting it to a 5-volt power supply allowing the IC to function accordingly.
You will note that the IC creates a drop in the voltage by approximately 2 volts. For instance, when using a 12-volt power supply, your motor terminal’s voltage will be approximately 10 volts. This means that it will not be sufficient to power up your 12 volts DC motor to its maximum speed.
The other thing you will need to factor in is logic control inputs. Enable pin A and B are often used in enabling as well as controlling your motor’s speed. Therefore, when a jumper is present on these pins, your motor will become enabled and will be able to operate at its maximum speed.
However, when the jumper is removed, and the pin is then connected to the PWM input, you will be able to have control over your motor’s speed. When you connect the pin to the Ground, the motor will become disabled.
Using L298n with Arduino
Now you can try using the 298n motor driver with an Arduino. You can use a potentiometer when controlling the speed of your meter and then use a pushbutton to change the rotation direction. Therefore, you will require an L298n motor driver, DC motor, pushbutton, potentiometer, and Arduino board.
The first thing you have to do is define the pins along with the essential variable required for this particular program. You will have to set your pin modes along with an initial direction of your motor.
When it comes to the loop section, you start by first reading the value displayed in the potentiometer and map it from 0 to 1023 to the value you receive from your PWM signal from 0 to 255.
Once that is done, you will proceed and use the analogWrite () function, this allows you to send the PWM signal to enable pin of your L298n board in order to drive your motor.h You will then check if you have your pushbutton pressed and if so, you will go ahead and change the direction of your motor by setting input 1 along with input 2 inversely. Your push button will operate as a toggle button; every time you press it, the motor’s direction will change.
With the right coding, setting your L298n with the Arduino board is relatively easy. By following this step, you will be able to connect your L298n motor driver with the utmost ease and use it without any problem.
Final Word
Using an L298n motor driver with an Arduino is relatively easy. By adhering to the tips discussed in this article, you will come to find out that the whole process is straightforward. As we conclude, we hope that you have found this article helpful.