DC motor + L298N
A DC motor just spins when you power it — no feedback, no position, and reversing the supply reverses it. That makes it the motor for wheels, fans and anything that turns continuously. A GPIO cannot drive one: the coils want far more than the pin's 16 mA safe limit, and the back-EMF of a spinning motor kicks back at whatever switches it. The shop's part is a geared DC motor on an L298N H-bridge driver board for $12: two logic inputs pick the direction, a third pin enables the bridge, and the motor current comes from its own supply through the board.
Pins
| Pin | What it is | Type |
|---|---|---|
| ENA | Enable A / speed (PWM; jumper ties it high) | io |
| IN1 | Direction input 1 | io |
| IN2 | Direction input 2 | io |
| +12V | Motor supply 5–35 V | power |
| GND | Ground (join it to the Pi's ground) | ground |
| +5V | Logic supply 5 V (made on board when the 5 V jumper is on and +12V ≤ 12 V) | power |
| Setting | Type | Range | Default |
|---|---|---|---|
enaJumper | enum | removed, fitted | removed |
regulator | enum | fitted, removed | fitted |
Motor()— DC motor + L298N — $12- Pins: 6
IN1 and IN2 are direction: one high and the other low turns the motor; both the same brakes it. ENA is a switch on the bridge — drive it high to run, or PWM it to set the speed. +12V feeds the motor (despite the name, anything 5–35 V: 4×AA, a 6 V pack, a bench supply). +5V is the board's logic supply: with its jumper fitted the board makes 5 V from +12V when that is at least 7 V, and otherwise you feed 5 V in. Either way its GND and the Pi's ground must be joined.
Wiring it in circuit.py
from wiring import *
pi = Pi()
motor = Motor()
motor.v12 >> pi.V5 # motor supply (bench: a 6 V battery pack)
motor.v5 >> pi.V5 # logic supply (or drop the jumper and feed this pin)
motor.gnd >> pi.GND # the two grounds must be joined
motor.in1 >> pi.GPIO5
motor.in2 >> pi.GPIO6
motor.ena >> pi.GPIO12The ENA jumper on a real board is removed in the simulator default, so ENA must be driven (or hold the jumper to run flat out). motor.gnd >> pi.GND models the single ground wire between driver and Pi.
Driving it in main.py
from gpiozero import Motor
from time import sleep
motor = Motor(forward=5, backward=6, enable=12)
while True:
motor.forward() # full speed forward
sleep(1)
motor.backward(0.5) # half speed in reverse
sleep(1)gpiozero's Motor applies the PWM to the direction pins and simply holds ENA high, so wiring enable=12 as above is what makes motor.forward(0.5) mean half speed. If you would rather PWM ENA yourself, drive IN1 and IN2 with RPi.GPIO and keep the ENA jumper off.
Electrical notes
- The bridge loses about 2 V to its output transistors, so the motor sees
supply − 2 V. A 6 V motor therefore wants about 8 V on+12V; from the Pi's 5 V pin it gets 3 V and runs at half its rated speed. - Logic thresholds: the inputs read low below 1.5 V and high above 2.3 V, so 3.3 V GPIOs drive them safely. The board's own logic draws about 36 mA; it can pass up to 2 A to the motor.
- Keep the input PWM below 40 kHz; anything in the audio range will whine.
- The motor needs roughly a quarter of its rated voltage to break free. Below that it hums and stalls (
stallwarning), drawing stall current without turning. - The board's 5 V regulator needs
+12V≥ 7 V; from a 6 V pack, remove the regulator jumper and feed+5V. - A relay module switches a motor only on and off; speed and direction need the H-bridge.
Simulator vs real hardware
IN1/IN2/ENAare averaged into a motor voltage ((IN1 − IN2) × ENA × (supply − 2 V)), and the shaft follows it with first-order inertia (inertiaMs, default 150 ms). A real motor's first moments also fight inductance and friction; the model smooths them into one time constant.- Supply current follows back-EMF: from no-load current at full speed up towards
stallCurrentMaas the motor slows. Over 1.5× its rated voltage the model warnsovervolt— the windings will overheat. - The 47 kΩ pull-downs on the inputs are modelled, so a floating
INreads low instead of floating. - The board's regulator is modelled as a source when the jumper is fitted and
+12V≥ 7 V; otherwise the logic rail must be fed over 4.2 V. A real board also has a 5 V output you can borrow — the simulator has no pin for that, so power logic from the Pi.
Going deeper
- Primers: PWM and voltage, current and Ohm's law.
- On/off instead of speed: relay and relays and switching loads.
- Precise positioning instead of continuous rotation: stepper + ULN2003 and servo.
- The library: gpiozero covers
Motor.
Further reading
- ST: L298 dual full-bridge driver datasheet — saturation voltage, input thresholds and current limits.
- gpiozero: Motor —
forward(speed),backward(speed)and theenablepin. - Adafruit: Raspberry Pi Lesson 9 — Controlling a DC Motor — the same H-bridge idea wired on a bench.