Physical Computing With Rust On Raspberry Pi

Use Rust to control LEDs, Buzzers, and more with the Raspberry Pi

A couple of months ago I released the rust_gpiozero¹ crate. It is a port of the GPIO Zero² library by the Raspberry Pi Foundation. The library provides a simple interface to control GPIO devices with a Raspberry Pi. Following this, recently ported the Raspberry Pi Foundation’s Physical Computing with Python³ guide for Rust.

Why did I create the rust_gpiozero crate?

The idea behind rust_gpizero was to abstract away some of the complexities and provide a high-level interface to experimenting with GPIO pins on the Raspberry Pi using Rust. This is similar to the original python based GPIOZero library which uses the RPi.GPIO library under the hood. You can experiment with very little code. I also tried to keep a similar API to the original GPIOZero library.

Blinking an LED using GPIOZero

Generated using: carbon

Similarly, blinking an LED using rust_gpiozero

Generated using: carbon

The library is still a work-in-progress and there are many improvements which can be made.

Tutorials to accompany the crate

  • Use basic programming constructs to create simple programs
  • Use basic digital, analogue, and electromechanical components
Source: Raspberry Pi Foundation

I adapted the guide to make Physical Computing with Rust. The resource is aimed at those who have some basic programming skills and know some Rust. Ideally, it is recommended to have completed at least the first three chapters of The Rust Programming Language Book.

Source: Physical Computing with Rust

Porting the guide was an easy process. I used mdbook⁶ which allows you to generate online books from Markdown files. The Raspberry Pi Foundation already had the content in the format required by mdbook with all content for a particular step in a separate Markdown file. I simply had to create the SUMMARY.md file and structure the project accordingly. Next, I modified the content for specific steps required to experiment using Rust.

The guide does not yet cover all the steps in the original resource.

Roadmap

  1. Use the Linux GPIO Character Device API instead of sysfs_gpio which has been deprecated since linux 4.4. Paul Osborne is already working on the rust interface⁸.
  2. Add background tasks. For e.g, blinking an LED should not block the thread
  3. Implement SPI devices API

Thank you so much for reading. A big thank you goes to ashley williams, @terkwood and Jonathan Pallant for their invaluable feedbacks.

Enjoyed this post?

Lifelong Learner 📖

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store