Skip to content

Instantly share code, notes, and snippets.

Created March 6, 2012 15:49
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
BeagleBone GPIO with Python Test
# time module is needed for sleep function:
import time
# Open up the pins and set mode in/out
# TODO: Error handling
setupPin = file("/sys/class/gpio/export", "w")
setupPin.write("%d" % (38))
setupPin = file("/sys/class/gpio/gpio38/direction", "w")
setupPin = file("/sys/class/gpio/export", "w")
setupPin.write("%d" % (70))
setupPin = file("/sys/class/gpio/gpio70/direction", "w")
#Open outPin for writing and inPin for reading:
outPin = file("/sys/class/gpio/gpio38/value", "w")
inPin = file("/sys/class/gpio/gpio70/value", "r")
while True:
# This method uses polling to read the state of the pin.
# It's not the way it should be done, but we'll make do
# with this for now.
if =="1\n":
print "Button Pressed!\n"
# don't peg the processor:
# Control+C will get us out of that loop and close the pins so that other apps can use:
except KeyboardInterrupt:
inPin = file("/sys/class/gpio/unexport", "w")
inPin.write("%d" % (38))
outPin = file("/sys/class/gpio/unexport", "w")
outPin.write("%d" % (70))
Copy link

I'm just starting with Beaglebone GPIO, and I an wondering about when GPIO related files should be close()'ed.
In this code the outPin and inPin file handles, which are related to the /value files are never closed. Is that ok?
Does writing to the /unexport file for the GPIO eliminate the need for closing the /value files?
Thanks for any help with this, and thank you for posting this code.

Copy link

Yes, the value file should be closed. This code is a little old and I abstracted these functions into a Python module:

Copy link

Okay, hello there! So, I'm quite in love with python. I started it about 4 months ago. I'm more than efficient in languages Java, VB.Net/C#, PHP, and some C/C++ and ASM. Python was a cinch to pick up.

I was wondering, in the likely event that I don't use the default BBB's OS, where can I get the python API files for this? From the original out-of-box OS and just copy the file over to the new /sys ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment