Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Teaching Physical Computing with Raspberry Pi and Python. Join the course to learn more.

Reflection: Selection in physical computing

Just like last week, in this step you are going to reflect on what you achieved in the project you just completed. You are going to think about buttons and selection in physical computing.


Buttons and switches are a class of input component. They allow a user to have some control over a circuit or to send signals to a computer.

What type of data do we get from our button? A button can either be pressed or not; it can have one of two values, so a button returns Boolean data. How can programs react to this?


Programming is governed by three universal principles: sequence, selection, and iteration. In this course you will use all three of them in your projects, but for now I would like to focus on selection.

Selection refers to a process in your code that changes which instructions are executed; you don’t necessarily want every piece of code to run each time your program does. You want to select a specific part of your code to run, based on conditions. The most recognisable form of selection is the ubiquitous if statement:

if temperature < 10:
    print("It is cold! Wear a jacket")
elif temperature >= 10 and temperature < 25:
    print("Maybe a jumper would be appropriate")
elif temperature >= 25 and temperature < 40:
    print("It's t-shirt weather")
    print("Maybe stay inside and make sure you drink a lot of water")

These constructs use a condition, set by the programmer, to control whether or not the pieces of code contained in them execute.

  • If the condition is True, the code executes, and if it is False, those lines of code are skipped
  • You can also use an elif structure to check another condition after an if statement; the second condition will only be checked if those preceding it are False
  • An else statement will run the instructions contained within when the if and elif (if you have them) statements proceeding it return False

Making use of these structures allows our programs to behave differently under different circumstances, depending on the input.

Using buttons for selection

The GPIO Zero documentation for buttons can be found here. I will be walking you through some of the other methods and properties of buttons in this step, and I will link to the relevant points in the documentation.

If statements

In the previous step, you used an if statement to check the state of the button and to turn the LED on when the button was pressed. You did this with the is_pressed property of the button, accessing the raw Boolean data.

In the previous example, your code ran only when the button was pressed; now I would like you to switch it around.

Create another new Python file and copy your original light switch program into it.

Can you edit the program so that the light is on by default, and only turns off when the button is pressed?


Events in computing are detectable occurrences that we can react to in our programs. The button you have attached has two states, and an event is triggered when those states change. GPIO Zero allows you to use two events for the button. The pressed event is triggered when the switch inside the button is closed, and the released event is triggered when the switch opens again.

If there is a specific method you would like to call when an event is triggered, you can assign one to the event. To do this, you use when_pressed and when_released.

  1. Create a new file in Mu.

  2. Add the following to your program:

    from gpiozero import Button, LED
    btn = Button(4)
    led = LED(17)
    btn.when_pressed = led.on
  3. Save and run your code. Press the button.

    You should see the LED turn on. You just used event-driven programming to control an LED!

    Note: You may have noticed that when I set when_pressed to led.on, I did not add brackets to the on method. This is important. When setting a reaction to the when_pressed event, I am providing a reference to the method, not calling it. The event only needs the name of a function to call; when it is triggered, it will find the method and call it. If you include brackets with the on method it is called immediately.

  4. Use the when_released button event to turn the LED off again.

Waiting for events

Another way you can use events is to have your program halt until an event is triggered.

This is done with the wait_for_press() and wait_for_release() methods. Each will wait for the corresponding event before the program continues.

while True:

Buttons make things happen

You have now learnt how buttons can be used with GPIO Zero to instruct your programs to do things.

Use the comments section to share some of your ideas for things that you could make your Raspberry Pi do when a button is pushed or released.

Share this article:

This article is from the free online course:

Teaching Physical Computing with Raspberry Pi and Python

Raspberry Pi Foundation