The Pi filesystem is accessible from any programming language and also from a terminal.

The files that control GPIO pins are located in /sys/class/gpio.

Read a GPIO pin

read using the filesystem

This example:

  • Establishes access to GPIO pin 12.
  • Configures the GPIO as input.
  • Reads the input point mapped to GPIO 12 and writes its value to the terminal.
    (The input point is at rack position 7 and its status is 1 (Off).)

Syntax

echo [GPIO pin number] > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio[GPIO pin number]/direction
cat /sys/class/gpio/gpio[GPIO pin number]/value

Write to output

write using the filesystem

This example:

  • Establishes access to GPIO pin 25.
  • Configures the GPIO as output.
  • Turns On the output point mapped to the GPIO.
  • Reads and writes the point’s value to the terminal.
  • Turns Off the point.
  • Reads and writes the point’s value.

Syntax

echo [pin number] > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio[pin number]/direction
echo 0 > /sys/class/gpio/gpio[pin number]/value
cat /sys/class/gpio/gpio25/value
echo 1 > /sys/class/gpio/gpio[pin number]/value
cat /sys/class/gpio/gpio25/value

Back to Examples for Raspberry Pi