Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
120 min
Share

Control two motors with Arduino, using L298N motor driver

To control a DC motor with an Arduino, it’s important to know that you will need an intermediary between the Arduino and the motor, such as a motor driver. In this tutorial, we’ll learn how to use the popular motor driver L298N, which is commonly used for controlling DC motors.

Project Video

Overview

In this tutorial, we will learn how to use the L298N motor driver with the Arduino, and we will program the Arduino to turn the motor on and off, as well as change its direction and speed of rotation. We will program the Arduino to do this without using any external control accessories.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
2Amp 7V-30V L298N Motor Driver / Stepper Driver (2 Channels)
Get Item
775 Brushed DC Motor (36V 9000 RPM)
Get Item
2×18650 Battery Holder
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the L298N motor driver and the Arduino as shown in the image below.


Connections from the l298n motor driver to arduino :



      • ENABLE A pin→ Arduino pin 10

      • Input1 pin→ Arduino pin 9

      • Input2 pin→ Arduino pin 8

      • Input3 pin→ Arduino pin 7

      • Input4 pin→ Arduino pin 6

      • ENABLE B pin→ Arduino pin 5



Connections from 18650 battery holder to the l298n motor driver:



     • positive wire of the 18650 battery holder →  VS terminal

    • negative wire of the 18650 battery holder → Gnd terminal + Arduino GND pin



Connections from the motor 1 to the l298n motor driver:



     • Motor 1 first wire  →  Out1

     • Motor 1 second wire → Out2  


Connections from the motor 2 to the l298n motor driver:



     • Motor 2 first wire  →  Out3

    • Motor 2 second wire → Out4


Coding


/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial: https://learn.voltaat.com/2023/03/30/control-two-motors-with-arduino-using-l298n-motor-driver/

Tutorial: Control two 775 DC motors with Arduino, using L298N motor driver!

The purpose of this sketch is to control a dc motor to Turn on and off
and change the rotation speed of two 775 dc motors.

Connections from the l298n motor driver to arduino :

• ENABLE A pin→ Arduino pin 10

• Input1 pin→ Arduino pin 9

• Input2 pin→ Arduino pin 8

• Input3 pin→ Arduino pin 7

• Input4 pin→ Arduino pin 6

• ENABLE B pin→ Arduino pin 5

Connections from 18650 battery holder to the l298n motor driver:

• positive wire of the 18650 battery holder →  VS terminal

• negative wire of the 18650 battery holder → Gnd terminal + Arduino GND pin

Connections from the motor 1 to the l298n motor driver:

• Motor 1 first wire  →  Out1

• Motor 1 second wire → Out2  

Connections from the motor 2 to the l298n motor driver:

• Motor 2 first wire  →  Out3

• Motor 2 second wire → Out4

*/

//Initially, we will name the Arduino pins that we will use:

#define enA 10
#define in1 9
#define in2 8
#define in3 7
#define in4 6
#define enB 5


void setup() {
//setting the Arduino pins to work as an output or an input:
 pinMode(enA, OUTPUT);
 pinMode(in1, OUTPUT);
 pinMode(in2, OUTPUT);
 pinMode(in3, OUTPUT);
 pinMode(in4, OUTPUT);
 pinMode(enB, OUTPUT);

// Set initial rotation direction:
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);
 digitalWrite(in3, LOW);
 digitalWrite(in4, LOW);

}

void loop() {
/*In the first three seconds, we will make the first motor spin while the other one will stop */

   analogWrite(enA, 100); // Send PWM signal to L298N Enable pin
   analogWrite(enB, 100); // Send PWM signal to L298N Enable pin
   digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);
   digitalWrite(in3, LOW);
   digitalWrite(in4, LOW);
   delay(3000);

/* While in the next three seconds, the second motor will spin, while the first will stop*/

   digitalWrite(in1, LOW);
   digitalWrite(in2, LOW);
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
   delay(3000);
 
/* Then the two motors will rotate together in the same direction for three seconds at a speed specified in the code(PWM=100)*/

   digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
   delay(3000);
 

/* Then the two motors will rotate together in the opposite direction for a period of three seconds at a speed faster than the previous stage. It is specified in the code to be the maximum speed (PWM = 255)*/

   analogWrite(enA, 255); // Send PWM signal to L298N Enable A pin
   analogWrite(enB, 255); // Send PWM signal to L298N Enable B pin
   digitalWrite(in1, LOW);
   digitalWrite(in2, HIGH);
   digitalWrite(in3, LOW);
   digitalWrite(in4, HIGH);
   delay(3000);

}

Testing it Out

Once you’ve uploaded the code to the Arduino board, In the first three seconds, you will find that the first motor is spinning while the other is stopped, as shown on the right GIF:

While in the next three seconds, the second motor will spin, while the first will stop:

Then the two motors will rotate together in the same direction for three seconds at a speed specified in the code(PWM=150):

Then the two motors will rotate together in the opposite direction for a period of three seconds at a speed faster than the previous stage. It was specified in the code to be the maximum speed (PWM = 255):

If you want to control a DC motor with Arduino, you must know that you cannot connect this motor to one of the Arduino pins directly because it produces a small current that cannot drive a motor, so you will need to an intermediary between Arduino and the motor, and this intermediary is the motor driver. In this tutorial, we will learn how to use the famous motor driver L298N, which is most used in controlling dc motors.

Resources

No items found.