Sunday, July 5, 2009

Accessing GPIO

1. On and Off LED at the expansion pins of Beagle Board

10 easy steps for switching on and off the LED at expansion pins of BB.

It is assumed that you are having Angstrom distribution on your BB.

Make sure that you have mounted (soldered) the expansion connector on BB. You have a low powered LED. If you dont have low power LED, use multimeter instead. Connect the LED between pin 24 and 28 (GND). And now follow the steps below to on and off LED via expansion pin.

1. Open a Terminal (Applications >> Accessories >> terminal ).

2. Boot BB by writing command minicom.

3. Log-in as a root.

4. write command cd /sys/class/gpio

5. Enter command echo 168 > export (expansion port GPIO 168 = pin 24. You can also access User LED where 150 = usr1 LED and 149 = usr0 LED. For numbers refer http://http://elinux.org/upload/5/51/Bb-expansion.pdf ). That should create directory /sys/class/gpio/gpio150, with all the access you want.

6. Enter cd /sys/class/gpio/gpio150

7. Enter echo "high" > direction (Check the LED status. It should be on)

8. Enter echo "low" > direction (Check the LED status. It should be off)

Done! smile

If you want then to unmount the device, use unexport

9. Enter cd /sys/class/gpio/

10. Enter echo 150 > unexport

You can follow the above procedure to check the functioning of the other pins wink



2. Infinite loop - On and Off LED at the expansion pins of Beagle

So log in to your Beagle Board as root, and enter the following program into a file called strobe_gpio:

#!/bin/sh
#
# Blink the onboard LED

GPIO=$1

cleanup() { # Release the GPIO port
echo $GPIO > /sys/class/gpio/unexport
exit
}
# Open the GPIO port
#
echo $GPIO > /sys/class/gpio/export

trap cleanup SIGINT # call cleanup on Ctrl-C

# Blink forever
while [ "1" = "1" ]; do
echo "high" > /sys/class/gpio/gpio$GPIO/direction
sleep 1
echo "low" > /sys/class/gpio/gpio$GPIO/direction
sleep 1
done

cleanup # call the cleanup routine
The vi editor is included with the Beagle Board, so you can type " vi strobe_gpio", type "i" (to get into insert mode), then paste the program in. Next, press Escape (to get back into command mode), and type to save the file and ":w"":q" to exit from VI. This should drop you back into the Linux shell. (There are many vi tutorials External link mark in the universe if this is proving to be troublesome.)

Next, type " chmod 755 strobe_gpio". Then, insert an LED as shown (short end into pin 28, long end into pin 24) . Try the command"./strobe_gpio 168" and you should see the LED blinking.

Try this out also: type " chmod 755 strobe_gpio". Try the command "./strobe_gpio 149" or "./strobe_gpio 150" and you should see the user LED blinking wink

3. Reading Beagleboard User button or any GPIO


Here’s a shell script to read a GPIO and generate a square wave on the console (let's call the file read_gpio):

Start with " vi read_gpio", type "i" and paste the program below

#!/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

Now, type ":w" to save the file and ":q" to exit from VI.

type " chmod 755 read_gpio". Try the command "./read_gpio 7"

now press the user button which will lead the output to the one similar as below.

*****************************************
root@beagleboard:~# ./read_gpio 7
_________________________________|^^^^|_____|^^^|_____________|^^^|___|^^^|_____
____|^^|________|^^|____|^|________|^^^|_______|^^|____________|^^^|______|^^|______.

root@beagleboard:~#
*****************************************

22 comments:

  1. I tried gpio 168 as you say but it didn't work. Value or direction of pin couldn't be changed. I think there may be a problem with port multiplex, gpio 168 might be used by i2c. How can I fix that?

    ReplyDelete
  2. It doesn't works for me in my BB xM.

    Can you tell me what should the problem?

    ReplyDelete
  3. Hi luca26, did you try GPIO 7 for userbutton?

    ReplyDelete
  4. Actually, in BB xM, it has been changed to GPIO 4 for userbutton

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  5. Hi, i have Pandaboard, it's the same way of BB???
    thank you, people how me, only need a little help to construct a world.

    ReplyDelete
    Replies
    1. Also works for Pandaboard, use GPIO 121.
      BTW: For those who use busybox: sleep does not permit floats, so I use usleep 50000 instead.

      Delete
  6. For BB xM Rev C the expansion pin 24 (GPIO168) is reserved for the I2C SCL signal and it cannot be changed without tweaking U-Boot and kernel settings and as this is a "for dummies" guide such tuts would be beyond the scope. You can export GPIO168 just fine and it appears as if you could change the state, however the pin stays high no matter what you do.

    Instead one should use pin 22 for instance (GPIO157). This works fine.

    ReplyDelete
    Replies
    1. Hi, I've tried with pin 22 as you suggested but both with 24 and 22 (168 and 157) I'm denied permission when I do
      echo 157 > export
      I just need a script to light an LED in a BB xM. Any help?

      Delete
    2. You have to login as root to perform the operations. If you just do "sudo echo 157 > export", the file operation will still be undertaken by your normal user account. Try this:

      sudo su
      echo 157 > export
      exit

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I got mine to work on the pandaboard. Must change the owner:group and use chmod for this to work w/o using the sh.

    ReplyDelete
    Replies
    1. Hey Kelvin,

      Great work!

      Can you share your procedure here. I am also trying to do the same. It would be a great help.

      Jatin

      Delete
    2. #save this as a bash shell script and run it or
      #you can enter it in line by line on the terminal
      #once the owner is changed you can do whatever you like in the gpio140 folder
      #like echo 1 > value without using sudo or a shell
      #you can also change the owner of export command prior if you want
      #!/bin/sh

      gpio=140

      path=/sys/class/gpio/gpio$gpio #set path variable

      sudo chown -Rv kelvin:kelvin $path/

      Delete
  9. Hi I tried similar method as suggested in Part 3 (GPIO button) for one of the expansion header, pin GPIO159.
    Somehow, it only reads a 0 and doesn't go HIGH even on click of button.
    Are there any other settings in regards to pin mux or something for the expansion header?
    Thanks a ton!

    ReplyDelete
  10. For BeagleBoard xm Rev C Default is gpio 130, rest is on Multiplex (Selection )

    ReplyDelete
    Replies
    1. i mean Default GPIO if you Read or Write From GPIO130 to GPIO139 (pin 21,19,17,15,13,11,9,7,5,3)
      Rest Pins are Multiplex to else. i will post Detail on my Blog

      Delete
  11. I have a pandaboard es and testing the gpio 121 button. When i push the button no significant changes are done, i also tried cat value whenever i pushed button and it provides the same result. Why is this happening and how can solve this?

    ReplyDelete
  12. There are gpio 0, gpio 128, gpio 160 ,gpio 192, gpio 32, gpio 64 and gpio 96 in beagle board xM. can u pl let me know how to make a led blink?
    What are the steps involved to include other gpios

    ReplyDelete
  13. hello.. Iam working on Beagle board rev c4. I have a preinstalled raw image of ubuntu 12.04 running on my board. I wish to access uart2_rx pin which is already multiplexed as gpio(mode 4). I have to make it mode 1. Can anyone please give a detailed steps on how to pin configure the multiplexed pin. I couldnt get much idea from elinux,org pinmuxing .
    regards

    ReplyDelete
  14. # 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
    coul any one explain each command

    ReplyDelete
  15. None of these are working with beagleboard rev C3...

    ReplyDelete