Skip to content
systemcoding.dev
Embedded Linux

Toggle and Read GPIO Lines With gpioset and gpioget

Drive an LED and read a button from the shell with gpioset and gpioget from libgpiod 2.1 on Linux 6.12, including toggling, timed pulses and bias.

6 min read
Summarize in AI
Flat illustration of a microcontroller wired to an LED and a sensor, with the Linux, Espressif and Arm logos.

Once you know which chip and offset a pin has, two commands do most bench work: gpioset drives an output line high or low, and gpioget reads the level of an input. They are the tools that replace echo 1 > /sys/class/gpio/gpio17/value, and they behave differently in one important way that surprises people coming from sysfs: in libgpiod 2, gpioset holds the line until the command is stopped, because the request that configured the line belongs to the running process. This post uses libgpiod 2.1 on Linux 6.12 with an LED on offset 17 and a button on offset 27 of gpiochip0, shows the common invocations with their output and explains bias, active-low lines and timed pulses. The reference is the libgpiod tools documentation.

The wiring assumed

An LED with a series resistor connects between line 17 and ground, so the line high lights it. A push button connects line 27 to ground, so pressing it pulls the line low, and an internal pull-up resistor holds it high otherwise. Adjust the offsets to your board after finding them with gpioinfo. Before connecting anything to a pin, check the SoC's maximum current per pin and the voltage level of the board, typically 3.3 V. A GPIO cannot drive a large load directly.

Drive a line with gpioset

The syntax in version 2 names the chip with --chip or -c and gives line assignments as offset=value or name=value:

Shell
gpioset --chip gpiochip0 17=1
Text
(the LED lights; the command does not return until you press Ctrl-C)

When you stop it with Ctrl-C the request ends, the line is released and the kernel returns it to its default state, which for most SoCs is an input with no drive, so the LED turns off. That is the design: the line belongs to a process. If you want the state to persist after the command returns, you have two options. The first is to keep the process alive in the background:

Shell
gpioset --chip gpiochip0 --daemonize 17=1
Text
(returns immediately; a background process holds line 17 high)

The second is for programs that own the line for their whole lifetime, as an application daemon does. Running --daemonize leaves a background process that you must stop to release the line, so use it deliberately, and find it later with gpioinfo, which shows the consumer.

Values are 1 or 0, and active, inactive, on, off, true and false are accepted too. Several lines are set in one request, and they change together as far as the hardware allows:

Shell
gpioset --chip gpiochip0 17=1 18=0 19=1
Text
(lines 17 and 19 are driven high, line 18 low, held until interrupted)

Timed pulses and blinking

The tool can end the hold by itself with a period, which makes pulse generation a one-liner. --hold-period waits after setting the values and before exiting, and --toggle alternates between values at set intervals:

Shell
gpioset --chip gpiochip0 --hold-period 500ms 17=1
gpioset --chip gpiochip0 --toggle 500ms,500ms,0 17=1
Text
(the first command drives 17 high for half a second and exits; the second blinks the LED once on and once off, then stops)

The toggle argument is a list of periods: the line holds its initial value for the first period, flips for the second, flips back for the third, and the final period of zero ends the sequence. A list with no trailing zero repeats forever. Software timing from a shell is not precise, because the tool sleeps and the scheduler wakes it when it can, so use these for human-visible effects and not for protocols.

Read a line with gpioget

gpioget requests the line as an input, reads it once and prints the state:

Shell
gpioget --chip gpiochip0 27
Text
"27"=active

The output is the line identifier and its logical value, active or inactive. With the button wired to ground and the pull-up enabled, an unpressed button is high on the pin. Because active means logical high unless the line is configured active low, the unpressed state prints active. To read the physical level in a form your scripts can use, use --numeric:

Shell
gpioget --chip gpiochip0 --numeric 27
Text
1

Read several lines at once, and their values are sampled together:

Shell
gpioget --chip gpiochip0 --numeric 27 22 23
Text
1 0 1

Bias: giving an input a defined level

An input with nothing connected floats and reads random values. Buttons need a pull resistor to give the line a level when open. Many SoCs have configurable internal resistors, and libgpiod exposes them as bias options on the request:

Shell
gpioget --chip gpiochip0 --bias pull-up --numeric 27
Text
1

The options are pull-up, pull-down, disabled and as-is, which leaves the existing setting alone. Not every chip supports every option, and some can only set bias through the pin controller's device tree configuration, in which case the request fails with "Operation not supported". If your hardware has an external resistor, as-is is correct. Press the button while a loop reads the line to see it change:

Shell
while true; do gpioget -c gpiochip0 --bias pull-up -n 27; sleep 0.2; done
Text
1
1
0
0
1

The -n short form is the same as --numeric. The zeros are the moments the button was held.

Active-low lines

A line wired so that low means on, such as an LED that connects the pin to the supply through a resistor, is active low. Rather than invert values in every script, tell the tool with --active-low, so that 1 means "on" regardless of the pin level:

Shell
gpioset --chip gpiochip0 --active-low 17=1
Text
(line 17 is driven low, and the LED wired to the supply side lights)

Device trees can express the same polarity in the consumer driver, which is the cleaner solution when the kernel owns the line.

Why it says the device is busy

If another process or a kernel driver holds a line, requesting it fails:

Text
gpioset: unable to request lines on chip '/dev/gpiochip0': Device or resource busy

Run gpioinfo --chip gpiochip0 and look at the consumer of that line. A kernel driver that was bound to the pin, such as gpio-leds or spi chip selects, owns it. Either use the interface the driver provides, for example the LED class under /sys/class/leds/, or remove the node from the device tree. A stale gpioset --daemonize from an earlier session is another common holder.

When not to script this

A shell loop is the wrong tool for anything timing-critical, for protocols that need microsecond precision, or for an application that must keep ownership of a line across its own restarts. For those, use the library from C, Python or Rust, request the line once, and keep the request open. The command-line tools are for bench work, tests and simple boot-time setups.

Putting the commands in a script safely

A small shell function keeps bench scripts readable and avoids repeating the chip name. Define the chip once in a variable, call gpioset in the background with --daemonize for lines that must stay set, and read lines with gpioget --numeric so the result is easy to test with [ "$(...)" = 1 ]. Always check the exit status of gpioset, since a busy line returns a non-zero code that a script otherwise ignores. For anything that outlives a shell session, replace the script with a small daemon or a systemd service that holds the lines, and let gpioinfo show its consumer name so an operator can see which unit owns which pin.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.