Two brains on one rover: a Raspberry Pi for seeing, an Arduino for moving
When I added a Raspberry Pi 4, a camera and a lidar to my little rover, the obvious plan was to let the Pi do everything.
Watch: The upgraded rover · The rover learning to follow a line
When I added a Raspberry Pi 4, a camera and a lidar to my little rover, the obvious plan was to let the Pi do everything. It runs Linux, it has Python and OpenCV, and it has GPIO pins. Why keep a microcontroller around at all?
Because the two boards are good at different things, much like a frontend and a backend.
Linux is not on time
A hobby servo like the MG90S that steers the rover expects a pulse every 20 ms, between about 1 and 2 ms long, and the length of that pulse is the angle. The motors are driven with PWM through a motor driver. Both need steady timing.
The Pi can produce PWM, but it runs a full operating system. Your Python script can be paused at any moment because the camera driver, Wi-Fi or a log write needs the CPU. For a web server, a 30 ms hiccup is invisible. For a servo, it’s a twitch.
An Arduino runs one program and nothing else. Its Servo library generates the pulses with hardware timers, so they stay steady however busy your loop is. So the split I ended up with is simple:
- The Pi does the thinking: camera frames, OpenCV, deciding where to go, and anything that touches the network.
- The Arduino does the moving: servo pulses, motor PWM, and stopping safely when something goes wrong.
A protocol small enough to read
The two boards need a way to talk. Over USB, the Arduino shows up on the Pi as a serial port. A one-line text format is enough to start with, and you can read it in a terminal while debugging:
S90 T120
That’s steering angle 90° and throttle 120 out of 255. On the Pi, with pyserial:
import time
import serial # pip install pyserial
arduino = serial.Serial("/dev/ttyUSB0", 115200, timeout=0.1)
time.sleep(2) # opening the port resets most Arduinos, so give it time to boot
def drive(steering_deg: int, throttle: int) -> None:
arduino.write(f"S{steering_deg} T{throttle}\n".encode())
drive(90, 0)
And on the Arduino:
#include <Servo.h>
const int STEERING_PIN = 9;
const int MOTOR_PWM_PIN = 5; // to the motor driver's PWM input
const unsigned long TIMEOUT_MS = 500;
Servo steering;
unsigned long lastCommand = 0;
void setup() {
Serial.begin(115200);
Serial.setTimeout(50);
steering.attach(STEERING_PIN);
steering.write(90);
pinMode(MOTOR_PWM_PIN, OUTPUT);
}
void loop() {
if (Serial.available()) {
String line = Serial.readStringUntil('\n');
int s = line.indexOf('S');
int t = line.indexOf('T');
if (s >= 0 && t > s) {
int angle = line.substring(s + 1, t).toInt();
int throttle = line.substring(t + 1).toInt();
steering.write(constrain(angle, 60, 120));
analogWrite(MOTOR_PWM_PIN, constrain(throttle, 0, 255));
lastCommand = millis();
}
}
// If the Pi goes quiet, stop the motors
if (millis() - lastCommand > TIMEOUT_MS) {
analogWrite(MOTOR_PWM_PIN, 0);
}
}
Three details in there matter more than they look:
- The timeout. If the Python script crashes, the Arduino keeps doing the last thing it was told, forever. With a timeout, the Pi has to keep talking, and silence means stop. It’s the same idea as a heartbeat on a WebSocket.
- constrain() on the steering. The linkage can only turn so far. Clamping on the Arduino means a bug on the Pi can’t push the servo into the frame and strip its gears.
- The pins. On an Arduino Nano, the Servo library uses the timer behind pins 9 and 10, so analogWrite stops working on those two. The motor PWM lives on pin 5.
Power is the other half
Some of the most confusing problems on a small robot aren’t in the code at all. When the motors start, they draw a burst of current. If the Pi shares that supply, the voltage dips and the Pi can reboot or corrupt its SD card.
The fix is boring and reliable: give the Pi its own supply (the official Raspberry Pi 4 adapter is 5.1 V, 3 A), power the motors and servo separately, and connect the grounds. A USB cable between the boards already shares the ground.
If you connect the boards through the UART pins instead of USB, watch the voltages. The Pi’s GPIO pins work at 3.3 V and aren’t 5 V tolerant, while a Nano talks at 5 V. The Nano’s TX line needs a level shifter, or at least a voltage divider, before it reaches the Pi’s RX pin. USB avoids the whole question, which is why I’d start there.
Why the split pays off
Once the Arduino side is solid, you rarely need to touch it again. The interesting work, like line following with OpenCV, happens on the Pi in Python, where restarting a script takes a second. The Arduino stays a small, boring, trustworthy layer underneath. That’s the best thing you can say about a backend.