Aim. To drive two DC motors forward, reverse and stop, and to change their speed, using an L298N driver module controlled by Arduino.
Components. Arduino UNO R3, L298N module, 2 DC gear motors (3 to 6 V or 6 to 12 V), battery pack (for example 4 × AA = 6 V, or 2 × 18650 = 7.4 V), jumper wires, breadboard (optional).
Concepts used. H-bridge, digital direction control, PWM speed control, common ground, separate motor supply.
Wiring.
| L298N terminal | Connect to |
|---|---|
| OUT1, OUT2 | Motor A |
| OUT3, OUT4 | Motor B |
| 12V (VS) | Battery positive |
| GND | Battery negative and Arduino GND |
| ENA | Arduino pin 5 (remove the ENA jumper) |
| IN1 | Arduino pin 7 |
| IN2 | Arduino pin 8 |
| ENB | Arduino pin 6 (remove the ENB jumper) |
| IN3 | Arduino pin 9 |
| IN4 | Arduino pin 10 |
| 5V-EN jumper | Keep fitted if the battery is 12 V or less |
Power the Arduino from USB (or its DC jack). Do not connect the L298N 5V pin to the Arduino 5V pin if the Arduino is powered through USB at the same time; they are not needed together.
Program.
const int ENA = 5, IN1 = 7, IN2 = 8; // Motor A
const int ENB = 6, IN3 = 9, IN4 = 10; // Motor B
void setup() {
pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
stopMotors();
}
void motorA(int speed, bool forward) {
digitalWrite(IN1, forward ? HIGH : LOW);
digitalWrite(IN2, forward ? LOW : HIGH);
analogWrite(ENA, speed); // 0..255
}
void motorB(int speed, bool forward) {
digitalWrite(IN3, forward ? HIGH : LOW);
digitalWrite(IN4, forward ? LOW : HIGH);
analogWrite(ENB, speed);
}
void stopMotors() {
digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
analogWrite(ENA, 0); analogWrite(ENB, 0);
}
void loop() {
motorA(200, true); motorB(200, true); // both forward, ~78% speed
delay(2000);
stopMotors(); delay(1000);
motorA(200, false); motorB(200, false); // both reverse
delay(2000);
stopMotors(); delay(1000);
for (int s = 0; s <= 255; s += 5) { // speed ramp up
motorA(s, true); motorB(s, true);
delay(40);
}
stopMotors(); delay(2000);
}forward ? HIGH : LOW is the conditional operator: if forward is true the value is HIGH, otherwise LOW.
Expected result. Both motors turn forward for 2 s, stop, turn in reverse for 2 s, stop, then gradually speed up.
Troubleshooting.
| Problem | Fix |
|---|---|
| Motors do nothing | Check ENA/ENB wiring, battery charge, common ground, and that the motor supply is on 12V (VS) |
| One motor runs the wrong way | Swap the two wires of that motor |
| Motors hum but do not turn at low PWM | Increase the PWM value; motors need a minimum voltage to start (often above about 80) |
| Arduino resets when motors start | Battery too weak or shared with the Arduino; use fresh batteries and check wiring |
| L298N gets hot | Normal at higher currents; keep the load within 2 A per channel |
Viva questions
- Draw the H-bridge and explain how direction is reversed.
- Why is the ENA jumper removed?
- What is the purpose of the common ground?
- What does
analogWrite(ENA, 128)do?
Show viva answers
- Two diagonal switch pairs alternate, reversing the current through the motor.
- So the PWM signal on ENA can control speed instead of always being enabled at full speed.
- It gives the logic signals a shared 0 V reference.
- It applies a 50% duty cycle, so the motor runs at roughly half speed.