Hello RPI Pico World - or Blinky
Let's update the hello_pico_world
project from the previous chapter and build a "blinky" project.
We'll start of with the blinky
example from the embassy
crate. This example will toggle the LED on the Pico board every 1s.
main.rs
#![no_std] #![no_main] use defmt::*; 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::init(Default::default()); let mut led = Output::new(p.PIN_25, Level::Low); loop { info!("led on!"); led.set_high(); Timer::after_secs(1).await; info!("led off!"); led.set_low(); Timer::after_secs(1).await; } }
Run the project with:
$ cargo run --release
And you should see the LED on the Target board blink every 1s.
The code is pretty simple. We initialize the Pico board with embassy_rp::init(Default::default())
and create a new
Output
pin with the LED pin p.PIN_25
. We then loop forever and toggle the LED on and off every 1s.
GP25
is the onboard LED on the Pico board, as you can see in
the Pico pinout.
If the above code works as expected, build a breadboard circuit with a LED and a 150Ω resistor and connect it to
pin GP16
on the Pico board. Don't remove any of the programming wires!
Schematic:
Update the main.rs
file to look like this:
main.rs
#![no_std] #![no_main] use defmt::*; 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::init(Default::default()); let mut led = Output::new(p.PIN_16, Level::Low); loop { info!("led on!"); led.set_high(); Timer::after_secs(1).await; info!("led off!"); led.set_low(); Timer::after_secs(1).await; } }
Run
$ cargo run --release
If everything is wired up correctly, you should see the LED blink every second. The example did hardly change, we just
changed the pin from GP25
to GP16
, which is the pin we connected the LED to.
The embassy
crate is a great way to write (async) code for embedded devices. The abstraction it provided makes coding
for embedded devices much more like "regular" Rust code.