[Logo for The C Shore Landing Page]
The C Shore

Extras for Module2: Button Logic

Table of Contents

Extras for Module 2: Button Logic

Extra: 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 your code so that it looks like this:

     from gpiozero import LED, Button
     from time import sleep
    
     led = LED(17)
     button = Button(2)
    
     while True:
         button.wait_for_press()
         led.toggle()
         sleep(0.5)

    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.

  • It would be great if you could make the LED switch on only when the button is being held down. With GPIO Zero, that’s easy. There are two methods of the Button class called when_pressed and when_released. These don’t block the flow of the program, so if they are placed in a loop, the program will continue to cycle indefinitely.

  • Modify your code to look like this:

     from gpiozero import LED, Button
     from signal import pause
    
     led = LED(17)
     button = Button(2)
    
     button.when_pressed = led.on
     button.when_released = led.off
    
     pause()
  • Save and run the program. Now when the button is pressed, the LED will light up. It will turn off again when the button is released.

  • Enter the following code:

     from gpiozero import Button
    
     button = Button(21)
    
     while True:
         print(button.is_pressed)

    In GPIO Zero, you create an object for each component used. Each component interface must be imported from the gpiozero module, and an instance created on the GPIO pin number to which it is connected.

  • Save and run the code.

  • In the shell it will be constantly printing False. When you press the button this will switch to True, and when you let go it will return to False.

    button.is_pressed is a property of the button object, which provides the state of the button (pressed or not) at any given time.

  • Now return to the code window and modify your while loop to show the following:

     while True:
         if button.is_pressed:
             print("Hello")
         else:
             print("Goodbye")
  • Run the code again and you’ll see “Hello” printed when the button is pressed, and “Goodbye” when the button is not pressed.

  • Modify the loop again:

     while True:
         button.wait_for_press()
         print("Pressed")
         button.wait_for_release()
         print("Released")
  • When you run the code this time, nothing will happen until you press the button, when you’ll see “Pressed”, then when you let go you’ll see “Released”. This will occur each time the button is pressed, but rather than continuously printing one or the other, it only does it once per press.

Extra: Add an LED

Now you’ll add an LED into the code and use GPIO Zero to allow the button to determine when the LED is lit.

  • In your code, add to the `from gpiozero import... line at the top to also bring in LED:

     from gpiozero import Button, LED
  • Add a line below button = Button(21)to create an instance of anLED` object:

     led = LED(25)
  • Now modify your while loop to turn the LED on when the button is pressed:

     while True:
         button.wait_for_press()
         led.on()
         button.wait_for_release()
         led.off()
  • Run your code and the LED will come on when you press the button. Hold the button down to keep the LED lit.

  • Now swap the on and off lines to reverse the logic:

     while True:
         led.on()
         button.wait_for_press()
         led.off()
         button.wait_for_release()
  • Run the code and you’ll see the LED stays on until the button is pressed.

  • Now replace led.on() with led.blink():

     while True:
         led.blink()
         button.wait_for_press()
         led.off()
         button.wait_for_release()
  • Run the code and you’ll see the LED blink on and off until the button is pressed, at which point it will turn off completely. When the button is released, it will start blinking again.

Extra: Some additional Button class properties

  • held_time

    • The length of time (in seconds) that the device has been held for. This is counted from the first execution of the when_held event rather than when the device activated, in contrast to active_time. If the device is not currently held, this is None.
  • hold_repeat

    • If True, when_held will be executed repeatedly with hold_time seconds between each invocation.
  • hold_time

    • The length of time (in seconds) to wait after the device is activated, until executing the when_held handler. If hold_repeat is True, this is also the length of time between invocations of when_held.
  • is_held

    • When True, the device has been active for at least hold_time seconds.
  • is_pressed

    • Returns True if the device is currently active and False otherwise. This property is usually derived from value. Unlike value, this is always a boolean.
  • value

    • Returns 1 if the button is currently pressed, and 0 if it is not.
  • when_held

    • The function to run when the device has remained active for hold_time seconds.

    • This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that activated will be passed as that parameter.

    • Set this property to None (the default) to disable the event.

  • when_pressed

    • The function to run when the device changes state from inactive to active.

    • This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that activated will be passed as that parameter.

    • Set this property to None (the default) to disable the event.

  • when_released

    • The function to run when the device changes state from active to inactive.

    • This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that deactivated will be passed as that parameter.

    • Set this property to None (the default) to disable the event