Here we will show how to use L298N motor driver with Arduino to drive a DC motor. Also provided is the L298N motor driver Schematic with Arduino and Program code or sketch for Arduino.
L298N is a dual full bridge driver that can be used to drive inductive loads such as motors, relay and solenoids. It can be used to drive two DC motors simultaneously and single stepper motor. Another similar motor driver is L293D but L298N is a high current and high voltage H-bridge driver meaning that L298N is suitable as motor driver to drive high voltage and high current motors.
Following picture shows L298N integrated chip.
The following picture shows pin-out of the L298N H bridge taken from L298N datasheet:
L298N Arduino Interfacing
The following picture shows schematic drawing of interfacing of L298N motor driver with Arduino for DC motor control is as shown below.
In the above schematic drawing, the inputs IN1(pin 5) and IN2(pin 7) of the L298N motor driver are connected to the pin 10 and pin 9 of Arduino respectively. By sending signal from the arduino on these L298N pins controls the direction of the motor rotation. The enable ENA pin of the L298N is connected to the digital pin 8 of the Arduino. By sending LOW from the Arduino we can switch off the first dual H-bridge of the L298N and by sending HIGH from Arduino we can turn on the first dual H-bridge of the L298N. The OUT1 and OUT2 of the L298N are connected to two wires of the DC motor. Four 1N4004(or use 1N4001) diodes are connected as shown in the schematic diagram to avoid any unwanted reverse current flowing into the DC motors. The SenseA and SenseB are for current sensing which are simply connected to ground in this example tutorial. The Vss(or Vcc) pin of the L298N is connected to the +5V logic voltage(can be connected to +5V of the Arduino). The Vs pin of the L298N is connected to the +5V supply which is used to drive the DC motor. This Vs pin should be connected to the higher voltage source in the above L298N motor driver Schematic if higher rated DC motor is used. Here we have used only +5V to drive simple DC motor.
The following picture shows the connected L298N, Arduino Uno, diodes and DC motor. Thus in this example, a single DC motor is driven using only one of the two dual H bridge motor driver unit contained in L298N.
L298N Arduino program code
The following program code is a simple example code to drive a DC motor using L298N with Arduino. In the code, the motor is rotated in one direction for about 3 seconds, and stopped for 3 seconds and then rotated in the other direction for 3 seconds. Then the process is repeated.
//Program for DC motor control using L298N motor driver & Arduino
//source: https://ee-diary.blogspot.com
void forward(){
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
}
void reverse(){
digitalWrite(8,HIGH);
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
}
void stop(){
digitalWrite(8, LOW);
}
void userDelay(){
int k;
for(k=0; k<=3; k++){
delay(1000);
}
k=0;
}
void setup () {
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
digitalWrite(8,LOW);
}
void loop() {
forward();
userDelay();
stop();
userDelay();
reverse();
userDelay();
stop();
userDelay();
}
Video demonstration of L298N with Arduino that controls DC motor control is below.
In this tutorial we have shown how to use L298N motor driver with Arduino to drive a DC motor. The next tutorials Arduino L298N DC Motor Speed control with PWM and Speed and direction control of DC motor using Arduino Fast PWM illustrates how to use PWM with L298N for DC motor speed control.
For L293D motor driver IC tutorial see DC motor control using L293D Motor Shield and Arduino.