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.
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.
apt-get update && apt-get install -y gpioddnf install -y libgpiod-utilspacman -Sy --noconfirm libgpiodgpiodetect --versionThe 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 gpiochip0A 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 17Pass --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=offThe 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 500msflips the lines after each period. Periods default to milliseconds;m,s,msandusunits are accepted.--hold-periodsets the minimum time to hold the values.--daemonizesets the values and detaches from the terminal.--active-lowtreats the line as active low.--driveselectspush-pull,open-drainoropen-source.
gpioset -c gpiochip0 --toggle 500ms 17=activeTo 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 4Addressing 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
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.