What you will build
A small 2WD or 4WD robot car powered by Raspberry Pi 4 or Raspberry Pi 5. It can drive forward/backward, turn left/right, and it will stop/turn when it detects an obstacle using an HC-SR04 ultrasonic sensor.
Parts list (with shopping links)
These are examples from common reputable stores. You can swap brands - just match the part type.
Raspberry Pi 4 or Raspberry Pi 5
Either works. Pi 5 is faster.
Official Power Supply
Pi 5: official 27W USB-C PD recommended. Pi 4: official 15W USB-C is fine.
microSD Card (32GB+)
Use a name brand A1/A2 card. Cheap fakes cause random crashes.
Robot car chassis + motors (2WD or 4WD)
2WD is easiest. 4WD is fine and gives more traction.
Motor driver (choose one)
TB6612FNG runs cooler than L298N. L298N is common and beginner-friendly.
Ultrasonic distance sensor (HC-SR04)
Important: the Echo pin is 5V. Use a resistor divider to protect the Pi.
Battery holder / motor battery
Beginner-safe option: 6xAA NiMH to power motors + driver. (Pi uses its own USB-C supply.)
Optional: Buck converter (if you want one battery)
LM2596 step-down can be used to make 5V from a higher battery. Only do this if you know what you're doing.
Blunt truth (so you don't fry the Pi)
- Motors cause voltage dips + noise.
- If the Pi shares motor power, it will reboot or corrupt the SD card.
- Use separate motor power and tie grounds together (GND).
Build steps (detailed)
Step 1 - Assemble the chassis
- Mount motors to the chassis.
- Press/attach wheels firmly.
- Install caster wheel (if included).
- Mount the battery holder.
- Mount the Raspberry Pi on standoffs (do not let it touch metal).
Step 2 - Wiring the motor driver
Suggested GPIO mapping (BCM): IN1=17, IN2=18, IN3=22, IN4=23. Always connect GND between Pi and driver.
Printable wiring + pin map
Print this page section for a clear pin-by-pin build reference.
Tip: the PDF is easiest to print; the PNG is best for zooming on a phone/tablet.
Step 3 - Wiring the ultrasonic sensor safely
TRIG=GPIO 24. ECHO=GPIO 25 through a resistor divider (example: 2k from Echo to GPIO, 1k from GPIO to GND).
Step 4 - Install Raspberry Pi OS
- Flash Raspberry Pi OS (64-bit) with Raspberry Pi Imager.
- Boot, finish setup, connect network.
- Update:
sudo apt update && sudo apt full-upgrade -y
Code
Motor test (robot_test.py)
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
IN1, IN2, IN3, IN4 = 17, 18, 22, 23
for p in (IN1, IN2, IN3, IN4):
GPIO.setup(p, GPIO.OUT)
def stop():
for p in (IN1, IN2, IN3, IN4):
GPIO.output(p, 0)
def forward():
GPIO.output(IN1, 1); GPIO.output(IN2, 0)
GPIO.output(IN3, 1); GPIO.output(IN4, 0)
try:
forward()
time.sleep(2)
stop()
time.sleep(1)
finally:
stop()
GPIO.cleanup()
Run: python3 robot_test.py
Obstacle avoidance (concept)
Loop: measure distance. If under 20cm - stop, turn, continue. Otherwise drive forward.
Videos that help (embedded)
These are external YouTube videos. If one ever disappears, the text tutorial still works.
Install Raspberry Pi OS with Raspberry Pi Imager.
HC-SR04 distance sensing on Raspberry Pi.
TB6612FNG motor controller wiring basics for Raspberry Pi.
Printable PDF
Download the printable version here: wolfieweb_pi_robot_tutorial.pdf