This one is short and sweet, based on the blinking LED example found here.
Here’s a shell script to read a GPIO and generate a square wave on the console:
#!/bin/sh # # Read a GPIO input GPIO=$1 cleanup() { # Release the GPIO port echo $GPIO > /sys/class/gpio/unexport echo "" echo "" exit } # Open the GPIO port # echo "$GPIO" > /sys/class/gpio/export echo "in" > /sys/class/gpio/gpio${GPIO}/direction trap cleanup SIGINT # call cleanup on Ctrl-C THIS_VALUE=`cat /sys/class/gpio/gpio${GPIO}/value` LAST_VALUE=$THIS_VALUE NEWLINE=0 # Read forever while [ "1" = "1" ]; do # next three lines detect state transition if [ "$THIS_VALUE" != "$LAST_VALUE" ]; then EV="|" else EV="" fi # "^" for high, '_' for low if [ "1" = "$THIS_VALUE" ]; then EV="${EV}^" else EV="${EV}_" fi echo -n $EV # sleep for a while sleep 0.05 # wrap line every 72 samples LAST_VALUE=$THIS_VALUE THIS_VALUE=`cat /sys/class/gpio/gpio${GPIO}/value` NEWLINE=`expr $NEWLINE + 1` if [ "$NEWLINE" = "72" ]; then echo "" NEWLINE=0 fi done cleanup # call the cleanup routine
I saved this as ~/read_gpio, did a ‘chmod 755 read_gpio’and invoked it to read the user button, GPIO 7:
root@beagleboard:~# ./read_gpio 7 _________________________________|^^^^|_____|^^^|_____________|^^^|___|^^^|_____ ____|^^|____|^^|____|^|________|^^^|_______|^^|____________|^^^|______|^^|_______ root@beagleboard:~#
Sampling at a 50ms interval appeared to catch most of my button pushes, even at an unreasonably high rate. A 100ms interval was too long and some of the faster button pushes were missed.
5 replies on “Reading BeagleBoard User Button (or any GPIO)”
when i tried this i got this error over and over
cat: /sys/class/gpio/gpio7/value: No such file or directory
what have i done wrong?
Sounds like the
echo “$GPIO” > /sys/class/gpio/export
command failed to setup the GPIO properly.
Please note my post is 9 months old and has not been updated. My work with the BeagleBoard was short-lived and I don’t have access to hardware to double-check what I’ve posted. Please refer to the link I reference in the first paragraph and try the steps manually before running the script as written.
This post is old but I figured I would leave this here for anyone else who might come across this same error. I think the author used an older board revision which works fine using gpio7. On the newer beagleboard-xm the user button is mapped to gpio4 not gpio7.
Yes, this article is circa May 2009. I can’t remember off the top of my head what revision board we used, but I expect things have changed since the post. Unfortunately, I am no longer working with the Beagleboard and can’t update what I done to reflect newer hardware.
Thanks for pointing out the differences.
I have just got this working with a BB-xM release C. Even though the manual says it should be GPIO 7, GPIO 4 works.
The BeagleBoard community is not the easiest to understand, they probably dont want to be understood by the likes of us. Now I want to get the LED example from MAKER working, then I will be a touch happier…