Pairing a Pi with a Pico for Real-Time Control: the short answer
Linux is not a real-time operating system. On a Raspberry Pi, your Python loop can be paused for milliseconds at any time while the kernel handles Wi-Fi, the SD card or another program.
- Level
- Advanced
- Time
- About 6 min
- Topic
- Raspberry Pi
Split a project between a Raspberry Pi for Linux tasks and a Pico microcontroller for precise timing, linked by USB serial.
Before this: Raspberry Pi GPIO with Python, Interrupts, Timers and FreeRTOS on ESP32, Serial Communication Basics
Linux is not a real-time operating system. On a Raspberry Pi, your Python loop can be paused for milliseconds at any time while the kernel handles Wi-Fi, the SD card or another program. That is fine for dashboards and cameras, but not for generating steady step pulses for a stepper motor, reading a fast encoder without missing counts, or bit-banging a protocol with microsecond timing. Software PWM on Pi GPIO pins visibly jitters.
The common professional pattern is to split the work. A microcontroller such as a Raspberry Pi Pico (RP2040) or Pico 2 (RP2350) handles everything time-critical: motor pulses, encoder counting, fast ADC sampling, safety limits and watchdogs. Its PIO blocks can generate or read precise waveforms with no CPU involvement at all. The Pi handles everything Linux is good at: networking, storage, the camera, a user interface and AI. If the Pi crashes or reboots, the Pico keeps motors safe.
Connect them by USB, which gives power and a serial port (/dev/ttyACM0 on the Pi), or by UART pins with a shared ground. Define a simple, robust message protocol: one line of text or JSON per message, such as SET speed=120 or a reply POS 1534, with a checksum or at least a length check, and acknowledge every command. The Pico should treat silence as a fault: if it receives no message for, say, 500 ms, it stops the motors. On the Pi, use pyserial with a timeout and reconnect if the port disappears. Use a udev rule or /dev/serial/by-id/ paths so the device name stays the same after reboots.
Split the work by timing
A Raspberry Pi running Linux is good for networking, storage, a web page and heavy processing, but it cannot promise a task will run at an exact microsecond. A Pico runs one program with predictable timing, so it handles motor pulses, fast sampling and tight control loops. Let the Pico do anything with a hard time limit, and the Pi everything else.
Connect them with USB serial, UART or SPI. USB serial is the simplest, and it also powers the Pico.
Design the message protocol
Choose a clear format, such as one line of text or a short binary frame with a start byte, a length, the data and a checksum. Make commands simple, such as speed or position, and send status back at a steady rate. The Pico should keep working safely if messages stop: add a timeout, so that if no command arrives for a moment it stops the motors.
Do not rely on the Pi to send every control step. Send targets, and let the Pico run the fast loop.
Develop and debug
Test the Pico alone from a serial terminal before connecting the Pi. Log both sides with timestamps, and handle a partial or damaged line without crashing. Keep the grounds connected and use level-compatible pins, which are 3.3 V on both.
- Running a hard-timed control loop on the Linux side.
- Not stopping the actuators when the messages from the Pi stop.
- Sending every control step over serial instead of a target.
Practise this lesson with Pro
The explanation above is free for everyone. The practice for Advanced lessons in every domain are part of the Pro plan at ₹999 a month.
With Pro you also get: a worked example, a hands-on task, a 3-question check.
