GPIO with libgpiod Tools on Linux: Replacing Deprecated sysfs

Use gpiodetect, gpioinfo, gpioget, gpioset and gpiomon to list and control GPIO lines from the shell instead of the obsolete sysfs interface.

Embedded Published 3 min read

The old way of toggling a pin from the shell, writing to files under /sys/class/gpio, is obsolete. The kernel documentation points to the GPIO character device API instead, and the libgpiod command-line tools are the easiest way to use it. This article shows how to list GPIO chips and lines, read and set values, and watch for edges.

Why sysfs is on its way out

The kernel documentation calls the sysfs GPIO interface obsoleted by the GPIO character device API. It says new developments should use the character device, and that the sysfs API will be removed in the future. New features go only to the character device.

The libgpiod tools work with GPIO chips by number, name or /dev/gpiochipN path.

Install the libgpiod tools

Package names differ by distribution.

✓ tested · debian,ubuntu
apt-get update && apt-get install -y gpiod
✓ tested · fedora
dnf install -y libgpiod-utils
✓ tested · arch
pacman -Sy --noconfirm libgpiod
✓ tested · debian,ubuntu,fedora,arch
gpiodetect --version

The options below follow the current man pages. Check --version and your local man pages, since older releases may differ.

List chips and lines

gpiodetect prints each chip with its label and number of lines. gpioinfo prints information about the lines; with no arguments it covers all of them.

gpiodetect
gpioinfo -c gpiochip0

A chip can be given as a number, a name or a path, so 0, gpiochip0 and /dev/gpiochip0 all select the same chip. Lines are given by name, or by offset when you pass --chip. For background on how boards describe their hardware, see Device Tree Basics, and for applying a device tree overlay, see Device Tree Overlays.

Read a line

gpioget reads the value of one or more lines. The --numeric option prints 0 or 1, and --bias can set a pull-up or pull-down.

gpioget --numeric -c gpiochip0 17
gpioget -c gpiochip0 --bias pull-up 17

Pass --as-is to leave the line direction unchanged instead of forcing it to input.

Set a line

gpioset takes line=value pairs. Values can be 1/0, active/inactive or on/off.

gpioset -c gpiochip0 17=1 22=off

The man page notes that the line state is not guaranteed after gpioset exits, so by default the tool keeps running to hold the value. Keep this in mind if you are used to sysfs. Useful options:

  • --toggle 500ms flips the lines after each period. Periods default to milliseconds; m, s, ms and us units are accepted.
  • --hold-period sets the minimum time to hold the values.
  • --daemonize sets the values and detaches from the terminal.
  • --active-low treats the line as active low.
  • --drive selects push-pull, open-drain or open-source.
gpioset -c gpiochip0 --toggle 500ms 17=active

To run gpioset as a long-lived service, see systemctl basics.

Watch for edges

gpiomon waits for events on lines and prints them. --edges selects rising, falling or both (the default).

gpiomon -c gpiochip0 --edges rising 4

Addressing lines

The tools address a line by name, or by offset within a chip when --chip is given.

If you reach the board over a serial console, see Serial Console on Embedded Linux for picocom, minicom and screen.

Sources

  1. GPIO Sysfs Interface for Userspace (Linux kernel documentation)
  2. gpiodetect(1) manual page
  3. gpioinfo(1) manual page
  4. gpioget(1) manual page
  5. gpioset(1) manual page
  6. gpiomon(1) manual page

#Gpio #Libgpiod #Gpiod #Sysfs #Embedded

This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.