Using the GPIO ports

A maximum of 66 GPIO pins are accessible from the expansion header. All of these pins are 3.3V and can be configured as inputs or outputs. Any GPIO can be used as an interrupt and is limited to two interrupts per GPIO Bank for a maximum of eight pins as interrupts. The max output current varies by pin. For some pins it's 4mA and for some pins it's 6mA. The inputs are not 5V tolerant. The max voltage is 3.3V.
 
To use a GPIO pin:
First, the port needs to be exported to user-land:
For this example, we will use GPIO port 23:
  1. cd /sys/class/gpio/
  2. echo 23 > export
Now, there is a /sys/class/gpio/gpio23 directory:
 
-rw-r--r-- 1 root root 4096 Mar 11 13:18 active_low
-rw-r--r-- 1 root root 4096 Mar 11 13:19 direction
-rw-r--r-- 1 root root 4096 Mar 11 13:18 edge
drwxr-xr-x 2 root root    0 Mar 11 13:18 power
lrwxrwxrwx 1 root root    0 Mar 11 13:18 subsystem -> ../../../../class/gpio
-rw-r--r-- 1 root root 4096 Mar 11 13:18 uevent
-rw-r--r-- 1 root root 4096 Mar 11 15:10 value
 
 
 To set the direction of the GPIO to output:
  1. echo out > direction
To set the value of the GPIO output:
  1. echo 1 > value
 
To read the value of the GPIO:
  1. cat value
 
The GPIO port can be set to provide events. The event can trigger on rising edge, falling edge or both.
  1. echo rising > edge
To get events, just do a blocking read to the value file, and the read will block until the event occurs.
 
 
 
Subpages (1): GPIOs already in use
Comments