Raspberry Pi practice problem · Motors & actuators
Bumper car
HardBudget $24Python · gpiozero
A robot car: a DC motor on an L298N (IN1 GPIO5, IN2 GPIO6, ENA GPIO12), an HC-SR04 distance sensor looking ahead (TRIG GPIO23, ECHO GPIO24 — its echo is 5 V, so use the 3.3 V-safe module or a divider) and an active buzzer on GPIO25 (pi.GPIO25 >> Buzzer() >> pi.GND; it may draw up to 25 mA).
Read the distance about every 0.1 s and drive by it:
| Distance | Motor | Buzzer |
|---|---|---|
| over 60 cm | forwards, full speed (~100 rpm) | off |
| 20–60 cm | forwards, 60 % (~60 rpm) | off |
| under 20 cm | stopped | on (steady) |
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.
The test starts with the obstacle at 100 cm, moves it to 40 cm at 2 s, 12 cm at 4 s and back to 80 cm at 6 s.