# GPIO pin output stays high after setting to low

I'm working on a project in which I have to set a GPIO pin of the Raspberry Pi to high/low in order to signal a transponder which in turn communicates with a lock and opens it when it receives a high signal. For reading and writing to the GPIO, the RPi.GPIO library for Python is used.

All goes well and executes as it should on the first run. However, it appears the output of the pin remains high even after it should have been set to low. So when the script has been executed once after booting the Raspberry Pi, the pin remains high. The script executes without any warnings and/or errors. The continuous high signal confuses the transponder.

import RPi.GPIO as GPIO
import time

class GPIODoor(object):
"""Can be used to open a door at physical/Board pin 5.
In the current revision (2) this is known as GPIO 01"""
def __init__(self):
GPIO.setmode(GPIO.BOARD)
GPIO.setup(5, GPIO.OUT)

def openDoor(self, duration = 0):
"""Sends the open door command to the transponder.
@duration [Optional] the time the relay is switched to high"""
GPIO.output(5, True)
time.sleep(duration)
GPIO.output(5, False)
GPIO.cleanup()
print('1')

if __name__ == '__main__':
door = GPIODoor()
door.openDoor()


Above script is being called from a PHP script as follows:

// some irrelevant class code
exec('sudo python /path/to/script.py', $output); if ($output[0] == '1' ) {
return true;
}
return false;


Note that the given path is not the actual path used. In order to allow PHP to execute the Python script, the www-data user is given the permission to do so in the /etc/sudoers file. This is similar to what is described here.

Now my question is: is anyone able to tell me why the pin remains high? Is it a mistake in my script? Am I missing some initial setup of the GPIO?

• I think a few gpios stay high always.......like gpio 3,5,7 and 8. if you have enabled a few things like spi...Atleast i have experienced this....! – Sukhpreet Singh Oct 6 '15 at 13:06

You call GPIO.cleanup(), which will turn the OUTPUT pin 5, back into an INPUT.

• Aren't you supposed to call GPIO.cleanup() when finishing the script in order to reset everything? All the script has to do is make sure the pin has a high output for a short period of time in order to signal the transponder to open the lock. Then the pin should return to its default status, which is low. Also, without the call to GPIO.cleanup() the script generates a warning on executing saying that the channel is already in use. – Kaj Nelissen Jul 11 '13 at 12:51
• Ideally you should call it. But you can just skip it without any problems. The warning message can safely be ignored. Ideally you'd have a separate script that only run on start-up, and sets pin 5 to OUTPUT. Then in your doors-script you'd omit the setup call. That way you won't get any errors, and the pin will remain output-low between calls. But I'd personally not go this far. – Gerben Jul 11 '13 at 13:03
• I tried your solution, but it didn't solve the error. I did the following things: I created the file GPIOInit.py which contains a class GPIOInit which calls GPIO.setmode(GPIO.BOARD) and GPIO.setup(5, GPIO.OUT) in its __init__ method. The file is made executable with chmod +x GPIOInit.py. Then I added the line sudo python /home/pi/GPIOInit.py to the file /etc/rc.local so the GPIO pin would have its setup on boot. I also removed the setup calls and cleanup from the door script. – Kaj Nelissen Jul 11 '13 at 14:02
• Did the original solution of simply removing the cleanup() work? – Gerben Jul 11 '13 at 16:57
• I did some testing, and it seems you can't really prevent those warnings, even with the separate initialize script. Though you could add GPIO.setwarnings(False) to hide them :-). Just learn to live with them. – Gerben Jul 11 '13 at 17:03

## protected by goldilocks♦Oct 6 '15 at 13:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).