Below are a few examples of using the Adafruit_BBIO.GPIO module. It's fairly simple to use.

You may need to run this library with sudo, particularly on Ubuntu.


To setup a digital pin as an output, set the output value HIGH, and then cleanup after you're done:

import Adafruit_BBIO.GPIO as GPIO

GPIO.setup("P8_10", GPIO.OUT)
GPIO.output("P8_10", GPIO.HIGH)
You can also refer to the pin names:
GPIO.setup("GPIO0_26", GPIO.OUT)
In the first example, you can see we used the "P8_10" key to designate which pin we'd like to set as the output, and the same pin in the second example, but using it's name "GPIO0_26".
You can also set pins as inputs as follows:
import Adafruit_BBIO.GPIO as GPIO

GPIO.setup("P8_14", GPIO.IN)
Once you've done that, you can access the input value in a few different ways. The first, and easiest way is just polling the inputs, such as in a loop that keeps checking them:
if GPIO.input("P8_14"):
You can also wait for an edge. This means that if the value is falling (going from 3V down to 0V), rising (going from 0V up to 3V), or both (that is it changes from 3V to 0V or vice-versa), the GPIO library will trigger, and continue execution of your program.

The wait_for_edge method is blocking, and will wait until something happens:
GPIO.wait_for_edge("P8_14", GPIO.RISING)
Another option, that is non-blocking is to add an event to detect. First, you setup your event to watch for, then you can do whatever else your program will do, and later on, you can check if that event was detected.

A simple example of this is as follows:
GPIO.add_event_detect("P9_12", GPIO.FALLING)
#your amazing code here
#detect wherever:
if GPIO.event_detected("P9_12"):
    print "event detected!"
We'll continue to add more examples, and features as we go, so check back often!

This guide was first published on Jun 13, 2013. It was last updated on Jun 26, 2013.

This page (GPIO) was last updated on Jun 12, 2013.

Text editor powered by tinymce.