Making a switch

With a switch, a single press and release on the button would turn the LED on, and another press and release would turn it off again.

  • Modify the examples/ file so that it looks like this:

    extern crate rust_gpiozero;
    use rust_gpiozero::*;
    use std::thread::sleep;
    use std::time::Duration;
    fn main() {
    // Tell the Pi which GPIO pin you are using
    let mut led = LED::new(17);
    // Create a button which is attached to Pin 22
    let button = Button::new(22);
  • led.toggle(); switches the state of the LED from on to off, or off to on. Since this happens in a loop the LED will turn on and off each time the button is pressed.