The L298N is the red dual H-bridge in every robot-car kit. It lets a Pico W switch a motor’s direction with two pins and set its speed with PWM on the enable pin — while the motor current comes from its own battery.
Wire IN1/IN2 to GP14/GP13, ENA to GP15 (jumper removed), the battery to the 12V terminal, and join every ground. The script ramps the motor up, brakes, reverses and stops.
Wiring
| Raspberry Pi Pico W | L298N dual H-bridge motor driver | Wire | Notes |
|---|---|---|---|
| — | OUT1 | — | To DC motor (+) (not drawn) |
| — | OUT2 | — | To DC motor (−) (not drawn) |
| GP15 | ENA | purple | GP15 is physical pin 20. Remove the ENA jumper first, or speed control does nothing. |
| GP14 | IN1 | yellow | GP14 is physical pin 19. |
| GP13 | IN2 | orange | GP13 is physical pin 17. |
| GND | GND | black | Pico GND, battery − and L298N GND must all meet here. |
| Motor battery, 7–12 V (2× 18650 or 12 V pack) · + | 12V | red | Motor supply into the 12V screw terminal. |
| Motor battery, 7–12 V (2× 18650 or 12 V pack) · - | GND | black |
Motor supply 5–35 V on the 12V terminal; logic 5 V part, 3.3 V board. The L298N’s inputs see anything above about 2.3 V as HIGH, so 3.3 V boards drive IN1–IN4 and ENA/ENB directly. Always connect the board’s GND to the driver’s GND.
Board quirks
- The L298N treats anything above about 2.3 V as HIGH, so the Pico’s 3.3 V outputs drive IN1, IN2 and ENA directly.
- Leave the L298N’s 5V pin unconnected to the Pico. With the 5V-EN jumper fitted it is an output from the module’s regulator — don’t join it to VBUS while USB is plugged in.
- The L298N loses about 2 V inside, so a 6 V motor on a 7.4 V pack runs slower than you expect. A TB6612FNG is the efficient alternative.
- PWM at 1 kHz suits the L298N’s slow bipolar transistors; the motor may whine a little.
Code
# One DC motor on an L298N driver from a Raspberry Pi Pico W (MicroPython): direction + PWM speed.
# Wiring: ENA -> GP15 (pin 20, remove the ENA jumper), IN1 -> GP14 (pin 19), IN2 -> GP13 (pin 17),
# L298N GND -> Pico GND and battery -, battery + (7-12 V) -> 12V terminal, motor -> OUT1/OUT2.
# Never power the motor from the Pico.
from machine import Pin, PWM
import time
in1 = Pin(14, Pin.OUT, value=0)
in2 = Pin(13, Pin.OUT, value=0)
ena = PWM(Pin(15))
ena.freq(1000) # 1 kHz: the L298N's slow transistors don't like much faster
ena.duty_u16(0)
def motor(speed):
"""speed -100..100 %. 0 = coast."""
if speed > 0:
in1.value(1)
in2.value(0)
elif speed < 0:
in1.value(0)
in2.value(1)
else:
in1.value(0)
in2.value(0)
ena.duty_u16(min(abs(speed), 100) * 65535 // 100)
def brake():
in1.value(1)
in2.value(1) # both HIGH with ENA on = motor terminals shorted = quick stop
ena.duty_u16(65535)
print("L298N motor A: ENA GP15 (PWM), IN1 GP14, IN2 GP13")
while True:
for s in range(0, 101, 20):
print("Forward {} %".format(s))
motor(s)
time.sleep(1)
print("Brake")
brake()
time.sleep(1)
print("Reverse 60 %")
motor(-60)
time.sleep(2)
print("Stop")
motor(0)
time.sleep(2)# One DC motor on an L298N driver from a Raspberry Pi Pico W (MicroPython): direction + PWM speed.
# Wiring: ENA -> GP15 (pin 20, remove the ENA jumper), IN1 -> GP14 (pin 19), IN2 -> GP13 (pin 17),
# L298N GND -> Pico GND and battery -, battery + (7-12 V) -> 12V terminal, motor -> OUT1/OUT2.
# Never power the motor from the Pico.
from machine import Pin, PWM
import time
in1 = Pin(14, Pin.OUT, value=0)
in2 = Pin(13, Pin.OUT, value=0)
ena = PWM(Pin(15))
ena.freq(1000) # 1 kHz: the L298N's slow transistors don't like much faster
ena.duty_u16(0)
def motor(speed):
"""speed -100..100 %. 0 = coast."""
if speed > 0:
in1.value(1)
in2.value(0)
elif speed < 0:
in1.value(0)
in2.value(1)
else:
in1.value(0)
in2.value(0)
ena.duty_u16(min(abs(speed), 100) * 65535 // 100)
def brake():
in1.value(1)
in2.value(1) # both HIGH with ENA on = motor terminals shorted = quick stop
ena.duty_u16(65535)
print("L298N motor A: ENA GP15 (PWM), IN1 GP14, IN2 GP13")
while True:
for s in range(0, 101, 20):
print("Forward {} %".format(s))
motor(s)
time.sleep(1)
print("Brake")
brake()
time.sleep(1)
print("Reverse 60 %")
motor(-60)
time.sleep(2)
print("Stop")
motor(0)
time.sleep(2)Expected serial output
L298N motor A: ENA GP15 (PWM), IN1 GP14, IN2 GP13
Forward 0 %
Forward 20 %
Forward 40 %
Brake
Reverse 60 %
StopL298N motor A: ENA GP15 (PWM), IN1 GP14, IN2 GP13 Forward 0 % Forward 20 % Forward 40 % Brake Reverse 60 % Stop
Common errors
Motor runs full speed whatever the PWM
The ENA jumper is still on. Pull it off and connect ENA to GP15.
Motor doesn’t move at low PWM
DC motors need a minimum voltage to start. Begin at 40 % or more, then slow down.
Pico resets when the motor starts
Missing common ground or motor noise on the supply. Join all grounds, keep motor wires away from the Pico, and solder a 100 nF capacitor across the motor terminals.