Using GPIO Pins via sysfs
The GPIO pins available on the 40-pin header can be easily controlled manually via sysfs.
Please note however, that the sysfs
GPIO interface is deprecated and there may be more suitable alternatives for your application (like a real kernel driver).
First, identify which pins you would like to manipulate by reviewing the 40 Pin 4Kopen Connector page, and take a note of the number in the "sysfs gpio #" column.
Then, using a terminal, try the following:
$ cd /sys/class/gpio $ echo "${GPIO_NUMBER}" > ./export $ ls -l "./gpio${GPIO_NUMBER}/" total 0 -rw-r--r-- 1 root root 4096 Nov 30 02:15 active_low lrwxrwxrwx 1 root root 0 Nov 30 02:15 device -> ../../../961f080.pin-controller-sbc -rw-r--r-- 1 root root 4096 Nov 30 02:15 direction -rw-r--r-- 1 root root 4096 Nov 30 02:15 edge drwxr-xr-x 2 root root 0 Nov 30 02:15 power lrwxrwxrwx 1 root root 0 Nov 30 02:15 subsystem -> ../../../../../class/gpio -rw-r--r-- 1 root root 4096 Nov 30 02:15 uevent -rw-r--r-- 1 root root 4096 Nov 30 02:15 value
Writing the number into the export file (e.g: with echo
) will request that the kernel makes this pin available to you, in GPIO mode - it might fail if the pin is already in use by a driver, in which case you may need to investigate your device tree, or use a different pin.
A few important files are made available via the sysfs
GPIO interface;
active_low
- whether the signal is inverteddirection
- to configure the pin as input or outputvalue
- the pin's current input or output value
More information about this interface can be found in the kernel documentation: https://www.kernel.org/doc/Documentation/gpio/sysfs.txt
Once you have exported your pin, you can manipulate it as follows:
$ cd "/sys/class/gpio/gpio${GPIO_NUMBER}/" $ echo in > direction $ cat value 1 $ echo out > direction $ echo 1 > value # check the pin's voltage $ echo 0 > value # check the pin's voltage
After you have made use of the pin, you may want to return it to the kernel. To do so, simply write the pin's number into the unexport file:
$ cd /sys/class/gpio $ echo "${GPIO_NUMBER}" > ./unexport