Board and part

Arduino Nano L298N Simulator: Wiring, Code and a Live Circuit

This is an L298N motor driver wired to an Arduino Nano, running a DC motor forward, stopping, and running it in reverse live in your browser, powered by its own 9 V supply.

  • 5 V logic
  • No library
  • IN1 2, IN2 3
  • 9 V motor supply
Arduino Nano · L298Nlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the L298N to the Arduino Nano

Two signal wires, pin 2 and pin 3, a shared ground, the motor supply on 12V and the motor on OUT1 and OUT2.

Every connection in the circuit above, with the board’s own pin names.

L298N pinArduino Nano pinWhy
IN1pin 2With IN2, sets the direction: IN1 high and IN2 low turns the motor one way.
IN2pin 3IN2 high and IN1 low turns it the other way; both low lets it coast.
GNDGNDThe board and the driver must share a ground, and so must the motor supply: all three grounds are one net.
12VThe 9 V motor supplyThe motor’s own supply, not the board’s: the driver passes it on to the motor.
OUT1, OUT2The motor’s two terminalsSwap the two wires and the motor turns the other way for the same signals.
ENA (jumper)Jumper left onWith the jumper on, channel A is always enabled. Remove it to give ENA a pin and set the speed with analogWrite().

Power

The Arduino Nano’s pins only drive the two inputs, a few milliamps; the motor’s current comes from its own supply on the 12V terminal. The L298N reads 2.3 V and up as high, so 5 V signals work. The bridge drops about 2 V, so a 9 V supply gives the motor about 7 V. Ground must be shared by the board, the driver and the supply.

Pins to leave alone on the Arduino Nano

A6 and A7 are analog only: pinMode(A6, OUTPUT) does nothing and digitalRead(A7) is not there to call. D0 and D1 are the USB serial port.

On a real bench

The Nano plugs into the breadboard itself, straddling the center channel, so the L298N goes in a few columns along and every connection is a short jumper. The silkscreen labels its pins D2 to D13 and A0 to A7; in code D2 is simply 2. A Nano clone with a CH340 USB chip needs that driver on a real computer.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

L298N forward and reverse on Arduino Nano pin 2, pin 3
const int IN1 = 2;   // pin 2
const int IN2 = 3;   // pin 3

void setup() {
  Serial.begin(9600);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
}

void loop() {
  digitalWrite(IN1, HIGH);   // IN1 high, IN2 low: forward
  digitalWrite(IN2, LOW);
  Serial.println("Forward");
  delay(1000);

  digitalWrite(IN1, LOW);    // both low: the motor coasts
  digitalWrite(IN2, LOW);
  Serial.println("Stop");
  delay(500);

  digitalWrite(IN1, LOW);    // IN1 low, IN2 high: reverse
  digitalWrite(IN2, HIGH);
  Serial.println("Reverse");
  delay(1000);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  Serial.println("Stop");
  delay(500);
}

The L298N is an H-bridge: IN1 and IN2 choose which side of the motor is connected to the supply and which to ground, so the current can run either way through it. High on one input and low on the other is a direction; both the same stops the drive.

With the ENA jumper in place the channel is always on, so the motor runs at full speed whenever the inputs say go. To control speed, take the jumper off, wire ENA to a PWM pin and analogWrite() to it.

Serial.begin(9600) opens the serial monitor. Serial is the USB port, as on the Uno; a clone with a CH340 chip needs that driver on a real computer, which the simulator does not.

Pitfalls

Common mistakes with the L298N on the Arduino Nano

Forgetting the common ground

The inputs are measured against the driver’s ground. With the board’s ground not joined to it, the signals mean nothing and the motor does not turn or twitches at random.

Powering the motor from the board

A motor takes far more current than the Arduino Nano’s pins or its 5V pin can give. Use a separate supply on the 12V terminal, and join the grounds.

Expecting the full supply voltage at the motor

The L298N’s bridge drops about 2 V, so a 6 V motor on a 6 V supply runs slowly. Use a supply about 2 V above the motor’s rating.

FAQ

Questions and answers

How do I connect an L298N to an Arduino Nano?

Wire IN1 and IN2 to pin 2 and pin 3, join the L298N’s GND to GND and to the motor supply’s ground, put the supply on 12V, and the motor on OUT1 and OUT2.

How do I control the speed?

Remove the ENA jumper, wire ENA to a PWM pin, and analogWrite() a value from 0 to 255 to it while IN1 and IN2 set the direction.

Why does the motor run slowly?

The bridge drops about 2 V and the motor gets the rest, so a low supply means a slow motor. Check the supply voltage first.

Can the L298N run two motors?

Yes. Channel B has IN3, IN4, ENB and OUT3, OUT4 and works the same way as channel A.

Build your own L298N project

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