Rust on Raspberry Pi: Read the Board Model and Blink an LED¶
Build a small Rust program directly on a Raspberry Pi 4 or 5 running 64-bit Raspberry Pi OS. First print the board model with RPPAL and no extra hardware. Then use the same Cargo project to blink an indicator LED or build an ARM64 binary on another computer.
| What you want to do | Where to start |
|---|---|
| Install Rust or learn the language | Install Rust and run your first Cargo project |
| Try a Raspberry Pi-specific crate | Read the board model |
| Control a GPIO output | Blink an LED three times |
| Build on an x86-64 Linux computer | Cross-compile for 64-bit Raspberry Pi OS |
| Write firmware for a Raspberry Pi Pico | Start with the Embedded Rust Book. Pico firmware has a separate target and typically uses no_std. |
Read the board model with RPPAL¶
Run these commands on the Pi, either in its terminal or through SSH. The exercise needs Cargo, a working native linker and internet access for its first dependency download. Start with the Rust setup lesson if cargo cannot build a basic program yet.
This example uses aarch64 with 64-bit user space. Create a fresh project in your home directory and add the version of RPPAL used here.
Keep the project path. In the new project, replace src/main.rs with:
Cargo builds and runs the program. The --locked flag keeps the dependency versions resolved in Cargo.lock. DeviceInfo::new() reads model information exposed by Linux. The ? returns an error to the caller if the board cannot be identified. You can check the board's raw device-tree name separately. RPPAL DeviceInfo reference
On a Pi 4 Model B, the program printed:
This step works without an LED or a GPIO permission change. A successful build on a different computer can still give an unknown-model error at runtime there. Run the binary on the Pi you want to identify.
Blink an LED three times¶
This optional step switches a physical output. Use a spare GPIO24 on a Pi with a 40-pin header, a low-current LED, a 1 kΩ series resistor, and a breadboard. Shut down and disconnect power before wiring. Confirm GPIO24 is free of other circuitry and software uses. If you have a HAT or another project on this pin, finish the read-only example and use a spare board for this exercise.
GPIO24 is the number used by the program. Match the physical positions on the header before power-up. The GPIO uses 3.3 V logic. Keep the circuit away from the 5 V pins. See the LED wiring walkthrough and Raspberry Pi pin layout.
In the same project, create a directory for the second binary:
Save this as src/bin/blink.rs:
Use cargo build --locked --bin blink to compile without changing any pin. When the powered-off wiring check is complete, reconnect power and run as your regular Pi user:
Expect three flashes and automatic exit. The pin starts low and each flash ends low. RPPAL restores the pin mode on ordinary scope exit by default. A forced process exit can skip that cleanup. Check for other software using the pin before you run. RPPAL GPIO reference
| Symptom | What to check |
|---|---|
| Cargo cannot find a linker | Complete the native toolchain setup. |
| RPPAL cannot identify the model | Run on the Raspberry Pi OS host and check the device-tree name and supported boards. |
| GPIO access is denied | Check your login's groups with id -nG and the OS's GPIO permissions. Raspberry Pi GPIO permissions |
| The binary exits but the LED stays dark | Disconnect power and check pins 18 and 20, LED polarity, and the series resistor. |
Cross-compile for 64-bit Raspberry Pi OS¶
Cross-compilation is useful if you edit on a Linux workstation and want an executable for the Pi. Confirm 64-bit user space on the Pi first. For a Debian or Ubuntu x86-64 workstation with Rust installed, use its ARM64 GNU linker and the stable Rust Linux target. Rust ARM64 Linux target
Run this on the workstation from a copy of the project containing Cargo.toml, Cargo.lock and the source files:
file should identify an AArch64 ELF executable. Transfer it to a directory you control using your existing SSH connection. Run ./pi-rust-probe in that directory on the Pi. If it reports a missing GLIBC_ version, build against a compatible target system or return to the native build. An ARM64 target determines the CPU and ABI. The selected linker and libraries still determine runtime compatibility. Cargo linker configuration
Continue learning Rust¶
Can you explain why the board probe needs Linux but wiring is only part of the blink example? Can you point to GPIO24 on the header without using physical pin 24?
Follow the Rust course for ownership, error handling and concurrency. If you already know Cargo and want a larger no-wiring project, build the Raspberry Pi system status tool.