Posted on
Questions and Answers

Control GPIO pins using `sysfs` (eg, `/sys/class/gpio/export`)

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Controlling GPIO Pins on Linux Using sysfs: A Comprehensive Guide

Introduction to GPIO Control in Linux General Purpose Input/Output (GPIO) pins are versatile interfaces found in various microprocessors and microcontroller boards. They allow interaction with different electronic components like LEDs, sensors, and switches. Linux, with its vast capabilities and broad device support, offers a unique method for interacting with GPIO pins called sysfs. This approach will be our focus today as we delve into how you can manipulate these pins directly from a Linux Bash shell.

Q1: What is sysfs in Linux?

A: sysfs is a virtual filesystem in Linux that provides a tree-like hierarchy of device information, allowing user space processes to interact with kernel objects. For GPIO, sysfs offers a means to manipulate the pins via simple file operations.

Q2: How do you access a GPIO pin using sysfs?

A: Accessing a GPIO pin involves several steps: 1. Export the GPIO Pin: This makes the pin available for use in user space by writing its number to /sys/class/gpio/export. 2. Set the Pin Direction: By writing "in" or "out" to /sys/class/gpio/gpioN/direction, where N is the pin number, you configure it as an input or output. 3. Write or Read the Pin: Writing a "1" or "0" to /sys/class/gpio/gpioN/value sets the pin high or low. Reading from this file gives the current state if the pin is configured as input.

Q3: What is an example of a simple operation on a GPIO pin?

A: A common example is turning an LED on and off. Assume you have an LED connected to GPIO pin 24:

  • Export the pin: echo 24 > /sys/class/gpio/export

  • Set as output: echo out > /sys/class/gpio/gpio24/direction

  • Turn LED on: echo 1 > /sys/class/gpio/gpio24/value

  • Turn LED off: echo 0 > /sys/class/gpio/gpio24/value

  • Unexport when done: echo 24 > /sys/class/gpio/unexport

Background and Simple Examples

The sysfs interface provides a simplistic file-based approach to interact with hardware. This is particularly useful for scripting and automation directly from the command line or a Bash script.

Example: Blinking an LED Blinking an LED is one of the easiest yet visually satisfying demonstrations. You toggle the LED's state at regular intervals.

Executable Bash Script To show the power of controlling GPIO pins, here's a script that blinks an LED connected to pin 24 every second:

#!/bin/bash

# Define GPIO pin
PIN=24

# Export the GPIO pin
echo $PIN > /sys/class/gpio/export

# Set the direction of the GPIO pin
echo out > /sys/class/gpio/gpio$PIN/direction

# Function to control the LED
control_led() {
    echo $1 > /sys/class/gpio/gpio$PIN/value
}

# Blink the LED 5 times
for i in {1..5}
do
    control_led 1
    sleep 1
    control_led 0
    sleep 1
done

# Unexport the GPIO pin
echo $PIN > /sys/class/gpio/unexport

echo "Done blinking the LED."

Conclusion Controlling GPIO pins using the sysfs interface provides a powerful way to interact with electronics directly from a Linux system. Although newer interfaces like the GPIO Character Device offer more features, sysfs remains a valuable and accessible method, especially useful in older embedded systems or for those who prefer a straightforward, file-based access method. The simplicity of sending commands through Bash scripts exemplifies the adaptability and power of Linux in managing embedded hardware components. As always, ensure careful handling of GPIOs to avoid hardware damage.

Further Reading

For those interested in expanding their knowledge on controlling GPIO pins and further details on interfacing with hardware through Linux, the following resources may be useful:

  • GPIO Programming in C: Understanding low-level GPIO programming can be vital. This guide covers interfacing with GPIOs using C programming language on a Linux-based system. Read more on GPIO Programming in C

  • Understanding Linux’s sysfs Filesystem: Provides an in-depth discussion of the sysfs filesystem, how it organizes device information, and its role in system monitoring and interaction. Explore the sysfs overview

  • Linux GPIO tutorial for Raspberry Pi: This tutorial offers practical examples and explanations for controlling GPIOs on Raspberry Pi, which can be adapted for similar Linux-capable boards. View the Raspberry Pi GPIO Guide

  • Advanced Control with GPIO Character Device: This documentation provides insight into using the newer GPIO character device framework, which is being positioned to replace sysfs for GPIOs. Learn about GPIO Character Device

  • Bash Scripting Basics: Since this article uses Bash for controlling GPIOs via sysfs, a strong understanding of Bash scripting can be helpful. Read more on Bash Scripting

These resources should provide a well-rounded understanding of GPIO control in Linux, from beginner guides to deeper technical dives.