For Raspberry Pi 4 & 5. Safe motor wiring, power isolation, ultrasonic hookup, and mandatory level shifting. Works with L298N or TB6612FNG.
Pi GPIO inputs to driver control pins:
Speed control (optional): Use PWM pins for PWMA / PWMB (TB6612FNG) or ENA / ENB (L298N) if you want speed control. Otherwise tie them high.
Left motor:
Right motor:
If a wheel spins backward, swap + and − for that motor only.
Motors + driver must be powered from a separate battery pack:
Raspberry Pi is never powered from the motor battery. The Pi must use its own USB-C PSU.
Ground rule: Pi GND + Driver GND + Battery − + Sensor GND must be tied together.
Use 2 kΩ + 1 kΩ to drop ECHO from 5V → 3.3V:
HC-SR04 ECHO → 2 kΩ → GPIO25
|
1 kΩ
|
GND
Never feed 5V directly into Pi GPIO pins.
| Component | Signal | Pi GPIO / Power |
|---|---|---|
| Motor driver | IN1 | GPIO17 |
| Motor driver | IN2 | GPIO18 |
| Motor driver | IN3 | GPIO22 |
| Motor driver | IN4 | GPIO23 |
| Ultrasonic | TRIG | GPIO24 |
| Ultrasonic | ECHO | GPIO25 (via divider) |
| Ultrasonic | VCC | 5V |
| Ultrasonic | GND | GND |
| Driver + motor | VIN | Motor battery + |
| Driver + motor | GND | Shared ground |