I am using Ruby to drive output GPIO pins using the old SYSFS pseudo-filesystem interface, and am regularly encountering intermittent Errno::EACCES: Permission denied @ rb_sysopen errors.

My user is a member of the gpio group. The pseudofiles involved are writeable by the gpio gpoup.

The following is the simplest failure I've been able to boil it down to (a pry session), using a gem called gpio_sysfs, which is an extremely simple wrapper around reads and writes to files within /sys/class/gpio/:

[18] pry(main)> p0 = GpioSysfs::Pin.new(0)
#<GpioSysfs::Pin:0x0000005574264408 @pin=0>
[19] pry(main)> p0.value = true
Errno::EACCES: Permission denied @ rb_sysopen - /sys/class/gpio/gpio0/direction
from /home/slack/.gem/ruby/3.0.3/gems/gpio_sysfs-0.1.0/lib/gpio_sysfs/pin.rb:30:in `write'
[20] pry(main)> p0.direction = "out"
[21] pry(main)> p0.value = true
true # The output switches high at this point

When p0.value=() is called, the library first writes to gpio0/direction using the same call that I have used on line [20].

Does anybody know why this call fails the first time and succeeds the second?

Hardware is a model 3B and system is a recently installed Raspberry Pi OS Bullseye (64 bit, lite install).

1 Answer 1


Most likely a timing problem.

There are race conditions with the sysfs. You need to wait for Linux to set the GPIO to output mode before writing to the GPIO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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