In this post we run through the basics of controlling your beaglebone black GPIO (General Purpose I/O) pins. It is important to note that many of the pins are used by the beaglebone black for various processes at different times. This can make for a headache when trying to wire things up. Below is a table that maps out the GPIO pins and should help you use the ones that are always available (all of the pins with red boxes are unavailable). It is also a good reference for the pin layouts.
**For the above table- The default mode is 7.
The beaglebone black GPIO pins can be used, as the name implies, as inputs or outputs. Below is a simple LED circuit to build on your breadboard to practice control over a GPIO. Just about any common LED will work here, and the transistor we are using is an NPN 2222A.
The 3.3V comes from both GPIO pins 3 and 4, and in this example we are using GPIO 15 (P9_24) from the P9 header **Do not confuse GPIO_15 for Pin 15, GPIO_15 is Pin 24. In the programming we use the Pin number not the GPIO’s number. So to begin programming using python, we must first download the python library to the Beaglebone Black.
Run the following commands in the beaglebone black’s terminal:
if the error message below comes up, run the command easy_install -U distribute
The required version of distribute (>=0.6.24) is not available, and can't be installed while this script is running. Please install a more recent version first, using 'easy_install -U distribute
Now Run the command:
This command puts the system into interactive programming mode, meaning that as soon as you type a line of code it is immediately executed. This is good for this type of project where we are turning an LED on and off, we will go through more advanced forms of programming in a separate post. Now copy and past the following code into your terminal:
import Adafruit_BBIO.GPIO as GPIO GPIO.setup("P9_24",GPIO.OUT)
This portion of our code does 2 things:
- imports the Adafruit GPIO information for easy programming and setting up the pins
- sets up the P9 header pin 24 as the output to turn the LED on and off through the transistor
Your LED should have been on up until this point and turned off once the last line was entered. If not, double check that your circuit is wired correctly (and make sure you hit [enter] to submit each line) Now write in the python terminal:
That should turn your LED on. Now try to input:
This should turn your LED off. You can toggle your LED on and off by running those 2 commands.
Congratulations! You now know the basics of using python to control your beaglebone black GPIO pins, allowing you to interact with the physical world. You’ve completed your first step toward taking over the world with robots.
Check out part 2 and 3 in this three part series on GPIO control