Built-in project · Arduino Uno R3

Robot car drive on an Arduino Uno

Two gearbox motors on an L298N module from six Uno pins: forward, a turn, a spin on the spot and reverse, with the speed on ENA and ENB. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

IntermediateRuns in your browser. Free, and no account needed.

Robot car drive · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Robot car drive: two motors on an L298N, the way every robot car does it.
//
//   pin 5  ENA, the left motor's speed (the ENA jumper is off)
//   pin 10 ENB, the right motor's speed (the ENB jumper is off)
//   pin 6  IN1   pin 7  IN2     the left motor's direction
//   pin 8  IN3   pin 9  IN4     the right motor's direction
//   L298N  12V from the battery (7.4 V here), GND to the Uno's GND,
//          5V left alone: the regulator jumper is on and it makes its own
//
// A car has no steering. It turns by running one side slower than the other,
// and it spins on the spot by running the two sides opposite ways. So the
// whole of driving is four numbers: a direction and a speed for each side.
// drive() takes them as two signed speeds, -255 to 255.
//
// # The volts the bridge keeps
//
// The L298 is a bipolar chip and keeps about 2.5 V for itself: 1.35 V in the
// high side and 1.2 V in the low side. A 7.4 V pack leaves the motors under
// 5 V, which is why the car is slower than the motors' label says. And the
// regulator on the module needs 2 V of headroom to make its 5 V, which is why
// a 6 V pack of four AA cells runs nothing at all with the jumper fitted.
//
// # Why the ENA and ENB jumpers are off
//
// Fitted, a jumper ties the enable to 5 V and the motor only has full speed.
// Pulled off, the enable is a pin, and analogWrite on it is the throttle.

const int ENA = 5;
const int IN1 = 6;
const int IN2 = 7;
const int ENB = 10;
const int IN3 = 8;
const int IN4 = 9;

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: the motor's own EMF is shorted out.
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    speed = 255;
  }
  analogWrite(en, speed > 255 ? 255 : speed);
}

void drive(const char *what, int left, int right, unsigned long ms) {
  Serial.print("car: ");
  Serial.println(what);
  side(ENA, IN1, IN2, left);
  side(ENB, IN3, IN4, right);
  delay(ms);
}

void setup() {
  const int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4};
  for (int i = 0; i < 6; i++) {
    pinMode(pins[i], OUTPUT);
    digitalWrite(pins[i], LOW);
  }
  Serial.begin(115200);
  Serial.println("Mokxi Uno: a robot car on an L298N");
}

void loop() {
  drive("forward", 255, 255, 2000);
  drive("gentle left: the left side slower", 110, 255, 1500);
  drive("spin right on the spot", 200, -200, 1500);
  drive("stop", 0, 0, 700);
  drive("reverse", -255, -255, 2000);
  drive("stop", 0, 0, 1000);
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

13 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND
  • Arduino Uno R3 pin 10; Motor driver module, L298N pin ENB
  • Arduino Uno R3 pin 9; Motor driver module, L298N pin IN4
  • Arduino Uno R3 pin 8; Motor driver module, L298N pin IN3
  • Arduino Uno R3 pin 7; Motor driver module, L298N pin IN2
  • Arduino Uno R3 pin 6; Motor driver module, L298N pin IN1
  • Arduino Uno R3 pin 5; Motor driver module, L298N pin ENA
  • Motor driver module, L298N pin OUT1; DC motor (1) pin 2
  • Motor driver module, L298N pin OUT2; DC motor (1) pin 1
  • 7.4 V: Motor driver module, L298N pin 12V
  • Ground: Motor driver module, L298N pin GND
  • Motor driver module, L298N pin OUT3; DC motor (2) pin 1
  • Motor driver module, L298N pin OUT4; DC motor (2) pin 2

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.