PyRasp

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.

DC motor + L298N

Pins

PinWhat it isType
ENAEnable A / speed (PWM; jumper ties it high)io
IN1Direction input 1io
IN2Direction input 2io
+12VMotor supply 5–35 Vpower
GNDGround (join it to the Pi's ground)ground
+5VLogic supply 5 V (made on board when the 5 V jumper is on and +12V ≤ 12 V)power
SettingTypeRangeDefault
enaJumperenumremoved, fittedremoved
regulatorenumfitted, removedfitted

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

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.GPIO12

The 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

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 (stall warning), 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

  • The module is a real bridge in the simulator: a driver core (the input logic, the four base drives and the motor model) plus four TIP120 Darlingtons with their 1N4007 flyback diodes. By default the engine runs a fast model of it — (IN1 − IN2) × ENA × (supply − 2 V) — and flattens the full bridge when you open its internals. The conformance test drives both and holds them to the same motor.
  • In the full bridge the motor's current really flows through the Darlingtons, so their drop is solved rather than assumed: about 1.3 V at tens of milliamps and the datasheet's 2 V near the bridge's rated current, which is why a probe inside reads the outputs a little higher than the fast model's figure.
  • IN1/IN2/ENA are averaged into a motor voltage, 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 stallCurrentMa as the motor slows. Over 1.5× its rated voltage the model warns overvolt — the windings will overheat.
  • The 47 kΩ pull-downs on the inputs are modelled, so a floating IN reads 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.
  • The fast model is what the Inspector's props (jumper settings) and feed knobs (motor rating, inertia) tune, and the full bridge reads the same settings: opening the internals never changes the result.

Going deeper

Further reading