applications, ideas, technology, solutions

Reading BeagleBoard User Button (or any GPIO)

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:

# Read a GPIO input


cleanup() { # Release the GPIO port
  echo $GPIO > /sys/class/gpio/unexport
  echo ""
  echo ""

# 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`

# Read forever

while [ "1" = "1" ]; do
  # next three lines detect state transition
  if [ "$THIS_VALUE" != "$LAST_VALUE" ]; then

  # "^" for high, '_' for low
  if [ "1" = "$THIS_VALUE" ]; then
  echo -n $EV

  # sleep for a while
  sleep 0.05

  # wrap line every 72 samples
  THIS_VALUE=`cat /sys/class/gpio/gpio${GPIO}/value`
  NEWLINE=`expr $NEWLINE + 1`
  if [ "$NEWLINE" = "72" ]; then
    echo ""


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


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.


  1. jimmy's Gravatar jimmy
    November 2, 2009    

    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?

  2. rbw's Gravatar rbw
    March 2, 2010    

    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.

  3. abc's Gravatar abc
    November 15, 2010    

    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.

  4. rbw's Gravatar rbw
    November 16, 2010    

    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.

  5. April 11, 2012    

    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…

Leave a Reply

Your email address will not be published. Required fields are marked *


Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 2284732 bytes) in /home/content/55/4274555/html/wp-includes/wp-db.php on line 1978