I'm quite new to Pi programming. I know this question has been asked before but things do seem to have changed with Pi 5 and the latest Python libraries.
What is a way for 2 Python programs to access GPIO at the same time?
I took the blink led example from here (https://pypi.org/project/gpiod/) and modified it. Code below. The first instance runs fine but when I run a second instance I get:
File "/home/admin/venv/lib/python3.11/site-packages/gpiod/chip.py", line 331, in request_lines req_internal = cast(_ext.Chip, self._chip).request_lines( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OSError: [Errno 16] Device or resource busy This is not entirely surprising but how to work around it?
My code:
import gpiod import time from gpiod.line import Direction, Value OUTLINE = 22 INLINE = 27 with gpiod.request_lines( "/dev/gpiochip4", consumer="blink-example", config={ OUTLINE: gpiod.LineSettings( direction=Direction.OUTPUT, output_value=Value.ACTIVE ), INLINE: gpiod.LineSettings( direction=Direction.INPUT ) }, ) as request: while True: request.set_value(OUTLINE, Value.ACTIVE) print(request.get_value(INLINE)) time.sleep(1) request.set_value(OUTLINE, Value.INACTIVE) time.sleep(1) print(request.get_value(INLINE)) I tried something similar with gpiozero and got a similar problem. Chatgpt suggests libgpiod should support multiple access but Chatgpt can't produce an example with the latest Python API.
gpiodis a poorly documented library (with different incompatible API in different versions) - not really suitable for beginners.