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?

user3191334
  • 305
  • 1
  • 7
  • 15

1 Answers1

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.

joan
  • 71,852
  • 5
  • 76
  • 108