The L298N motor driver is a versatile dual H-bridge motor driver integrated circuit (IC) capable of controlling the speed and direction of two DC motors or one stepper motor. It is widely used in robotics, automation projects, and various applications where precise motor control is required.
Pin Number | Pin Name | Description |
---|---|---|
1 | OUT1 | Motor A output 1 |
2 | OUT2 | Motor A output 2 |
3 | VS | Motor power supply (up to 35V) |
4 | GND | Ground |
5 | OUT3 | Motor B output 1 |
6 | OUT4 | Motor B output 2 |
7 | VSS | Logic power supply (5V) |
8 | ENA | Enable motor A / PWM speed control for motor A |
9 | IN1 | Input 1 for motor A direction control |
10 | IN2 | Input 2 for motor A direction control |
11 | IN3 | Input 3 for motor B direction control |
12 | IN4 | Input 4 for motor B direction control |
13 | ENB | Enable motor B / PWM speed control for motor B |
14 | CS | Current sensing output (optional) |
// Define the L298N control pins
const int enA = 9;
const int in1 = 8;
const int in2 = 7;
const int enB = 3;
const int in3 = 5;
const int in4 = 4;
void setup() {
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
// Turn on motor A & B
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Set speed to 200 out of possible range 0-255
analogWrite(enA, 200);
analogWrite(enB, 200);
// Wait 2 seconds
delay(2000);
// Change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Wait 2 seconds
delay(2000);
// Turn off motors
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
}
Q: Can I control a stepper motor with the L298N? A: Yes, the L298N can control a bipolar stepper motor by using the two H-bridges to control the two coils.
Q: What is the maximum current the L298N can handle? A: The L298N can handle up to 2A per channel continuously, with peak currents of 3A.
Q: Do I need to use external diodes with the L298N? A: The L298N has built-in diodes for back EMF protection. However, additional external diodes may be used for extra protection.
Q: How do I use the current sensing feature? A: The CS pin can be connected to an analog input on a microcontroller to measure the voltage proportional to the motor current.