Home · Module 4 · Build the Raspberry Pi Car
From Pi to autonomous car
Click a GPIO pin to see what it does and what you'd typically wire there for the car project.
Raspberry Pi 4 / 5 — 40-pin GPIO header
Pin detail
Click any pin on the board.
Bill of materials
- 1× Raspberry Pi (3B+, 4, or 5) + microSD with Raspberry Pi OS
- 1× L298N H-bridge motor driver
- 2× DC gear motors with wheels (1:48 ratio typical)
- 1× HC-SR04 ultrasonic sensor
- 1× SG90 servo (optional — for sweeping the ultrasonic)
- 1× 4×AA or 6V battery pack for motors
- 1× USB power bank for the Pi
- Jumper wires, breadboard, chassis
Setup steps (Session 9–10)
- Flash Raspberry Pi OS Lite onto microSD.
- SSH in. Enable GPIO library:
pip install RPi.GPIO. - Connect L298N power inputs to battery pack (+/−).
- Motor A & B leads → OUT1-2, OUT3-4 on L298N.
- L298N IN1-IN4 → 4 Pi GPIOs (direction).
- L298N ENA, ENB → 2 Pi PWM-capable GPIOs (speed).
- HC-SR04 VCC→5V, GND→GND, TRIG→GPIO, ECHO→GPIO via voltage divider.
- Common ground between Pi and motor battery — critical.
Driving the motors (your first script)
import RPi.GPIO as GPIO
import time
# Motor pins
IN1, IN2, IN3, IN4 = 5, 6, 13, 19
ENA, ENB = 12, 18
GPIO.setmode(GPIO.BCM)
for p in [IN1, IN2, IN3, IN4, ENA, ENB]:
GPIO.setup(p, GPIO.OUT)
pwm_a = GPIO.PWM(ENA, 1000) # 1 kHz
pwm_b = GPIO.PWM(ENB, 1000)
pwm_a.start(70); pwm_b.start(70) # 70% duty
def forward():
GPIO.output(IN1, True); GPIO.output(IN2, False)
GPIO.output(IN3, True); GPIO.output(IN4, False)
def stop():
for p in (IN1, IN2, IN3, IN4): GPIO.output(p, False)
forward(); time.sleep(2); stop()
GPIO.cleanup()
Once this works, move to the Autonomous Navigation simulator to see what code-driven obstacle avoidance looks like — exactly what you'll port to the car.