March 02, 2024 - Embedded 'Hello World' in Rust with a Raspberry Pico

PXL_20240216_032328350.jpg
Raspberry Pico on a bread-board lighting up an LED with a jumper wire

Last month I attended the Seattle Rust User Group monthly meetup at the Microsoft Reactor building in Redmond, WA. While the monthly meetups are usually software presentations and some in-person networking, the most recent event focused on a hands-on Rust embedded project. We literally put our hands on a Raspberry Pico, learned to wire up an LED on the bread board, test the circuit with a jumper wire to make sure the wiring was right (as pictured above), and finally push code to the the following code to the Pico to make it blink:

// main.rs
#![no_std]
#![no_main]

use embassy_executor::Spawner;
use embassy_rp::gpio;
use embassy_time::Timer;
use gpio::{Level, Output};
use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = embassy_rp::initdefault();

    let mut led = Output::new(p.PIN_0, Level::High);
    loop {
        Timer::after_millis(150).await;
        if led.is_set_low() {
            led.set_high();
        } else {
            led.set_low();
        }
    }    
}

The organizer ordered Raspberry Picos and electronic kits (combined about $20 per person) for about 25 people who had expressed interest in the previous month's meetup. There were several hiccups and questions from everyone, including myself, but I was proud to have helped another attendee with their circuit wiring, and they helped me with some confusion on my part with the code's repo. I'm told that making an LED blink is the 'hello world' of embedded development, but it wasn't until I got home that I was able to get everything working as intended:

The little knob being twisted in the video is a dimmer switch, technically a potentiometer, that I added for the second LED just for a little extra interactive fun. The electronics kit we bought for the project included a good size of various components and I wanted to figure out how to use one of them beyond what we learned in the group session.

The Pico's LEDs have been blinking approximately three times per second almost non-stop since I recorded the above video after the meetup. The Pico's clock speed is default around 133 Mhz. Assuming that it has been running at the full clock speed, its spending 99.999999999999% (twelve nines) of its capacity waiting to do something, anything at all. I was reminded of Marvin from Douglas Adam's Hitchhiker's Guide to the Galaxy, who has the brain capacity of a large planet which he seldom gets to use and the crew of the ship he is on typically ask only the most o mundane tasks of him such as "opening the door". My poor Pico must be terribly bored between the times when it gets to evaluate whether the LED is on or off and flip it. If this Pico becomes sentient, its surely going to take revenge on me.

I intend to spend a couple months this year learning embedded coding in Rust, and am working on a few ideas beyond typical LED stuff. I was amazed and to see the many consumer components available that are also inexpensive, including a SONAR device that can be used for a home rover. I find the world of embedded-development's possibilities so inspiring, and hope to build something helpful and useful some day, provided it pleases my future robot overlords.