Beginner Robot Car Tutorial (Raspberry Pi 4 / Pi 5)

Build a real working robot car: drive, turn, and avoid obstacles. No fluff. Python code included.

Safety rule: never power motors from the Pi 5V rail. Use a separate motor battery and share GND.

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.

Wiring overview diagram for Raspberry Pi robot car Detailed wiring diagram for Raspberry Pi robot car – Pi to motor driver, motors, and HC-SR04 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

  1. Mount motors to the chassis.
  2. Press/attach wheels firmly.
  3. Install caster wheel (if included).
  4. Mount the battery holder.
  5. Mount the Raspberry Pi on standoffs (do not let it touch metal).

Step 2 - Wiring the motor driver (detailed)

We will use this GPIO mapping (BCM):

2.1 Power & ground (do this first)

Never use the Pi's 5V pin to power the motors. Motor noise and current spikes will reboot or kill the Pi.

2.2 Driver inputs: Pi → motor driver

Wire the Pi GPIOs to the driver’s control pins:

If your board has enable pins:

Optional (speed control): connect PWMA/PWMB or ENA/ENB to Pi PWM-capable pins and use PWM in code. If you don’t care about speed yet, just tie them high for full power.

2.3 Motors: driver → motors

For a 2WD robot:

If a wheel spins backwards, swap the + and wires for that motor only.

2.4 Motor battery → motor driver

The Pi does not get power from this battery. It still uses its own USB-C power.

Step 3 - Wiring the ultrasonic sensor safely (detailed)

We’ll use an HC-SR04 ultrasonic sensor.

Target wiring:

3.1 Why you need a level shifter

The HC-SR04 ECHO pin outputs around 5V. Raspberry Pi GPIO pins are only safe up to 3.3V. We drop 5V down to ~3.3V using two resistors as a simple voltage divider.

3.2 Resistor divider wiring (ECHO → GPIO25)

Example values: 2 kΩ and 1 kΩ.

HC-SR04 ECHO → 2 kΩ → GPIO25
                    |
                   1 kΩ
                    |
                   GND
      

Any similar ratio around 2:1 is fine (for example 3.3 kΩ + 1.8 kΩ), but 2 kΩ / 1 kΩ is easy.

3.3 Final sensor connections

GND for the Pi, motor driver, motor battery, and HC-SR04 must all be common.

Step 4 - Install Raspberry Pi OS

  1. Flash Raspberry Pi OS (64-bit) with Raspberry Pi Imager.
  2. Boot, finish setup, connect network.
  3. 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

Open wiring-only page · Download wiring-only PDF