Introduction: Connect L298N IC to Arduino

I recently got a L298N IC and I was searching the internet for connecting it with an arduino, but I found no tutorials that worked. I tried many options and finally found it. I hope you enjoy reading this!

Supplies

You will need the following components −

  • 1 × L298 bridge IC
  • 1 × DC motor
  • 1 × Arduino UNO
  • 1 × breadboard
  • 6 × jumper wires
  • 1 × 9v or more battery

Step 1: Fit the IC in the Breadboard

At first when you try fitting the IC to the breadboard it doesn't fit. As the pins are flexible you can adjust them to fit into the breadboard.

Step 2: Connections

Connection Steps:

1. Connect Logic supply voltage and the ground of the IC to 5V and the ground of Arduino, respectively.

2. Connect the motor to pins 2 and 3 of the IC.

3. Connect IN1 of the IC to pin 8 of Arduino.

4. Connect IN2 of the IC to pin 9 of Arduino.

5. Connect ENA of IC to pin 2 of Arduino.

6. Connect SENS A pin of IC to the ground.

7. Connect the 9V battery's positive to Supply voltage and the battery's negative with the ground you connected with the Arduino.

Step 3: Program the Arduino

The code for the Arduino is :

const int pwm = 2 ; //initializing pin 2 as pwm
const int in_1 = 8 ;
const int in_2 = 9 ;
//For providing logic to L298 IC to choose the direction of the DC motor


void setup() {
   pinMode(pwm,OUTPUT) ; //we have to set PWM pin as output
   pinMode(in_1,OUTPUT) ; //Logic pins are also set as output
   pinMode(in_2,OUTPUT) ;
}


void loop() {
   //For Clock wise motion , in_1 = High , in_2 = Low
   digitalWrite(in_1,HIGH) ;
   digitalWrite(in_2,LOW) ;
   analogWrite(pwm,255) ;
   /* setting pwm of the motor to 255 we can change the speed of rotation
   by changing pwm input but we are only using arduino so we are using highest
   value to driver the motor */
   //Clockwise for 3 secs
   delay(3000) ;
   //For brake
   digitalWrite(in_1,HIGH) ;
   digitalWrite(in_2,HIGH) ;
   delay(1000) ;
   //For Anti Clock-wise motion - IN_1 = LOW , IN_2 = HIGH
   digitalWrite(in_1,LOW) ;
   digitalWrite(in_2,HIGH) ;
   delay(3000) ;
   //For brake
   digitalWrite(in_1,HIGH) ;
   digitalWrite(in_2,HIGH) ;
   delay(1000) ;
}

Step 4: Result.

The motor will run first in the clockwise (CW) direction for 3 seconds and then counter-clockwise (CCW) for 3 seconds.