0

There's two applications, A and B.

A prepares to use a GPIO pin then starts doing its tasks.

After a while, B is also launched and prepares to use the same GPIO pin by A. A warning pops up, but this is only a warning.

RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.

Could B also toggle the same GPIO pin, potentially damaging A's work? What options do I have to prevent this?

I would love to set up an experiment, but all my Pis are currently in use.

1
  • If this is a duplicate, feel free to flag and link me there. I could not find info about this.
    – Aloha
    Jan 21 at 9:12
1

Yes this can and will happen.

e.g. the following scripts both run concurrently and end up toggling the same GPIO.

#!/usr/bin/env python

import time
import RPi.GPIO as GPIO

gpio=22

GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio, GPIO.OUT)

try:
   while True:
      GPIO.output(gpio, 1)
      time.sleep(0.1)

except:
   raise
#!/usr/bin/env python

import time
import RPi.GPIO as GPIO

gpio=22

GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio, GPIO.OUT)

try:
   while True:
      GPIO.output(gpio, 0)
      time.sleep(0.1)

except:
   raise

There is no way to prevent this. A lot of the GPIO modules write to the GPIO registers directly for performance reasons.

The new /dev/gpiochip does prevent this situation for conforming applications as only one can claim a given GPIO at any one time. However this doesn't help as the direct access modules do not conform.

1
  • Experimentally verified. Code review of a Python GPIO library also confirms it.
    – Aloha
    Jan 21 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.