PyRaspBeta

← All practice problems

Raspberry Pi practice problem · Motors & actuators

Emergency brake

MediumBudget $16Python · gpiozero

A DC motor on an L298N (IN1 GPIO5, IN2 GPIO6, ENA GPIO12) runs forwards at full speed from the start. A push button on GPIO27 (to ground; use the Pi's pull-up) is the emergency stop: when pressed, brake the motor and keep it braked.

Feed the driver's +12V and +5V terminals from the Pi's 5 V and join the grounds. On 5 V the bridge leaves the motor about 3 V: roughly 100 rpm at full speed, proportional to the PWM duty.

An H-bridge has two ways to stop. Coasting (both IN pins low, what gpiozero.Motor.stop() does) lets the motor spin down freely. Braking drives IN1 and IN2 both high with ENA high, shorting the motor's terminals through the bridge so it stops hard. The test presses the button at 2 s and from 2.2 s on checks that GPIO5, GPIO6 and GPIO12 are all high and that the motor has stopped (|rpm| < 2) by 2.6 s. Drive the three pins yourself (OutputDevice or RPi.GPIO).

More in Motors & actuators