Controlling DC motors with a L298n dual h bridge motor controller

In this blog I will discuss how to control a DC motor with a L298n Motor controller. A L298n motor controller can be used to control the direction of a motor and also the speed of a DC motor.

Materials required

  • An arduino uno,
  • Few male to male jumper wires,
  • Few male to female jumper wires,
  • A DC motor(in this project i used a 100rpm ,12 V motor),
  • A l298n motor controller,
  • A 9V battery ,
  • A Snap connector

Making the Arduino connections


  • Connect the In1 and In2 pins to pins to pins 7 and 8 on the Arduino.
  • Connnect the ENA pin to pin number 5 on the Arduino.
  • Connect the motor to the OUT1 and OUT2 pins located on the side of the motor controller
  • Connect the 12V pin to the + terminal of the battery using the snap connector
  • Connect the GND pin of the motor controller to the -terminal of the battery and the GND of the Arduino
  • Connect the 5v pin on the motor controller to the Vin pin on the Arduino.

The code

The code for this project is pretty simple. if we set in1 to HIGH and in2 to LOW the motor will spin in d anticlockwise direction and if we set in2 to LOW and in1 to HIGH , the motor will spin in the clockwise direction.


you can copy the code from here

int ena = 5;
int in1 = 7;
int in2 = 8;
int Speed = 1024;

void setup(){
  pinMode(in1,OUTPUT);
  pinMode(in2,OUTPUT);
  pinMode(ena,OUTPUT);
}

void loop(){
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
//Set in1 to LOW amd in2 to HIGH to make the motor spin in the opposite direction
 analogWrite(ena,Speed);  
}


All the best..!! see you back soon..!!

Comments