Skip to 0 minutes and 3 secondsIt's time for you to wire up an LED and push button to your bread board and Raspberry Pi. Make sure that your button has been pushed into the bread board across the dividing line and make sure it's pushed all the way in so that these metal pins hit the bottom of the bread board. When you press the button connected to pin 4, you change the state of that pin from low to high. Now, let's write a Python program to detect the changing state. First what we need to do is import the classes for LED and button.
Skip to 0 minutes and 31 secondsAnd we did this last week where we had LED, so to add another class, I'm just going to use a comma and type the word button with a capital B. Next, I need to create an object for the button. So I'm going to use some shorthand here. btn equals Button with a capital B. And then inside parentheses, I'm going to put the pin number that I use to connect the button, which is 4. Next, I'm going to create a while true loop. And inside, I'm going to set a condition for when I press the button.
Skip to 1 minute and 4 secondsSo to do this, I type if, use my shorthand for button, .is_pressed:.
Skip to 1 minute and 14 secondsThe next thing I need to do is create a print statement or something that will happen when we press the button so I can test it. So I'm going to type print, open parentheses, quotation for my string, button is pressed, and then we'll close my quotes and my parentheses. And then we need to add a pause, because the whole time, it's going to be checking the state of the button, waiting for that change in state. So I'm going to use sleep. And I'm just going to use fraction of a second in my code. Now let's run the program and press the button.
Skip to 1 minute and 54 secondsYay, my program works. If you press the button, it should say, button is pressed and it'll print it to the screen. Now, it's time for another challenge. Using your experience with LEDs from last week, edit your program to turn on the LED when the button is pressed and turn it back off after five seconds. Share a link to your code in the comments below. And if you get stuck, be sure to ask questions and help each other out. Good luck.
Project: Light switch
Now that you have learnt about input devices and the types of data they return, I would now like you to use one in a physical computing project. There are few input devices as ubiquitous as the push button.
The highs and lows
Look at the circuit below. When the button is pushed, the LED turns on because the current can flow across it. The LED is being sent high when the button is pushed, and low when the button is released.
It’s useful to talk about highs and lows when integrating circuits with computers. This is because we can think of a high as representing the number 1, and a low as representing the number 0. By using this representation of high and low as 1 and 0, we have a way of communicating with the computer using circuits.
Before you continue, wire an LED on a breadboard to your Raspberry Pi in the same way as you did in week one. I’ll use pin 17, but you can choose any available pin.
Wiring up your button
You can use physical buttons and switches to send signals to your Raspberry Pi and trigger all kinds of events.
In the diagram below, a button has been wired up to a Raspberry Pi on GP4 and a ground pin. You’ll notice that the button has been pushed into the breadboard across the dividing line. Set up your circuit in the same way as shown.
In this case, GP4 starts off high, and when the button is pressed and the circuit closed, the pin is sent low, as it is connected to the ground pin. If you want a more detailed explanation of why this is, have a look at this Wikipedia article on pull-up and pull-down resistors, but it isn’t important for what you are going to do. All you need to understand is that when the button is pushed, the state of pin 4 is changed.
Detect button presses
So how can you detect the state of a pin? Again, you can use the gpiozero library to help you.
Create a new Python program and add the following lines:
from gpiozero import LED, Button from time import sleep btn = Button(4) led = LED(17)
This will import the
LEDclasses from GPIO Zero, and also the
sleepmethod from the
timemodule. The last two lines create an object for each of the GPIO Zero classes,
LED, to allow the program to interact with the devices on the given pins. I’ve used pin
4for the button and pin
17for the LED.
You want to detect whether the button has been pressed, so you need to add an
ifstatement that uses the
is_pressedproperty of the
Buttonclass. If the button has been pressed, the
is_pressedproperty will be
True, and if the button is not pressed, it will be
Add the functionality described above to your program.
while True: if btn.is_pressed: print("You pressed me") sleep(0.1)
You need to add a small
sleepat the end of the loop, because the
Run your program and press the button to see what happens.
What happens if you hold the button down?
Creating a light switch
You have just created a switch that will print a statement when the button is pressed.
Using your experience with LEDs from last week, edit your program so:
- The LED is turned on when the button is pressed.
- The LED turns back off after five seconds, creating an energy-saving feature.
Upload your code to a site such as Pastebin and post a link to the code in the comments. Make sure to try it yourself before looking at other learners’ solutions. If you have any questions, you can leave a comment below and our facilitation team will help you.