Program the real world using Rust on Raspberry Pi

Program the real world using Rust on Raspberry Pi

rust_gpizero uses the Rust programming language to do physical computing on the Raspberry Pi.

A person working.
Image by : 
Opensource.com
x

Subscribe now

Get the highlights in your inbox every week.

If you own a Raspberry Pi, chances are you may already have experimented with physical computing—writing code to interact with the real, physical world, like blinking some LEDs or controlling a servo motor. You may also have used GPIO Zero, a Python library that provides a simple interface to GPIO devices from Raspberry Pi with a friendly Python API. GPIO Zero is developed by Opensource.com community moderator Ben Nuttall.

I am working on rust_gpiozero, a port of the awesome GPIO Zero library that uses the Rust programming language. It is still a work in progress, but it already includes some useful components.

Rust is a systems programming language developed at Mozilla. It is focused on performance, reliability, and productivity. The Rust website has great resources if you'd like to learn more about it.

Getting started

Before starting with rust_gpiozero, it's smart to have a basic grasp of the Rust programming language. I recommend working through at least the first three chapters in The Rust Programming Language book.

I recommend installing Rust on your Raspberry Pi using rustup. Alternatively, you can set up a cross-compilation environment using cross (which works only on an x86_64 Linux host) or this how-to.

After you've installed Rust, create a new Rust project by entering:

cargo new rust_gpiozero_demo

Add rust_gpiozero as a dependency (currently in v0.2.0) by adding the following to the dependencies section in your Cargo.toml file

rust_gpiozero = "0.2.0"

Next, blink an LED—the "hello world" of physical computing by modifying the main.rs file with the following:

use rust_gpiozero::*;
use std::thread;
use std::time::Duration;

fn main() {
    // Create a new LED attached to Pin 17
    let led = LED::new(17);

    // Blink the LED 5 times
    for  _ in  0.. 5{
       led.on();
        thread::sleep(Duration::from_secs(1));
        led.off();
        thread::sleep(Duration::from_secs(1));
    }
}

rust_gpiozero provides an easier interface for blinking an LED. You can use the blink method, providing the number of seconds it should stay on and off. This simplifies the code to the following:

use rust_gpiozero::*;
fn main() {
    // Create a new LED attached to Pin 17
    let mut led = LED::new(17);

    // on_time = 2 secs, off_time=3 secs
    led.blink(2.0,3.0);

    // prevent program from exiting immediately
    led.wait();
}

Other components

rust_gpiozero provides several components that are similar to GPIO Zero for controlling output and input devices. These include LED, Buzzer, Motor, Pulse Width Modulation LED (PWMLED), Servo, and Button.

Support for other components, sensors, and devices will be added eventually. You can refer to the documentation for further usage information.

More resources

rust_gpiozero is still a work in progress. If you need more resources for getting started with Rust on your Raspberry Pi, here are some useful links:

Raspberry Pi Peripheral Access Library (RPPAL)

Similar to GPIO Zero, which is based on the RPi.GPIO library, rust_gpiozero builds upon the awesome RPPAL library by Rene van der Meer. If you want more control for your projects using Rust, you should definitely try RPPAL. It has support for GPIO, Inter-Integrated Circuit (I2C), hardware and software Pulse Width Modulation (PWM), and Serial Peripheral Interface (SPI). Universal asynchronous receiver-transmitter (UART) support is currently in development.

Sense HAT support

Sensehat-rs is a library by Jonathan Pallant (@therealjpster) that provides Rust support for the Raspberry Pi Sense HAT add-on board. Jonathan also has a starter workshop for using the library and he wrote a beginner's intro to use Rust on Raspberry Pi, "Read Sense HAT with Rust," in Issue 73 of The MagPi magazine.

Wrap Up

Hopefully, this has inspired you to use the Rust programming language for physical computing on your Raspberry Pi. rust_gpiozero is a library which provides useful components such as LED, Buzzer, Motor, PWMLED, Servo, and Button. More features are planned and you can follow me on twitter or check out my blog to stay tuned.

Topics

About the author

Rahul Thakoor - Linux Foundation Certified System Administrator | Medical Graduate | Lifelong Learner | Developer | Maker | Volunteer