Beaglebone LESSON 8: Read Button State from Python


In this tutorial we will see how to read digital values from the GPIO pins. We will be doing digital reads, which means we will be limited to “HIGH” or “LOW” readings. This is a 3.3 volt system, so we need to make sure that the “HIGH” applied signal is 3.3 volts.

Our pinout from LESSON 1 shows which pins are suitable for digital reads.

Beaglebone Black Pinout
Default Pin Configuration for the Beaglebone Black Rev. C.

It is the green GPIO pins which we can use for digital reads. In this lesson we will demonstrate the digital read technique using a simple two button circuit. In order to complete this lesson, you should go ahead and build this circuit.

Beaglebone Button
Example of Simple Beaglebone Black Button Circuit

Notice we are using Pin 1 on Header P9 as the ground and Pins 11 and 13 on header P9 s the input pins. Also note the pulldown resistors are 1000 Ohm. It is important to use at least this much resistance. If you do not have 1,000 Ohm resistors, using something larger NOT something smaller.

Once you have the circuit set up we are ready to begin programming.

First up, you need to import the GPIO library. If you have the latest version of Debian Wheezy, you should have the library on your system. If you do not have it you will need to update and upgrade your operating system. To load the library, you will use the python command:

We now need to configure out pins P9_11 and P9_13 as inputs. We do this with the command:

Now to read the state of the buttons, we would use the commands:

state1 will be True if the top button is pushed, and False if it is not being pushed. Similarly, state2 will be True when the button is being pushed, and False when it is not being pushed.

We can bring these concepts together to make the following program. Play around with the program and see what all you can make it do.