Rust and Pico 2: Building and Flashing Your First Embedded Application

The Raspberry Pi Pico 2 represents an exciting evolution in microcontroller development, and Rust provides an excellent foundation for building reliable embedded applications. In this tutorial, we’ll walk through setting up your development environment and creating your first Rust application for the Pico 2.

What You’ll Need

Step 1: Environment Setup

Clone the Pico SDK

git clone https://github.com/raspberrypi/pico-sdk

Set Environment Variables

export PICO_SDK_PATH=/path/to/pico-sdk
echo 'export PICO_SDK_PATH=/path/to/pico-sdk' >> ~/.bashrc
source ~/.bashrc

Install picotool

git clone https://github.com/raspberrypi/picotool

Install Dependencies

On macOS:

brew install libusb pkg-config cmake

On Linux:

sudo apt install libusb cmake

Build picotool

cd picotool
mkdir build
cd build
cmake ../
make

This will create the picotool executable that we’ll use to flash our applications.

Step 2: Adding Rust Targets

rustup target add thumbv8m.main-none-eabihf

Step 3: Building and Blinking the LED

Clone the Library

git clone https://github.com/thejpster/rp-hal-rp2350-public
cd rp-hal-rp2350-public

Build the Example

cargo build --example pwm_blink --target thumbv8m.main-none-eabihf --all-features

Step 4: Connecting and Verifying Your Pico 2

Connect Your Pico 2

  1. Hold down the BOOTSEL button on your Pico 2
  2. While holding BOOTSEL, connect the USB cable to your computer
  3. Release the BOOTSEL button

Check Device Information

picotool info -d

Step 5: Flashing Your Application

Option 1: Direct Flash with picotool

picotool load -t elf ./target/thumbv8m.main-none-eabihf/debug/examples/pwm_blink

Option 2: Convert to UF2 Format

picotool makeuf2 -o output.uf2 ./target/thumbv8m.main-none-eabihf/debug/examples/pwm_blink

Then drag the output.uf2 file onto your Pico 2’s bootloader drive.

Step 6: Rebooting and Testing

Reboot Your Pico 2

picotool reboot

You should now see the LED on your Pico 2 blinking!

Understanding the Example

The PWM blink example uses Pulse Width Modulation to control LED brightness, creating a smooth breathing effect rather than just turning the LED on and off. This demonstrates how Rust can efficiently control hardware peripherals on the Pico 2.

Next Steps

Now that you have a working Rust application on your Pico 2, you can:

  1. Explore More Examples: The rp-hal-rp2350-public repository contains many examples
  2. Read Sensors: Try connecting and reading from various sensors
  3. Communicate: Experiment with UART, SPI, or I2C communication
  4. Create Custom Applications: Build your own embedded projects

Useful Resources

Conclusion

You’ve successfully set up a complete Rust development environment for the Raspberry Pi Pico 2 and flashed your first application! This foundation will serve you well as you explore more advanced embedded programming concepts.

The combination of Rust’s memory safety guarantees and the Pico 2’s powerful dual-core ARM Cortex-M33 processor makes for an excellent platform for embedded development. Whether you’re building IoT devices, robotics projects, or custom hardware controllers, you now have the tools to get started.

Happy coding! 🦀