The L298N Motor Driver is a controller that uses an H-Bridge to easily control the direction and speed of up to 2 DC motors. This tutorial will show you how to use it.
Video InstructionsText InstructionsHardware:
1.) The controller can take up to 2 motors. Plug one motor into the terminal labelled OUT1 and OUT2. Plug the second motor into the terminal labelled OUT3 and OUT4:
2.) The row of pins on the bottom right of the L298N control the speed and direction of the motors. IN1 and IN2 control the direction of the motor connected to OUT1 and OUT2. IN3 and IN4 control the direction of the motor connected to OUT3 and OUT4. Here I plugged them into pins 2, 3, 4, and 5 on the Arduino.
3.) You can power the L298N with up to 12V by plugging your power source into the pin on the L298N labelled "12V". The pin labelled "5V" is a 5V output that you can use to power your Arduino. Here are some ways to wire it depending on your use case:
Powering Arduino with L298N
Powering L298N and Arduino Separately:
Powering L298N with Arduino:
Software:
4.) Write the code. Setting IN1 to HIGH and IN2 to LOW will cause the left motor to turn a direction. Setting IN1 to LOW and IN2 to HIGH will cause the left motor to spin the other direction. The same applies to IN3 and IN4. Here is a short example (You can download the full code in the "Code" section at the bottom of the page):
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
Speed:
5.) You can change the speed with the EN pins using PWM. ENA controls the speed of the left motor and ENB controls the speed of the right motor. Here I plugged them into pins 9 and 10 on the Arduino. This is optional and the motors will still run if you don't do this.
6.) To change the speed in the code, use the analogWrite() function (more info) on the ENA and ENB pins. Here is an example (You can download the full code in the "Code" section at the bottom of the page):
analogWrite(ENA_pin, 50);
Comments