Digital Read/Write using sysfs

In this post I will describe how to use sysfs to access the GPIO pins in the BeagleBone black, this should also be compatible with the recently released BeagleBone green.

From Wikipedia: "sysfs is a virtual file system provided by the Linux kernel that exports information about various kernel subsystems, hardware devices, and associated device drivers from the kernel's device model to user space through virtual files"

The BeagleBone black will expose GPIO pins via sysfs, and the location we will use is "/sys/class/gpio/".

Setup

I will wire up an LED to pin 60 and a button on pin 44, please refer to the BeagleBone Black GPIO layout

bbb-setup

Digital Read/Write

All the following commands are run on the BeagleBone after starting an ssh session.

I will describe how to perform the following actions:

  1. Export the pins
  2. Write the pin direction (in/out)
  3. Write/Read values
  4. Unexport the pins

Export/Unexport

To use a pin we must export it, to release a pin we must "unexport" it, we achieve these actions by writing the pin's numeric value to the export/unexport files.

ls -il /sys/class/gpio/ | grep export

127 --w------- 1 root root 4096 Jan  1 00:00 export
128 --w------- 1 root root 4096 Jan  1 00:00 unexport

You can see we have write but not read access to both these files, lets export pin 60

echo 60 > /sys/class/gpio/export

Now we will see a "/sys/class/gpio/gpio60" symlink to "/sys/devices/virtual/gpio/gpio60" as a result of exporting the pin

ls -il /sys/class/gpio/ | grep gpio60

8848 lrwxrwxrwx 1 root root    0 Jan  1 00:22 gpio60 -> ../../devices/virtual/gpio/gpio60

Write

Now that our pin 60 has been exported its primed and ready to be used, within the "/sys/class/gpio/gpio60" folder we only care about two files: direction and value.

ls -il /sys/class/gpio/gpio60/ | grep 'value\|direction'

8857 -rw-r--r-- 1 root root 4096 Jan  1 00:29 direction
8855 -rw-r--r-- 1 root root 4096 Jan  1 00:29 value

First we set the direction to "out":

echo "out" > /sys/class/gpio/gpio60/direction

Now we can turn the LED on pin 60 on and off by flipping the value from 0 to 1 and vice-versa.

echo 1 > /sys/class/gpio/gpio60/value

echo 0 > /sys/class/gpio/gpio60/value

alt text

Read

The following script will export, write "in" as the direction then read the value on pin 44 every second:

echo 44 > /sys/class/gpio/export

echo "in" > /sys/class/gpio/gpio44/direction

watch --interval 1 cat /sys/class/gpio/gpio44/value

Now you can see 0 and 1 on the screen as you press/release the button.

Unexport

Once we unexport the pin the /sys/class/gpio/gpio[pin#] folder will disappear:

echo 60 > /sys/class/gpio/unexport
echo 44 > /sys/class/gpio/unexport

Now we can make sure no gpio60 or gpio44 folders exist under "/sys/class/gpio/"

ls /sys/class/gpio/ | grep '60\|40'

In the wild

This technique is the "magic" behind some of the BeagleBone GPIO libraries, two examples:

BoneScript Node.js library

writeGPIOValue method

readGPIOValue method

Adafruit's Python library

C bound gpio_set_value method

C bound open_value_file method