A couple of months ago I released the
rust_gpiozero1 crate. It is a port of the GPIO Zero2 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 Python3 guide for Rust.
Why did I create the
After going through The Rust Programming language book, I decided to work on a side project to practice some of the concepts. I enjoy working with microcontrollers and Single Board Computers and making beginner tutorials. I looked for crates related to the raspbery Pi and controlling GPIO pins and came across several great ones such as
i2cdev. 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
Similarly, blinking an
The library is still a work-in-progress and there are many improvements which can be made.
Tutorials to accompany the crate
Physical Computing with Python (published under a Creative Commons license) by The Raspberry Pi Foundation is a great guide which introduces physical computing. The guide uses
GPIOZero and teaches how to interface with electronic components such as LEDs, Buzzers and Sensors. More importantly, the guide
covers elements from the following strands of the Raspberry Pi Digital Making Curriculum4:
- Use basic programming constructs to create simple programs
- Use basic digital, analogue, and electromechanical components
I adapted the guide to make Physical Computing with Rust5. 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.
Porting the guide was an easy process. I used
mdbook6 which allows you to generate online books from Markdown files. The Raspberry Pi Foundation already had the content7 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.
As I learn more about Rust, I will continue to improve the
rust_gpio crate and add corresponding tutorials to the Physical Computing with Rust guide. You can contribute to the project by sending PRs, commenting on the code or providing feedback. I intend to learn a lot from the community and any input is much appreciated. Finally, here are some things I wish to implement eventually:
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 interface8.
Add background tasks. For e.g, blinking an LED should not block the thread
Implement SPI devices API
- https://crates.io/crates/rust_gpiozero [return]
- https://gpiozero.readthedocs.io/ [return]
- https://projects.raspberrypi.org/en/projects/physical-computing [return]
- https://curriculum.raspberrypi.org/ [return]
- https://rahul-thakoor.github.io/physical-computing-rust/ [return]
- https://github.com/rust-lang-nursery/mdBook [return]
- https://github.com/raspberrypilearning/physical-computing [return]
- https://github.com/posborne/rust-gpio-cdev [return]
Want to get updates on these blog posts?
Enjoyed this post?Buy me a coffee