Help regarding IO configuration of multiple lines
Hello,
I am trying to learn how to use libgpiod on a Pi 5. I created for that goal a simple circuit with a push button and 2 LEDs, one of which turns on on a button press.
Since I want to keep my program as modular as possible, I created a pinconfig function that configures the pins according to the parameters passed in main.
However, when trying to pass an enum array of gpiod_line_direction to the function, the code no longer works and the LEDs no longer turn on. I suspect it is due to the way the array is handled, but since this use case isn't shown in the examples and that online ressources seems to only talk about v1 of gpiod, I'm a bit stuck.
Could someone help me? You'll find my code attached in a zip in this email.
Thanks in advance
Mathis Joussemet
Quoted 16 lines · On Fri, Mar 06, 2026 at 12:09:50PM +0100, [email protected] wrote:
Hello,
I am trying to learn how to use libgpiod on a Pi 5. I created for that goal a simple circuit with a push button and 2 LEDs, one of which turns on on a button press.
Since I want to keep my program as modular as possible, I created a pinconfig function that configures the pins according to the parameters passed in main.
However, when trying to pass an enum array ofgpiod_line_directionto the function, the code no longer works and the LEDs no longer turn on. I suspect it is due to the way the array is handled, but since this use case isn't shown in the examples and that online ressources seems to only talk about v1 of gpiod, I'm a bit stuck.
Could someone help me? You'll find my code attached in a zip in this email.
Yeah, the example code doesn't provide a good example of requesting lines with different settings.
The closest is get_multiple_line_values.c. Though that applies settings for each line individually when it could just use a single gpiod_line_config_add_line_settings() call, as all the lines have the same settings. Not sure why. ¯\_(ツ)_/¯
The relevant section of your code, which seems to follow that example, is:
gpiod_line_settings_set_direction(line_settings, *direction);
// ...
for (int i = 0; i < line_nbr; i++) {
gpiod_line_config_add_line_settings(line_config, &pin_list[i], 1, line_settings);
}That applies the first entry in your direction array as the direction for all lines. Definitely not what you want.
See if this works for you:
for (int i = 0; i < line_nbr; i++) {
gpiod_line_settings_set_direction(line_settings, direction[i]);
gpiod_line_config_add_line_settings(line_config, &pin_list[i], 1, line_settings);
}That applies the direction for each line individually, with other line settings being the same.
Cheers,
Kent.
Hi Kent,
Sorry for the late reply, was up until now in exam hell. It indeed works! I can now drive 2 LEDs at the same time!
I just have 2 new questions:
- I am now using a button to toggle one of the two LEDs by checking the buttons value usinggpiod_line_request_get_value(request, offset[2])!=0)
in a if statement. I've setup the correct input bias, but when I press on it, the LED doesn't toggle. Checking with a scope still shows the voltage jumping to 3.3v when pressed and 0v at other times. Is there something I'm missing?
- I'm trying to switch from an snapshot based line change reporting to an event one. however when using a watch line event before/after the request, the program still blocks. How should i code it to prevent blocking while still having infos like output values types when there is a change?
Thanks for the help and sorry again for the late reply Mathis
Quoted 54 lines · Kent Gibson
Quoted 16 lines · On Fri, Mar 06, 2026 at 12:09:50PM +0100, [email protected] wrote:
Hello,
I am trying to learn how to use libgpiod on a Pi 5. I created for that goal a simple circuit with a push button and 2 LEDs, one of which turns on on a button press.
Since I want to keep my program as modular as possible, I created a pinconfig function that configures the pins according to the parameters passed in main.
However, when trying to pass an enum array ofgpiod_line_directionto the function, the code no longer works and the LEDs no longer turn on. I suspect it is due to the way the array is handled, but since this use case isn't shown in the examples and that online ressources seems to only talk about v1 of gpiod, I'm a bit stuck.
Could someone help me? You'll find my code attached in a zip in this email.Yeah, the example code doesn't provide a good example of requesting lines with different settings.
The closest isget_multiple_line_values.c. Though that applies settings for
each line individually when it could just use a singlegpiod_line_config_add_line_settings()call, as all the lines have the same settings. Not sure why. ¯\_(ツ)_/¯The relevant section of your code, which seems to follow that example, is:
gpiod_line_settings_set_direction(line_settings, *direction); // ... for (int i = 0; i < line_nbr; i++) { gpiod_line_config_add_line_settings(line_config, &pin_list[i], 1, line_settings); }That applies the first entry in your direction array as the direction for all
lines. Definitely not what you want.See if this works for you:
for (int i = 0; i < line_nbr; i++) { gpiod_line_settings_set_direction(line_settings, direction[i]); gpiod_line_config_add_line_settings(line_config, &pin_list[i], 1, line_settings); }That applies the direction for each line individually, with other line settings being the same.
Cheers,
Kent.
Quoted 10 lines · Mathis Joussemet
Hi Kent,
Sorry for the late reply, was up until now in exam hell. It indeed works! I can now drive 2 LEDs at the same time!
I just have 2 new questions:
- I am now using a button to toggle one of the two LEDs by checking the buttons value usinggpiod_line_request_get_value(request, offset[2])!=0)
in a if statement.
I don't see any obvious reason that wouldn't read the button, assuming it is the third line in your offset array, and the condition is tested at the time you are pushing the button.
Have you tried the equivalent with the gpiod tools (gpioget) or some of the example code (e.g. get_line_value) to confirm your hardware setup is working as expected?
Quoted 4 lines
I've setup the correct input bias, but when I press on
it, the LED doesn't toggle. Checking with a scope still shows the voltage jumping to 3.3v when pressed and 0v at other times. Is there something I'm missing?
That being the input voltage? For an input pulled down when the button is not pressed and pulled high when it is pressed?
That is what I would expect to see.
Quoted 5 lines
- I'm trying to switch from an snapshot based line change reporting to an event one. however when using a watch line event before/after the request, the program still blocks. How should i code it to prevent blocking while still having infos like output values types when there is a change?
You can use edge events in a polling scenario by testing if the request fd is ready to read using poll() or select() or similar.
It will become ready when an event is available.
You can then read the event as usual and the read will not block. So similar to the async_watch_line_value example, but with a 0 timeout in the poll() and checking the ret:
ret = poll(&pollfd, 1, 0);
if (ret > 0) {
// read event...
}and doing whatever else you want in the polling loop.
Cheers,
Kent.