When initializing the pins on Raspbian, there appears to be a timing issue between the /sys/class/gpio/export and the write to /sys/class/gpio/direction
periphery/gpio.py
After running the code the first time the pin is added to the /sys/class/gpio/gpio{pin_number} list and therefore it works on the second try.
It looks like it could be addressed by adding a wait time if it fails. Here's a wrapper which addresses the issue:
for _ in range(number_of_attempts): try: gpio_pin = GPIO(pin, direction) break except GPIOError as e: if _+1 == number_of_attempts: raise e time.sleep(0.05) return gpio_pin
When initializing the pins on Raspbian, there appears to be a timing issue between the
/sys/class/gpio/exportand the write to/sys/class/gpio/directionperiphery/gpio.py
After running the code the first time the pin is added to the /sys/class/gpio/gpio{pin_number} list and therefore it works on the second try.
It looks like it could be addressed by adding a wait time if it fails. Here's a wrapper which addresses the issue:
for _ in range(number_of_attempts): try: gpio_pin = GPIO(pin, direction) break except GPIOError as e: if _+1 == number_of_attempts: raise e time.sleep(0.05) return gpio_pin