Board and part

Raspberry Pi Pico L298N Simulator: Wiring, Code and a Live Circuit

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

  • 3.3 V logic
  • No library
  • IN1 15, IN2 14
  • 9 V motor supply
Raspberry Pi Pico · 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 Raspberry Pi Pico

Two signal wires, GP15 (physical pin 20) and GP14 (physical pin 19), 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 pinRaspberry Pi Pico pinWhy
IN1GP15 (physical pin 20)With IN2, sets the direction: IN1 high and IN2 low turns the motor one way.
IN2GP14 (physical pin 19)IN2 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 Raspberry Pi Pico’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 3.3 V signals work directly even from a 3.3 V pin. 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 Raspberry Pi Pico

The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.

On a real bench

Solder headers on the Pico and press it into the breadboard across the center channel; the L298N goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.

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 Raspberry Pi Pico GP15 (physical pin 20), GP14 (physical pin 19)
const int IN1 = 15;   // GP15 (physical pin 20)
const int IN2 = 14;   // GP14 (physical pin 19)

void setup() {
  Serial.begin(115200);
  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(115200) opens the serial monitor. Serial is the USB port at 115200 baud in this sketch; on a real Pico it appears once the board has enumerated.

Pitfalls

Common mistakes with the L298N on the Raspberry Pi Pico

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 Raspberry Pi Pico’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 a Raspberry Pi Pico?

Wire IN1 and IN2 to GP15 (physical pin 20) and GP14 (physical pin 19), 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.