Skip to content

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.

1
2
3
4
# Check both the kernel architecture and user-space width.
uname -m
getconf LONG_BIT
cargo --version

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.

1
2
3
4
5
rust_pi_work=$(mktemp -d "$HOME/pi-rust-start.XXXXXX") &&
  cd "$rust_pi_work" &&
  cargo new --edition 2024 pi-rust-probe &&
  cd pi-rust-probe &&
  cargo add 'rppal@=0.22.1'

Keep the project path. In the new project, replace src/main.rs with:

1
2
3
4
5
6
7
8
use rppal::system::DeviceInfo;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let device = DeviceInfo::new()?;
    println!("Board: {}", device.model());
    println!("SoC: {}", device.soc());
    Ok(())
}
cargo run --locked --bin pi-rust-probe

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:

Board: Raspberry Pi 4 B
SoC: BCM2711
1
2
3
# Compare with the name from Linux's device tree.
tr -d '\000' < /proc/device-tree/model
printf '\n'

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.

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 (physical pin 18) → 1 kΩ resistor → LED anode
LED cathode → GND (physical pin 20)

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:

mkdir -p src/bin

Save this as src/bin/blink.rs:

use rppal::gpio::Gpio;
use std::thread;
use std::time::Duration;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let gpio = Gpio::new()?;
    let mut led = gpio.get(24)?.into_output_low();
    let interval = Duration::from_millis(300);

    for _ in 0..3 {
        led.set_high();
        thread::sleep(interval);
        led.set_low();
        thread::sleep(interval);
    }

    Ok(())
}

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:

cargo run --locked --bin blink

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:

1
2
3
4
5
sudo apt install gcc-aarch64-linux-gnu libc6-dev-arm64-cross file
rustup target add aarch64-unknown-linux-gnu
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \
  cargo build --locked --release --target aarch64-unknown-linux-gnu --bin pi-rust-probe
file target/aarch64-unknown-linux-gnu/release/pi-rust-probe

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.

Donate