How to Build a Raspberry Pi Robot: Parts and First Code
A working first Raspberry Pi robot needs six things: a Pi, a chassis with two motors, a motor driver board, one battery pack for the motors, a power bank for the Pi, and an ultrasonic sensor. Wire them so the motors and the Pi never share a power source, run about fifteen lines of Python, and you have a small car that drives forward and backs away from walls. Expect a weekend, not an evening, if this is your first time.
We have not built this exact robot on our own bench for this article, so treat what follows as editorial guidance based on how these parts are documented and what makers consistently report. Where something is a common trap, we say so.
What do you actually need to buy?
Start with the board. A Raspberry Pi Zero 2 W is small, light and gentle on batteries, which matters because every gram and every milliamp comes out of a robot’s patience. A Pi 4 gives you more speed and full-size USB ports, handy if you plan to add a camera. A Pi 5 will run vision models more comfortably, but it wants a stronger power supply and is overkill for a car that avoids walls. If you already own a spare Pi, use it and skip this decision.
The chassis is the part that decides whether you have fun. A two-wheel-drive kit with a caster ball, two geared yellow motors and a battery holder is the classic cheap starting point, and it costs roughly the price of a nice dinner. Four-wheel-drive kits grip better on carpet but skid when they turn. For a first robot we would take the simple two-wheeler, because it turns on the spot and behaves predictably. Search for a 2WD robot chassis kit and check that motors, wheels and a battery box are all included.
The motor driver is the translator between the Pi and the motors. An L298N board is the cheap, widely documented option and has screw terminals, so no soldering. It is inefficient and drops a couple of volts, which is acceptable on a toy. A TB6612-based board is more efficient and runs cooler; it is a better pick if your motors are small and your battery is tiny. A motor driver board is a few dollars either way.
For the sensor, an HC-SR04 ultrasonic module is the usual choice. It is cheap and good enough to stop a robot before it hits a wall, though it can miss soft or angled surfaces like a sofa cushion.
Last, the power. You need a battery pack for the motors (four AA cells in a holder, or a rechargeable 7.4 V pack, depending on your motors’ rating) and a USB power bank for the Pi. A Pi Zero 2 W will run happily from most power banks. A Pi 4 or 5 is picky: some banks cut out under load, and a flaky bank looks exactly like a software bug.
| Part | Cheap pick | Better pick | Skip if |
|---|---|---|---|
| Board | Pi Zero 2 W | Pi 4 (2 GB is enough) | You want a Pi 5 for a car that avoids walls |
| Chassis | 2WD kit with caster | 4WD kit for rough floors | You plan to 3D print your own (a good second build) |
| Motor driver | L298N | TB6612 board | Your motors draw more than the board can handle |
| Sensor | HC-SR04 | Time-of-flight sensor (VL53L0X) | You want mapping, which needs lidar |
| Power | AA holder + basic power bank | Rechargeable pack + quality bank | Never share one supply for both |
Why shouldn’t the motors share the Pi’s power?
This is the mistake that eats the first weekend of nearly everyone who builds one of these, so it gets its own section.
A motor is a hungry, noisy load. When it starts turning it pulls a burst of current, and if that burst comes from the same supply as the Pi, the voltage dips just long enough for the Pi to reboot or its SD card to complain. You will see the robot twitch, the SSH connection drop and the green LED flicker, and you will assume the code is broken. It is not. The supply is.
The wiring principle is simple. The battery pack goes to the motor driver’s power input. The driver’s motor outputs go to the motors. The Pi’s GPIO pins go to the driver’s input pins and only send signals. The Pi itself is powered through its own USB power bank. The one connection the two sides share is ground: run a wire from the driver’s GND terminal to a Pi GND pin so the signals have a common reference. Without it the driver may react randomly or not at all.
How do you set up the Pi without a monitor?
Use Raspberry Pi Imager on your laptop. Before it writes the SD card, open the advanced options and set a hostname (something like robot), enable SSH, enter your Wi-Fi details and create a user. Flash the card, insert it, power the Pi and give it a minute or two. Then from your laptop open a terminal and type ssh yourname@robot.local. If that works, you never need a screen on the robot again.
If robot.local does not resolve, look for the Pi’s address in your router’s device list and use the IP number instead. Keep the robot on a bench or on blocks while you test, with the wheels in the air, so a bad script does not drive it off the table.
The first code: drive and avoid
Raspberry Pi OS comes with gpiozero, a Python library that makes motors and sensors read almost like English. Its Robot class takes two pairs of pins, one pair per side, where the first pin of each pair drives forward and the second drives backward. Wire them to the driver’s input pins (IN1 to IN4 on an L298N) and match the numbers in the script.
The ultrasonic sensor needs one extra part. An HC-SR04 runs on 5 V, and its echo pin sends 5 V back, which is more than the Pi’s 3.3 V pins tolerate. Put a voltage divider on the echo line (for example a 1 kΩ and a 2 kΩ resistor pair) before it reaches the Pi. Skipping that is how people damage a pin.
from gpiozero import Robot, DistanceSensor
from time import sleep
robot = Robot(left=(5, 6), right=(13, 19))
sensor = DistanceSensor(echo=24, trigger=23, max_distance=2)
while True:
if sensor.distance < 0.25: # closer than about 25 cm
robot.backward(0.5)
sleep(0.4)
robot.right(0.6)
sleep(0.5)
else:
robot.forward(0.6)
sleep(0.05)
Save it as drive.py and run it with python3 drive.py. Press Ctrl+C to stop. The numbers in the brackets are speeds from 0 to 1, and they are the first things to change: if the robot veers to one side, lower the speed on the stronger motor, because cheap geared motors are never perfectly matched.
If the robot turns the wrong way, swap the two wires on that side of the driver instead of editing the code. If it spins when you expected it to go straight, one side is almost certainly reversed.
What goes wrong, and what to do about it
Most problems come in three flavours, and all three are fixable.
When the robot wanders or curves, the motors are mismatched or the wheels are slipping. Lower the speed, and try a straight run on a hard floor before blaming code. When it does nothing, check the common ground and whether the driver’s enable jumpers are in place; on an L298N, those little jumpers on ENA and ENB must be fitted if you want full speed. When it reboots under load, go back to the power section and separate the supplies.
A fourth issue is less obvious: the ultrasonic sensor can read nonsense when the robot is vibrating or pointing at a soft surface. A rock-steady mount and a small delay between readings help. If readings jump around, average the last three.
Is a Raspberry Pi the right base for a robot?
Not always, and we would rather tell you now. If you only want a car that avoids walls, an Arduino is simpler and cheaper, and it runs for longer on batteries; our guide to Arduino robot kits for beginners covers that path. An Arduino has no operating system to boot, no SD card to corrupt and no Wi-Fi to configure.
The Pi earns its place when you want something an Arduino cannot do: a camera that follows a coloured ball, a robot you steer from your phone’s browser, voice commands, or a small language model on board. That is the real advantage, and it is why many makers use both together, with an Arduino handling motors and a Pi handling thinking. The trade-off is boot time, power draw and the fact that a Linux computer can fail in ways a microcontroller never does.
If the learner is a child, a kit built for them will be less frustrating; see our STEM robots for kids guide. And if you are curious about how a commercial robot builds a map and avoids obstacles with far better sensors, our piece on lidar versus camera navigation shows what a Pi robot is a toy version of.
These bars are our editorial reading of how a typical first Pi robot compares with an Arduino one, not measured results.
Where to go from here
Once the car avoids walls, the next steps are obvious and satisfying. Add a second sensor on the side so it follows a wall. Add a Pi camera and try a colour-tracking script with OpenCV. Put a small web page on the Pi with arrow buttons so you can drive it from your phone. Each is a weekend, and each teaches something the previous one did not.
Our verdict: if you want to learn how a robot senses, decides and moves, and you want the freedom to add vision later, build a Pi robot, and keep the motor power separate from the start. If you only want something that rolls, take the Arduino route and enjoy it. For more, browse the robotics section.