I used to use the sysfs interface to the GPIO pins (/sys/class/gpio) but
I understand that is deprecated nowadays. So I tried to switch to lgpio
which looks OK.
However, I have problems writing and reading back pin
states from different programs.
[snip]
However ... When I set the pin's state to "1", I still read back "0"!
What am I doing wrong? Thanks in advance for any pointers.
Hi,
I used to use the sysfs interface to the GPIO pins (/sys/class/gpio) but
I understand that is deprecated nowadays. So I tried to switch to lgpio
which looks OK. However, I have problems writing and reading back pin
states from different programs.
My setup is as follows:
I have a couple of relays (solid state and mechanical ones) that control various external devices.
I use one program to switch devices on and off and want to use another program to read back the state of the device.
Doing that with sysfs is easy:
1) export the pin:
echo $pin > /sys/class/gpio/export
echo $direction > /sys/class/gpio/gpio$pin/direction
this needs to be done only once.
2) write the state of the pin, thus switching the device on/off:
echo $newstate > /sys/class/gpio/gpio$pin/value
this is done every time this is required
3) read back the state of the pin
value=$(</sys/class/gpio/gpio$pin/value
this is done every time I want to check the state of the device
Now I switch a device on/off with lgpio as follows:
1) open the GPIO chip:
h = lgGpiochipOpen(0);
2) claim the pin as an output:
lgGpioClaimOutput(h, LG_SET_PULL_NONE, pin, value);
which, to my understanding, already changes the pin's state?!?
3) write the new state
lgGpioWrite(h, pin, value);
4) close the chip
lgGpiochipClose(h);
Reading back the state of the pin requires me to
1) open the GPIO chip:
h = lgGpiochipOpen(0);
2) claim the pin as an input:
lgGpioClaimInput(h, LG_SET_PULL_NONE, pin);
3) read back the pin's state
lgGpioRead(h, pin);
4) close the chip
lgGpiochipClose(h);
However ... When I set the pin's state to "1", I still read back "0"!
What am I doing wrong? Thanks in advance for any pointers.
Josef
Josef M?llers <josef@invalid.invalid> wrote:
I used to use the sysfs interface to the GPIO pins (/sys/class/gpio) but
I understand that is deprecated nowadays. So I tried to switch to lgpio
which looks OK.
Personally I switched to using the "gpio" command that's one of the
example programs included with the bcm2835 library. If you're just
trying to build C programs to replace the /sys/class/gpio devices
in a shell script, it's an existing option. It does have some bugs
though.
https://www.airspayce.com/mikem/bcm2835/
See examples/gpio/gpio.c in the source code.
However, I have problems writing and reading back pin
states from different programs.
[snip]
However ... When I set the pin's state to "1", I still read back "0"!
What am I doing wrong? Thanks in advance for any pointers.
At a GUESS, you're reading the input buffer instead of the output
buffer. In output mode the input is disabled and always reads zero
or is meaningless.
Unfortunately I've forgotten whether this is the case with the Pi,
or at least where to look to confirm I'm not mis-remembering, so
check for yourself. But this is a common way for IO hardware to
work.
Sysop: | deepend |
---|---|
Location: | Calgary, Alberta |
Users: | 254 |
Nodes: | 10 (0 / 10) |
Uptime: | 27:49:34 |
Calls: | 1,647 |
Files: | 4,022 |
Messages: | 389,299 |