1

This is my code to light up my LED when I press 'l' character. In first run it works as expected. But in the second run the LED is light up at the startup. But why?? I used GPIO.cleanup() at the end.

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)

def getch():
    ch = input("Input: ")
    return ch

print("Enter l to  lightup")
print("Enter any key to exit")

while True:
    char = getch()

    if(char=="l"):
        GPIO.output(11,GPIO.HIGH)
    else:
        break
GPIO.cleanup()
2

cleanup will change the state of each GPIO you have used to be an INPUT (it will also disable any internal resistor pulls for that GPIO).

When you next set a GPIO to be an OUTPUT the GPIO hardware remembers the last written state. So if it was last written HIGH it will be still be HIGH when set as an OUTPUT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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