USING BUTTONS WITH GPIO
In this project, we will use GPIOs to get input from a pushbutton into our software.
I/O (Input/Output) RaspberrySTEM Cell
STRING THEORY
CALCULATIONS
Having issues? Check out the Troubleshooting Guide.

In the previous project, we saw how GPIOs can be used as outputs to control components attached to the Raspberry Pi (like our LED). GPIOs can also be used as inputs to take information from external components; that input can then be acted upon by the Raspberry Pi and appropriate actions can be taken.

GPIOs as Inputs

Much like GPIO outputs work by simply setting a voltage on the GPIO pin to either 0V or 3.3V, GPIO inputs work by reading the voltage – either 0V or 3.3V – being sent to the GPIO pin.

For example, a GPIO can be used to get the current state of a button or switch (pressed or released).  Based on the current state, the software can do what we expect when the button/switch is in that state.  


"State"

"State" is a common term using in electronics and just means the current condition or situation of a component or circuit.

For example, in the case of a button or switch, state would refer to whether the button is currently being pressed or released.  For an LED, state might refer to whether the LED was illuminated or not.


In the INTRODUCING BUTTONS project, we inserted a button into the circuit to act as a physical switch -- when pressed, the circuit was completed and when released, the circuit was open. No software was required.

But if we write software that interacts with our hardware (by reading the state of the button through a GPIO) we add a world of possiblites. For example, perhaps you want to count how many times a button is pressed in a certain time period, and take different actions based on that number?  Or perhaps you want to time how long a button is pressed and take action based on the length of the press?  Or maybe a button press needs to trigger an action in another circuit that the button isn’t attached to?

To perform more complicated operations like these, instead of wiring the button directly into the circuit, we can wire the button as an input to a GPIO.  As we discussed earlier, GPIOs can be used as inputs to take information from external components (like buttons); that input can then be acted upon by the Raspberry Pi.

GPIO inputs work by reading the voltage at the GPIO pin – either 0V (“low”) or 3.3V (“high”).

For example, a GPIO can be used to get information from a button or switch.  In the circuit design we use and will explain below, when a button is left unpressed, the voltage at the GPIO pin is “high” (3.3V); when the button is pressed, the voltage at the GPIO pin will go “low” (switch to 0V).  The GPIO will register the voltage, and the Raspberry Pi can then take action on either a pressed or released button.

To wire a button to a GPIO, this is what the circuit schematic would look like:


Reading Buttons Using GPIOs

To implement what we discuss above as an actual breadboard circuit is easy. First, we start with an empty breadboard, with power and ground wired from the Lid Connector Board, like this:


With our breadboard empty, here is how we build our circuit and then use code to get input from the button through the GPIO:


Step #1:  Place button on breadboard.

We start by placing a button on the breadboard. Remember, it is important that the button is oriented correctly -- the two independent sides of the button must sit on different connect strips, as follows:


CORRECT WRONG!!!

2.  
Step #2:  Wire one side of the button to a GPIO.

Next, we connect one side of the button (it doesn’t matter which one, as the internal components of the button are symmetrical) to a GPIO.  In this example, we’ll connect the button to GPIO14.



Step #3:  Wire the other side of the button to ground.

Finally, we connect the other side of the button to ground.


That’s it for the wiring – pretty simple, right?


Step #4: Write the code.

Now that the circuit is wired, we can write the code to get the GPIO state (low or high):



In this most basic example, we are reading the GPIO a single time (the moment the program is run) and we are printing the state of the button at that moment.  Try running the program a couple times, either holding down or not holding down the button at the moment the program is run.

You may notice that this example is similar to the GENERAL PURPOSE INPUT OUTPUT example. But in this case, we configure the GPIO as a button instead of an output:

We'll look at these concepts again in more detail in upcoming projects.

To make the program a little more interesting (and convenient!), we can insert a loop so that, instead of just reading the state of the GPIO one time, we can continually read and print the state of the GPIO for as long as the program is running. 

Here’s what that code would look like:



Run this program and then alternate between pressing and not pressing the button.  Take a look at the output window – you should see an indication of when the button is pressed and when it is released.


"while True:"

You may have noticed that we used a new programming concept above:

while True:

We'll discuss exactly how this works in a future project, but for now, just know that using this line of code will force the block of code below it to be repeated over and over, forever.


When you're done testing your button, you can stop the program by pressing the square STOP icon in the upper-left-hand corner of the code window (this icon replaced the PLAY icon while the program was running).

home | prev | next