1

Is it possible to read the pin state (1 or 0) whether it's set as output or input without setting this with RPi.GPIO.setup (pin, GPIO.IN/GPIO.OUT)? I mean, I can do this via this command:

gpio read 2

But when I try to do this in a Python script like this:

GPIO.input(pin)

It's says:

RuntimeError: you must setup() the GPIO channel first.

So it seems that I must specify pin input or output, but I don't want to do this!

6
  • It's a requirement of RPi.GPIO. There are other Python modules for gpio which don't have this requirement. Are you sure you know what you are doing? The setting of a gpio as an input or an output (or ALT0-ALT6) may well affect its level. How do you know the mode of the gpio?
    – joan
    Commented Sep 24, 2014 at 6:54
  • Thanks for reply. With this pin i control LED and i want to check their status, but i don't want to set this pin as output as long as i don't need this to just check pin value.
    – bercik
    Commented Sep 24, 2014 at 19:26
  • Can you reccomend some other python library which can control raspberry pi GPIO?
    – bercik
    Commented Sep 24, 2014 at 19:32
  • I'm still not sure I understand what you are trying to achieve. You could have a look at my Python module.
    – joan
    Commented Sep 24, 2014 at 19:39
  • What do you mean by "check status"? Do you want to find out if the pin is set as an input or an output? Or do you want to find out the logic level of the pin, regardless of whether it is set as an input or an output?
    – JRI
    Commented Sep 24, 2014 at 23:05

1 Answer 1

2

If you are using an operating system that has Sysfs support for GPIO (e.g. Raspbian), then you can use that to read the pin state from within Python. Note that this method uses BCM pin numbering.

To check the state of pin GPIO24, first you need to export it to userspace. As root, do the shell command

echo 24 > /sys/class/gpio/export

That creates a sysfs directory for the pin at /sys/class/gpio/gpio24/, containing various files you can read and write to to control the pin. You can access them via a Python script like this:

try:
  with open("/sys/class/gpio/gpio24/value") as pin:
    status = pin.read(1)
except:
  print "Remember to export the pin first!"
  status = "Unknown"

print status

This will work whether or not you have set up the pin using RPi.GPIO. However, if you do then use RPi.GPIO or another library to access the pin, you need to be aware that setting up a pin as an input or output may change its state. You will need to experiment!

eLinux.org has lots of useful information about accessing RPi Low-level peripherals and GPIO by different methods. There is also detailed documentation for the Sysfs GPIO interface.

2
  • 1
    The effect of echo 24 > /sys/class/gpio/export disappears after a restart (also without removing the power). What is a good way to make it survive a restart? Commented Jul 6, 2015 at 21:05
  • 1
    Peter - you can edit /etc/rc.local and put the line there at the end of the file... Commented Jul 7, 2015 at 7:44

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.