1

I'd like to get the current export state (output / input) of a GPIO pin on a RaspberryPi using JavaScript.

Due to the fact that I've a low-active relay, which is activated in the second I export out a GPIO-pin, I need to be able to specific control the export states.

The idea is, that when I export out a GPIO pin using the pi-gpio library:

gpio.open(17, "output", function(err) {
    // do something...
}

and some other functions / applications are working too, the GPIO pin could be in any state (exported in / out).

Does anyone know how to check the export state of a GPIO pin, in order to determine whether it is currently in output mode or input mode?

1
  • could you write a for loop that checks GPIO pins 0-31 for their respective states? like gpio read $Pin would tell you. as a system command. are you opposed to making system calls?
    – j0h
    Sep 29, 2016 at 17:50

1 Answer 1

3

If GPIO X has been exported then the directory /sys/class/gpio/gpioX will exist.

E.g. if GPIO 4 has been exported

$ ls /sys/class/gpio
export  gpio4  gpiochip0  gpiochip100  unexport

If it has been exported the directory will have the following entries.

$ ls /sys/class/gpio/gpio4
active_low  device  direction  edge  power  subsystem  uevent  value

To check if it is in input or output mode read the direction file.

$ cat /sys/class/gpio/gpio4/direction  
in

It will be in for an input and out for an output.

1
  • sysfs tho. super slow.
    – j0h
    Sep 29, 2016 at 17:51

Your Answer

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

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