Chapter 9 · Arduino Practicals and Programming Projects

Practical 6Connecting DC Motors to the L298N Motor Driver

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 terminalConnect to
OUT1, OUT2Motor A
OUT3, OUT4Motor B
12V (VS)Battery positive
GNDBattery negative and Arduino GND
ENAArduino pin 5 (remove the ENA jumper)
IN1Arduino pin 7
IN2Arduino pin 8
ENBArduino pin 6 (remove the ENB jumper)
IN3Arduino pin 9
IN4Arduino pin 10
5V-EN jumperKeep fitted if the battery is 12 V or less
Arduino UNOPin 5 (ENA)Pin 7 (IN1)Pin 8 (IN2)Pin 6 (ENB)Pin 9 (IN3)Pin 10 (IN4)GNDL298NENAIN1IN2ENBIN3IN4GNDOUT1/2Motor AOUT3/4Motor B12VGND5VABBattery 6–12 Vcommon ground
Practical 6 circuit

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.

Arduino C/C++
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.

ProblemFix
Motors do nothingCheck ENA/ENB wiring, battery charge, common ground, and that the motor supply is on 12V (VS)
One motor runs the wrong waySwap the two wires of that motor
Motors hum but do not turn at low PWMIncrease the PWM value; motors need a minimum voltage to start (often above about 80)
Arduino resets when motors startBattery too weak or shared with the Arduino; use fresh batteries and check wiring
L298N gets hotNormal at higher currents; keep the load within 2 A per channel

Viva questions

  1. Draw the H-bridge and explain how direction is reversed.
  2. Why is the ENA jumper removed?
  3. What is the purpose of the common ground?
  4. What does analogWrite(ENA, 128) do?
Show viva answers
  1. Two diagonal switch pairs alternate, reversing the current through the motor.
  2. So the PWM signal on ENA can control speed instead of always being enabled at full speed.
  3. It gives the logic signals a shared 0 V reference.
  4. It applies a 50% duty cycle, so the motor runs at roughly half speed.