The L298N is a popular integrated circuit that allows you to control two DC motors with your Arduino. This tutorial will guide you through connecting and using the L298N to control the direction and speed of your motors.
Let’s dive into how the L298N motor driver works:
H-Bridge Configuration:
- The L298N is an integrated circuit (IC) that acts as an H-bridge. An H-bridge allows you to control the direction of current flow through a load (such as a motor) by switching the polarity.
- The L298N has two H-bridges, which means it can control two DC motors independently.
- Each H-bridge consists of four transistors (usually MOSFETs or bipolar transistors) arranged in a bridge configuration. These transistors allow current to flow in either direction through the load.
Motor Control Pins:
- ENA and ENB: Enable pins for motor A and motor B. You can use PWM signals on these pins to control the motor speed.
- IN1, IN2, IN3, and IN4: Input pins that determine the motor direction. By setting the appropriate combination of these pins, you can make the motor move forward, backward, or stop.
- OUT1, OUT2, OUT3, and OUT4: Output pins connected to the motor terminals.
.
Motor Speed Control:
- To control the motor speed, you apply a PWM signal to the ENA and ENB pins. The higher the PWM duty cycle, the faster the motor spins.
- The L298N adjusts the average voltage applied to the motor based on the PWM signal. It rapidly switches the motor voltage on and off to achieve the desired speed.
Motor Direction Control:
-
- To change the motor direction, you set the IN1, IN2, IN3, and IN4 pins accordingly:
- For motor A:
- IN1 = HIGH, IN2 = LOW: Motor A moves forward.
- IN1 = LOW, IN2 = HIGH: Motor A moves backward.
- Both LOW or both HIGH: Motor A stops.
- Similar logic applies to motor B using IN3 and IN4 pins.
- For motor A:
- To change the motor direction, you set the IN1, IN2, IN3, and IN4 pins accordingly:
Power Supply:
- The L298N requires an external power supply (usually 7V – 12V) to drive the motors.
- Connect the power supply to the +12V and GND pins on the L298N.
Current Limitations:
- The L298N can handle currents up to 2A per channel (motor A and motor B).
- If your motors draw more current, consider using an external motor driver with higher current capacity.
Components
Arduino Uno
Breadboard
L298n Motor Driver
Cables
Motors 2Pcs
Connection diagram
The code!
// Define motor pins const int motor1PWM = 6; // ENA const int motor1IN1 = 5; const int motor1IN2 = 4; const int motor2PWM = 9; // ENB const int motor2IN3 = 3; const int motor2IN4 = 2; void setup() { pinMode(motor1PWM, OUTPUT); pinMode(motor1IN1, OUTPUT); pinMode(motor1IN2, OUTPUT); pinMode(motor2PWM, OUTPUT); pinMode(motor2IN3, OUTPUT); pinMode(motor2IN4, OUTPUT); } void loop() { // Move motor 1 forward digitalWrite(motor1IN1, HIGH); digitalWrite(motor1IN2, LOW); analogWrite(motor1PWM, 200); // Adjust speed (0-255) // Move motor 2 backward digitalWrite(motor2IN3, LOW); digitalWrite(motor2IN4, HIGH); analogWrite(motor2PWM, 150); // Adjust speed (0-255) delay(2000); // Run for 2 seconds // Stop both motors digitalWrite(motor1IN1, LOW); digitalWrite(motor1IN2, LOW); digitalWrite(motor2IN3, LOW); digitalWrite(motor2IN4, LOW); delay(1000); // Pause for 1 second }