I should be getting my Rover 5 Motor Driver Board to go with my Rover 5 Chassis within the next day or so. This means I can start (for the third time) building my first robot. Hopefully this time I will have more success then my first two attempts. Before the new board arrives I wanted to write a post that will show how to use Python with the Adafruit library to read the state of a button.
What we need:
For the experiments in this post we will need a breadboard, a push button, four solder-less jumpers and a 10K ohm resister. All of these parts come with Make’s Getting started with the BeagleBone Black kit that I mention in my Introduction post. The 10K ohm resistor is the resister with the brown-black-orange-(gold or silver) bands. If you want to find out the value of your resistors but you are not familiar with the resistor color codes, I would recommend using this digikeypage.
Rather than rehashing the information about the BeagleBone’s Expansion Headers here, I recommend that you read the “Our BeagleBone” section of my last post “Playing with LEDs” if you are not familiar with the expansion headers.
Lets jump right in and connect our external components to our breadboard. Keep in mind, that whenever you connect items to your BeagleBone Black, it is a good idea to disconnect the power to the BeagleBoard first.
Using a solder-less jumper, connect the ground rail of your breadboard to pin 1 of the P9 expansion header. Then use another solder-less jumper to connect the power rail of your breadboard to pin 3 of the P9 expansion header. This will connect your breadboard to both power and ground.
Now lets add the push button to the breadboard. You will want the button to straddle the middle section as show in the image below. Using a solder-less jumper, connect the power rail of your breadboard to one end of the button. Next connect the same end of the button to the ground rail of your breadboard using the 10K pulldown resistor. Finally connect the other end of the button to pin 11 of the P8 expansion header. The connections should look like the following image but hopefully you did a neater job hooking up the wires then I did.
Power up and writing code:
New lets power up our BeagleBone and write some code to monitor our button. This first example can be used to check the current state of the button. Create a file called “button.py” and add the following code.
import Adafruit_BBIO.GPIO as GPIO
print "The button was pressed"
Now run the script with this command:
If everything is connected correctly, when you press the button you should see “The button was pressed” message appearing on the screen. If you hold the button down you will see lots of “The button was pressed” messages.
The script starts off by importing the Adafruit_BBIO.GPIO and time libraries that are needed. The next line sets pin 11 on expansion header P8. You saw in the last post “Playing with LEDs” that when we set the pin to GPIO.OUT we wrote data to the pin. For this post we want to read data from the pin so we set the pin to GPIO.IN.
We then create an endless loop and within that loop we check pin 11 of expansion header P8 using the GPIO.input function. This function returns true if the button is pressed and false if the button is not being pressed. We then pause for one-tenth of a second and check again.
This script works great if you want to check the current state of a button but what if we wanted to wait for the button to be pressed. We could create an endless loop and then break out of it when the GPIO.input() function returned true but that is kind of a messy if you ask me. Luckily there is a better way. Try this next script; it waits for the status of the button to reach a certain state and them moves on.
import Adafruit_BBIO.GPIO as GPIO
print "Button Pressed"
print "Button Released"
The GPIO.wait_for_edge() function waits for the state of the button to change. In this script we wait for the button to be pressed (GPIO.RISING) and then waits for the button to be released (GPIO.FALLING). You could also use GPIO.BOTH to wait for any change in the button’s state.
Hopefully my motor driver board will arrive in the next day or so and I can begin trying to get a robot working. Whether the project is a success or another failure, I will write a post explaining what I did and how it worked.