Learn
L298N Motor Driver and DC Motor on a Raspberry Pi 4B
Wire an L298N motor driver, a DC motor and a DC power supply to a Raspberry Pi 4B, then run the motor forward, brake and reverse with Python and PWM speed.
Note: the L298N Motor Driver + DC Motor is a VIP-tier component in the simulator — you’ll need a VIP plan to use it there.
Try this directly in the free Raspberry Pi 4B simulator — no hardware or signup required. New to the GPIO header? Start with the interactive pinout guide.
What you’ll need
- Raspberry Pi 4B
- L298N Motor Driver
- DC Motor
- DC Power Supply (the motor's own supply — see the DC Power Supply guide)
Step by step
- Drag a Raspberry Pi 4B, an L298N Motor Driver, a DC Motor and a DC Power Supply onto the Canvas. The L298N is not breadboard-mounted; you wire it with drawn wires.
- Hover over the pins to read their labels. The L298N has OUT1, OUT2, 12V (motor supply), GND, 5V, OUT3 and OUT4 along the top, and ENA, IN1, IN2, IN3, IN4 and ENB along the bottom. The DC Motor has Lead 1 and Lead 2. The DC Power Supply has + and −.
- Select the DC Power Supply and set Output voltage to 9. The motor is rated 6 V by default, and the driver drops about 2 V, so 9 V gives the motor 7 V at full speed.
- Wire the power: DC Power Supply + → L298N 12V. DC Power Supply − → L298N GND. Also wire a Pi GND pin (for example pin 39) → L298N GND, so the Pi and the driver share a ground, as on real hardware. Leave the L298N 5V pin unwired: the 5V regulator jumper is installed by default, so the board makes its own 5 V from the motor supply.
- Wire the motor: DC Motor Lead 1 → L298N OUT1. DC Motor Lead 2 → L298N OUT2.
- Wire the control pins: L298N IN1 → Pi pin 37. L298N IN2 → Pi pin 35. Leave ENA unwired. The ENA jumper is installed by default, which holds the motor at full speed.
- Go to the Code tab and write:
import RPi.GPIO as GPIOimport asyncio IN1 = 37IN2 = 35 GPIO.setmode(GPIO.BOARD)GPIO.setup(IN1, GPIO.OUT)GPIO.setup(IN2, GPIO.OUT) print("Forward")GPIO.output(IN1, GPIO.HIGH)GPIO.output(IN2, GPIO.LOW)await asyncio.sleep(3) print("Brake")GPIO.output(IN1, GPIO.HIGH)GPIO.output(IN2, GPIO.HIGH)await asyncio.sleep(2) print("Reverse")GPIO.output(IN1, GPIO.LOW)GPIO.output(IN2, GPIO.HIGH)await asyncio.sleep(3) print("Brake")GPIO.output(IN1, GPIO.LOW)GPIO.output(IN2, GPIO.LOW)await asyncio.sleep(2) GPIO.cleanup()- Click Start. Watch the motor and the small text on the L298N board.
Optional: control the speed with PWM. Stop the simulation, select the L298N and uncheck the ENA jumper in its settings. Wire L298N ENA → Pi pin 33. Then replace the script with this one and click Start:
import RPi.GPIO as GPIOimport asyncio IN1 = 37IN2 = 35ENA = 33 GPIO.setmode(GPIO.BOARD)GPIO.setup(IN1, GPIO.OUT)GPIO.setup(IN2, GPIO.OUT)GPIO.setup(ENA, GPIO.OUT) GPIO.output(IN1, GPIO.HIGH)GPIO.output(IN2, GPIO.LOW) pwm = GPIO.PWM(ENA, 1000)pwm.start(100)print("Full speed")await asyncio.sleep(3) pwm.ChangeDutyCycle(50)print("Half speed")await asyncio.sleep(3) pwm.ChangeDutyCycle(0)print("Coast")await asyncio.sleep(4) pwm.stop()GPIO.cleanup()What “working correctly” looks like
- How to test it: select the L298N while the script runs. Its settings panel shows a line like "Supply 9.0V · Logic powered · A: fwd · B: brake". Channel B shows brake because nothing is wired to it and both of its inputs are LOW.
- Forward: the board shows A:fwd and the motor rotor turns. Reverse: the board shows A:rev and the rotor turns the other way.
- Brake (IN1 and IN2 both HIGH, or both LOW): the board shows A:brake and the rotor stops quickly.
- PWM part: half speed turns the rotor slower than full speed. Duty cycle 0 shows A:coast, and the rotor slows down gradually instead of stopping fast. That difference between brake and coast is the point of an H-bridge.
If something’s wrong
- The board shows A:off and the motor never turns → the driver has no power. Check that the DC Power Supply + goes to 12V and − goes to GND, and that GND also reaches a real ground. The settings panel shows "Logic unpowered" until that works.
- The Console shows a warning about the 5V regulator jumper → your supply is above 12 V, which the onboard regulator cannot handle. Set the supply to 12 V or less, or remove the 5V jumper and power the logic from a separate 5 V source.
- The Console shows a note that an enable pin is wired but its jumper is still installed → the jumper holds ENA at full speed. Uncheck the ENA jumper in the L298N settings to control speed with PWM.
- The Console warns that the motor would overheat → the motor voltage is more than 1.5 times its rating. Lower the supply, use PWM, or raise the motor's Rated voltage in its settings.
- The motor spins the wrong way → swap the motor's two leads, or swap IN1 and IN2 in your script.
- An LED wired to an OUT pin does nothing → in this simulator only a wired DC Motor uses the driver's outputs.