Pibrella Button

Pibrella has a built in button. We can use it to control things attached to the board.

Make sure Python 3 on the Raspberry Pi is updated and that you have the latest Pibrella library first.
Open idle3 for python as an administrative user
You can access both these commands from the Pibrella Update Pi page.
import pibrella

while True:
    if pibrella.button.read() ==1:
        pibrella.light.amber.on()
        pibrella.light.red.off()
    elif pibrella.button.read() ==0:
        pibrella.light.red.on()
        pibrella.light.amber.off()
#Import the Pibrella library of commands

#Note the small case w and capital T. For other while commands see here
#If the button is equal (==) to 1 it is making a circuit and is on
#Turn amber light on 
#Turn red light off
#If the button is equal (==) to 0 it is not making a circuit and is off
#Turn red light on
#Turn amber light off
Please note you can't use an if else as you can't define what state else is.
elif allows us to define what state the button is in.
We could use the if on its own to define what happens when the button is pressed.
This example uses lights but other outputs such as the buzzer or motors could be controlled.

If the button was attached to an input A, B, C or D it could be read like this
if pibrella.input.a.read() ==1: #If the button attached to a is equal (==) to 1 it is making a circuit and is on
Comments