0

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.

7
  • think about this similar problem ... three people cannot all drive one car ... how can those three people get around town in that car to the places they want to visit? Commented Oct 6 at 17:01
  • Unfortunately, there's many "out-of-date" (i.e. redundant for Pi 5) examples on google – see Milliways comment below. Been asked already, but no answer: Using same GPIO pins simultaneously from two Python scripts using RPi.GPIO. Also, with an answer, on SO 2 Python scripts use the same GPIO pin RPI and also Running two GPIO python scripts at once. Commented Oct 6 at 19:03
  • 1
    What OS is installed (detail including version)? Commented Oct 6 at 21:40
  • "I tried something similar with gpiozero" but haven't listed the code. gpiod is a poorly documented library (with different incompatible API in different versions) - not really suitable for beginners. Commented Oct 6 at 21:46
  • 1
    @Greenonline those examples are old, using different libraries and different SOC and no amount of fiddling will override the safeguards built into the kernel drivers which only allow exclusive access to GPIO on Pi5. It is possible for multiple programs to use the same pins, provided they release them after use. Commented Oct 6 at 22:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.