GPIO
— General Purpose I/O interface¶
This module provides access and control of pins set up as General Purpose I/O (GPIO).
Note
You need to be part of the gpio
group of the OS running on the
Beaglebone to be able to run GPIO code as a non-root user. The default
user created upon the Debian image installation should already be
part of the group. Otherwise, you can use
sudo usermod -a -G gpio userName
to add userName
to the group.
Note
When coding with this module, you will be using pin names for better readability. As such, you can specify them in the header 8 or 9 form (e.g. “P8_16”) or in pin name form (e.g. “GPIO1_14”). For easy reference, you can use the Beaglebone pin names table
Note
On-board LEDs (USR0-USR3) are handled by LED class driver rather than the GPIO pin driver.
They have a different path in the /sys/ filesystem.
Setup the pin for output and write GPIO.HIGH or GPIO.LOW
Example:
# Use the config-pin command line tool to set a pin's function to GPIO
# Then you can control it with the GPIO module from Python
config-pin P9_14 gpio
import Adafruit_BBIO.GPIO as GPIO
# Set up pins as inputs or outputs
GPIO.setup("P8_13", GPIO.IN)
GPIO.setup("P8_14", GPIO.OUT)
GPIO.setup("GPIO0_26", GPIO.OUT) # Alternative: use actual pin names
# Write a logic high or logic low
GPIO.output("P8_14", GPIO.HIGH) # You can also write '1' instead
GPIO.output("P8_14", GPIO.LOW) # You can also write '0' instead
# Blinking onboard led example
import Adafruit_BBIO.GPIO as GPIO
import time
for i in range(4):
GPIO.setup("USR%d" % i, GPIO.OUT)
while True:
for i in range(4):
GPIO.output("USR%d" % i, GPIO.HIGH)
time.sleep(1)
for i in range(4):
GPIO.output("USR%d" % i, GPIO.LOW)
time.sleep(1)
-
Adafruit_BBIO.GPIO.
setup
(channel, direction[, pull_up_down=GPIO.PUD_OFF, initial=None, delay=0])¶ Set up the given GPIO channel, its direction and (optional) pull/up down control
Parameters: - channel (str) – GPIO channel to set up (e.g. “P8_16”).
- direction (int) – GPIO channel direction
(
GPIO.IN
orGPIO.OUT
). - pull_up_down (int) – pull-up/pull-down resistor configuration
(
GPIO.PUD_OFF
,GPIO.PUD_UP
orGPIO.PUD_DOWN
). - initial (int) – initial value for an output channel
(
GPIO.LOW
/GPIO.HIGH
). - delay (int) – time in milliseconds to wait after exporting the GPIO pin.
-
Adafruit_BBIO.GPIO.
cleanup
()¶ Clean up by resetting all GPIO channels that have been used by the application to
IN
with no pullup/pulldown and no event detection.Note: It’s recommended that you call this function upon exiting your application.
-
Adafruit_BBIO.GPIO.
output
(channel, value)¶ Set the given output channel to the given digital value.
Parameters: - channel (str) – GPIO channel to output the value to (e.g. “P8_16”).
- value (int or bool) – value to set the output to– 0/1 or False/True
or
GPIO.LOW
/GPIO.HIGH
.
-
Adafruit_BBIO.GPIO.
input
(channel)¶ Get the given input channel’s digital value.
Parameters: channel (str) – GPIO channel to read the value from (e.g. “P8_16”). Returns: Channel value–– 0 or 1. Return type: int
-
Adafruit_BBIO.GPIO.
add_event_detect
(channel, edge[, callback=None, bouncetime=0])¶ Enable edge detection events for the given GPIO channel.
Parameters: - channel (str) – GPIO channel to detect events from (e.g. “P8_16”).
- edge (int) – edge to detect––
GPIO.RISING
,GPIO.FALLING
orGPIO.BOTH
- callback (func) – a function to call once the event has been detected.
- bouncetime (int) – switch bounce timeout in ms for the callback.
-
Adafruit_BBIO.GPIO.
remove_event_detect
(channel)¶ Remove edge detection for the given GPIO channel.
Parameters: channel (str) – GPIO channel to remove event detection from (e.g. “P8_16”).
-
Adafruit_BBIO.GPIO.
event_detected
(channel)¶ Checks if an edge event has occured on a given GPIO.
Note: You need to enable edge detection using add_event_detect()
first.Parameters: channel (str) – GPIO channel to check for event detection for (e.g. “P8_16”). Returns: True if an edge has occured on a given GPIO, False otherwise Return type: bool
-
Adafruit_BBIO.GPIO.
add_event_callback
(channel, callback[, bouncetime=0])¶ Add a callback for an event already defined using
add_event_detect()
Parameters: - channel (str) – GPIO channel to add a callback to (e.g. “P8_16”).
- callback (func) – a function to call once the event has been detected.
- bouncetime (int) – switch bounce timeout in ms for the callback.
-
Adafruit_BBIO.GPIO.
wait_for_edge
(channel, edge[, timeout=-1])¶ Wait for an edge on the given channel.
Parameters: - channel (str) – GPIO channel to wait on (e.g. “P8_16”).
- edge (int) – edge to detect––
GPIO.RISING
,GPIO.FALLING
orGPIO.BOTH
- timeout (int) – time to wait for an edge, in milliseconds. -1 will wait forever.
-
Adafruit_BBIO.GPIO.
gpio_function
(channel)¶ Return the current GPIO function (
IN
,IN
,ALT0
) of the given pin.Warning: Currently only returning the direction of the pin (input or output) is supported. Parameters: channel (str) – GPIO pin to query the status of. Returns: 0 if IN
, 1 ifOUT
Return type: int
-
Adafruit_BBIO.GPIO.
setwarnings
(gpio_warnings)¶ Enable or disable GPIO warning messages.
Warning: Currently enabling or disabling warnings has no effect. Parameters: gpio_warnings (int) – 0–– disable warnings; 1–– enable warnings
-
Adafruit_BBIO.GPIO.
ALT0
¶ Pin mode– alternate function 0.
-
Adafruit_BBIO.GPIO.
BOTH
¶ Edge detection– detect both edges.
-
Adafruit_BBIO.GPIO.
FALLING
¶ Edge detection– detect falling edge.
-
Adafruit_BBIO.GPIO.
HIGH
¶ Pin status– logic low.
-
Adafruit_BBIO.GPIO.
IN
¶ Pin mode– input.
-
Adafruit_BBIO.GPIO.
LOW
¶ Pin status– logic low.
-
Adafruit_BBIO.GPIO.
OUT
¶ Pin mode– output.
-
Adafruit_BBIO.GPIO.
PUD_OFF
¶ Pull-up/pull-down resistor type– no pull-up/pull-down.
-
Adafruit_BBIO.GPIO.
PUD_DOWN
¶ Pull-up/pull-down resistor type– pull-down.
-
Adafruit_BBIO.GPIO.
PUD_UP
¶ Pull-up/pull-down resistor type– pull-up.
-
Adafruit_BBIO.GPIO.
RISING
¶ Edge detection– detect rising edge.
-
Adafruit_BBIO.GPIO.
VERSION
¶ GPIO module version. Currently unused.