How to Control a DC motor with Arduino and L298N Motor Driver

Author: Release time:2023-01-11 Source: Font: Big Middle Small View count:103

DC motors are the simplest motors encountered by beginners and hobbyists. The operation is very simple: just connect the two wires of the motor to the two terminals of the battery! The motor starts to rotate.


If you want to control the rotation speed of a simple DC motor, then there is a technique called PWM DC motor control. The pulse width modulation or PWM signal generated by this technique will allow us to control the average voltage delivered to the DC motor.


Speed Control of DC motors using PWM


Using the PWM technique, the average rate of the voltage applied to the DC motor is controlled by turning on and off a very high power supply. The frequency of this switching will be in the order of tens of kilohertz.


Now, the average voltage applied to the DC motor will depend on the period of the so-called "load" PWM signal. The duty cycle of a PWM signal is simply the ratio of the time the signal is ON or HIGH to the total time of the signal.


The duty cycle is usually expressed as a percentage, and the figure below shows the different PWM signals for 12V power supply with different duty cycles of 0%, 25%, 50%, 75% and 100% respectively.



How to Control a DC motor with Arduino and L298N Motor Driver


Now that we have controlled the average voltage supplied to the DC motor, how do we apply this motor voltage? Here is the use of transistors.


A PWM signal from any source, such as the Arduino in this example, can be supplied to the gate of the MOSFET, depending on the duty cycle.


The diagram below shows a simple circuit diagram where the PWM output of the Arduino is given to the MOSFET and the 12V DC motor is connected through the MOSFET.


arduino pwm.jpg



The code for this circuit is shown below. Using this code, the Arduino will change the speed of the DC motor in a decaying manner, i.e. gradually increasing the speed to a peak and then gradually decreasing the speed to stop.


This circuit is suitable for controlling the motor speed, but is not an effective way to change the direction of rotation. In order to change the direction of rotation without changing the motor leads, you need to use a special circuit called an H-bridge.


DC Motor Control with H-Bridge


The H-Bridge is a simple electronic circuit consisting of four switching elements such as transistors (BJT or MOSFET) that can drive a motor in two directions without switching leads.


The name "H-Bridge" refers to the appearance of a connection consisting of four transistors and a motor forming the center of the letter "H".


A H-Bridge connection using four transistors and a motor is shown below. By activating two specific transistors at the same time, we can control the current through the motor and thus the direction of rotation.


h bridge.jpg



The two control inputs A and B in the above circuit will determine the direction of rotation of the motor. If A is low and B is high, transistors Q1 and Q4 will conduct and allow current to flow through the motor in a specific direction.


If control input A is when HIGH is high and B is low, transistors Q2 and Q3 will conduct and the current through the motor will be reversed, thus reversing the direction of rotation.


Combining two functions, PWM technology for speed control and H-bridge connection for directional control, you can have full control of the DC motor.


An effective H-bridge connection using transistors is very tedious. For this reason, there are dedicated H-bridge motor driver ICs on the market, two common ones being the L293D and L298N.


We have seen how to control the speed in earlier projects using the L293D for DC motors. In this project we will focus on the more advanced L298N motor driver and look at Arduino DC motor control using PWM techniques using the L298N motor driver.


Simultaneous reading of DC motor control using ARDUINO and L293D


A brief description of the L298N motor driver


The L298N motor driver IC is a 15-pin high voltage, high current motor driver IC with two full bridge drivers. The logic level of the L298N IC is compatible with standard TTL and the IC can be used to drive different inductive loads such as DC motors, stepper motors, relays, etc.


The following figure shows the pinout of the L298N IC in a multi-watt package (multi-lead power package).


 l298n pinout.jpg


Since the L298N motor driver IC is a dual full-bridge driver IC, you can control two motors simultaneously with separate inputs. The logic supply voltage is 5 V, but the motor supply voltage can be as high as 45 V. The peak output current per channel is 2A.


Typically, the L298N driver can be used as a module containing all the necessary components and connectors for controlling two DC motors. One such module is shown below. I will explain some of the important components in this module.


The L298N motor driver module consists of two 2-pin screw terminals for connecting two motors. It also has six pin male headers for connecting two enable inputs and four input pins (two for each motor).


There is a 3-pin screw terminal through which you need to supply power to the motors. If the motor used is rated at 12V or lower, 12V power is provided through this screw terminal and the on-board 5V regulator will provide 5V logic power to the L298N IC.


 l298n ic.jpg


You can also access this regulated 5V via the third pin in the 3-pin screw terminal block. the jumper provided near the 3-pin screw terminal must be turned on for supply voltages up to 12V, as this jumper will enable the on-board regulator. The 5V output on the third pin of the 3-pin screw terminal can only be used when the jumper is on, i.e., when the supply voltage is 12V. This 5V output can be used to power the Arduino board.


If the supply voltage is higher than 12V, remove the jumper, as it may damage the regulator. In this case, the 5V logic power of the L298N IC is given through the third pin of the 3-pin screw terminal.


Arduino DC motor control using L298N


We will now see a simple circuit where we use the Arduino and the L298N IC to control the speed and direction of a DC motor. You will need to add a few other components for this project, the full list is shown below. In the Arduino DC motor control using L298N project, we will use a combination of PWM signals and L298N (H-bridge) to control the functions of a simple DC motor, i.e. speed and direction of rotation.


Circuit diagram


 arduino dc motor control using  l298n circuit diagram.jpg


Required components


  • Arduino UNO

  • L298N motor driver module

  • 12V DC motor

  • 100KΩ potentiometer

  • Button

  • 12V Power Supply

  • Breadboard

  • Connection cable

  • Code




Using the L298N motor driver project for Arduino DC motor control can be the beginning step for many advanced projects.


Almost all robots have wheels and we need to control the motors connected to these wheels. Therefore, any Arduino based robot can use the L298N to implement this type of motor control.


Some of the robotics applications for L298N motor drivers are gesture controlled robots, wire follower robots, obstacle avoidance robots, etc.

Hot News