Learn

Two motors on an L298N, the way every robot car does it

Intermediate

Or see every lesson.

  • 261parts on the bench
  • 31boards running now
  • 1.00xreal time, on every board
Arduino Uno: robot car drive on an L298Nlive0.000 s 0.00x
Click to open it in the editor
The Robot car drive example on a simulated ATmega328P: forward, a turn, a spin and reverse, from a 7.4 V pack.

The L298N is the red board with the black heat sink that drives the wheels of nearly every Arduino robot car. It can run two DC motors, each forward or backward, at a speed you set with PWM. Most problems with it come from one of its three little jumpers, so this page spends time on those.

The circuit above is an Arduino Uno driving two gearbox motors through an L298N, from a 7.4 V battery pack. The sketch drives forward, turns gently left, spins on the spot, stops and backs up, and prints each move. It runs in your browser on the sketch below.

What you need

  • An Arduino Uno
  • An L298N motor driver module
  • Two DC gear motors (the yellow TT kind)
  • A battery pack of 7 V or more, such as a two-cell 7.4 V pack
  • Jumper wires

Wiring

Part and pinGoes toNote
L298N ENAPin 5Left motor speed, PWM; pull the ENA jumper off first
L298N IN1Pin 6Left motor direction
L298N IN2Pin 7Left motor direction
L298N IN3Pin 8Right motor direction
L298N IN4Pin 9Right motor direction
L298N ENBPin 10Right motor speed, PWM; pull the ENB jumper off first
L298N 12VBattery +The motor supply, 7.4 V here
L298N GNDBattery - and Uno GNDAll grounds must be joined
OUT1 and OUT2Left motorSwap these two if the wheel turns the wrong way
OUT3 and OUT4Right motorSwap these two if the wheel turns the wrong way

Direction on IN, speed on EN

Each motor has two direction pins and one enable pin. For the left motor, IN1 HIGH and IN2 LOW is forward. IN1 LOW and IN2 HIGH is reverse. Both the same is a brake. ENA is the throttle: analogWrite(ENA, 255) is full speed, and a smaller number is slower.

The sketch wraps that in one helper, side(), that takes a speed from -255 to 255. Then drive() calls it for both sides. A car has no steering. It turns by running one side slower, and it spins on the spot by running the two sides opposite ways.

One side of the car, from the Robot car drive example
void side(int en, int a, int b, int speed) {
  if (speed > 0) {
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
  } else if (speed < 0) {
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    speed = -speed;
  } else {
    // Both inputs low is a brake.
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    speed = 255;
  }
  analogWrite(en, speed > 255 ? 255 : speed);
}

The three jumpers

The ENA and ENB jumpers tie each enable pin to 5 V. With a jumper on, that motor only runs at full speed and analogWrite does nothing. To control speed, pull the jumper off and wire ENA or ENB to a PWM pin. On an Uno those are pins 3, 5, 6, 9, 10 and 11.

The third jumper turns on the board’s 5 V regulator. With it on, the board makes its own 5 V from the motor supply, and the 5V terminal is an output. That regulator needs about 2 V of headroom, so the motor supply must be 7 V or more. With a 6 V pack of four AA cells and the jumper on, nothing turns at all. The sketch’s own notes say so, and the model agrees.

Why your motors run slower than the label

The L298 chip is an old bipolar design, and it keeps about 2.5 V for itself. On the circuit above, the 7.4 V pack leaves each motor with about 4.8 V at full speed. That is why a car on an L298N is slower than its motors’ rating suggests.

If you need more speed from the same battery, a newer MOSFET driver such as the TB6612 or the DRV8833 wastes much less. Both are in the Mokxi parts bin, with their own robot car builds.

Try it in the editor

Change 110 in the gentle left turn to 0 and the car pivots on its left wheel instead. Change the delays to make a square.

Click the L298N and turn ena_jumper back on in the properties panel. Now the left motor ignores its speed and only runs flat out.

For a car that drives itself, open the obstacle-avoiding robot tutorial. It is the same board and the same pins, with an ultrasonic sensor on the front.

Common mistakes

No shared ground. The Arduino’s GND must connect to the L298N’s GND, or the inputs have nothing to be HIGH against.

Leaving the ENA and ENB jumpers on and wondering why speed control does nothing.

A 6 V battery with the regulator jumper on. The board cannot make its own 5 V, and the motors never move.

One wheel runs backward. The two motors are mounted mirror image on a chassis. Swap that motor’s two wires.

Powering motors from the Arduino’s 5 V pin. Motors draw far more than the board can give. Use a separate battery.

Questions

Do I remove the jumpers on the L298N?

Remove the ENA and ENB jumpers if you want speed control with analogWrite. Keep the 5 V regulator jumper on if your motor supply is 7 V or more and you want the board to make its own 5 V.

Why is my L298N motor not turning?

Check, in order: a shared ground, a motor supply of 7 V or more with the regulator jumper on, the enable pins (jumper on or a pin driven HIGH), and IN1 and IN2 set to different levels.

Can an L298N drive a stepper motor?

Yes, one bipolar stepper in full steps, using both channels. For smooth microstepping, an A4988 or DRV8825 driver is the better choice.

What does Mokxi not model on the L298N?

Current and heat. There is no current limit, nothing gets hot and there is no thermal shutdown. The voltage the chip keeps and the regulator’s headroom are modeled.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.