Monthly Archives: February 2015

Raspberry Pi: how to read pin status without shutting it down in Python

From now on, sometimes I would like to publish results of internet searching… that I did NOT find. The results I wanted and I found after just after a long wandering.


Raspi-PGB001_reasonably_small When you work with Raspberry Pi, and you are controlling something via output port, say a relay, you can have set a status on it, i.e. you can have put it HIGH, with the usual

import RPi.GPIO as GPIO
GPIO.setup(25, GPIO.OUT)
GPIO.output(25, HIGH)

Then, some times after, you are willing to know if pin 25 is HIGH o LOW. You have forgot it, or you are not you, but another person willing to know that. Problem: if you set pin 25 as input, you suddenly switch off it. So DO NOT DO this, in the same or another program:

GPIO.setup(25, GPIO.IN)
print GPIO.input(25)

because you will always get a False, at least with default pullups.

Fortunately the answer is simple: just set that pin as output… but read it!

GPIO.setup(25, GPIO.OUT)
print GPIO.input(25)

and you will get a True or a False depending on the real status.