The L298 Dual H Bridge Motor Speed Controller is an integrated circuit designed to control the direction and speed of DC motors. It is capable of driving two motors simultaneously in either direction, which makes it a popular choice for robotics, automation projects, and any application requiring bidirectional control of motors with speed variation.
Pin Number | Pin Name | Description |
---|---|---|
1 | Vs | Power supply for the motors (up to 46V) |
2 | GND | Ground |
3 | Vss | Logic supply voltage (5V from Arduino) |
4 | Out 1 | Output to motor A terminal 1 |
5 | Out 2 | Output to motor A terminal 2 |
6 | Out 3 | Output to motor B terminal 1 |
7 | Out 4 | Output to motor B terminal 2 |
8 | EnA | Enable motor A, PWM input for speed control |
9 | In1 | Input 1 for motor A direction control |
10 | In2 | Input 2 for motor A direction control |
11 | In3 | Input 1 for motor B direction control |
12 | In4 | Input 2 for motor B direction control |
13 | EnB | Enable motor B, PWM input for speed control |
14 | GND | Ground |
15 | GND | Ground |
16 | GND | Ground |
// 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);
delay(2000); // Run motors for 2 seconds
// Now change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000); // Run motors in the opposite direction for 2 seconds
// Turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
Q: Can the L298 drive stepper motors? A: Yes, the L298 can be used to drive stepper motors with the correct stepping sequence.
Q: What is the maximum current the L298 can handle? A: The L298 can handle up to 2A per channel, but it's recommended to stay below the peak current for continuous operation.
Q: Do I need to use external diodes with the L298? A: The L298 has built-in diodes for back EMF protection, but adding external diodes can provide additional protection.
Q: How do I control the speed of the motors? A: Speed control is achieved by applying PWM signals to the EnA and EnB pins. Adjusting the duty cycle of the PWM signal will vary the speed of the motors.