Board and part

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

This is an L298N motor driver wired to an Arduino Mega 2560, 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 Mega · 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 Mega

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 Mega 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 Mega’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 Mega

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

On a real bench

The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The L298N uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.

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 Mega 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; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.

Pitfalls

Common mistakes with the L298N on the Arduino Mega

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 Mega’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 Mega 2560?

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.