I'm currently working with Raspberry Pi. I have implemented push button(switch) program using python. But, When I press the switch, then if condition is true and data stored but when I release the switch button my program is stuck. Why?

My script:


import RPi.GPIO as GPIO

switch_1 = 17


GPIO.setup(switch_1, IN.OUT)

    sw1_status = GPIO.input(switch_1)

    print 'Switch_1 : ', sw1_status

    if sw1_status == True:
        print ('Data stored')
        while(sw1_status != 0): // here my program stuck.
  • This is pretty neat.
    – wogsland
    Commented Mar 1, 2017 at 20:19

2 Answers 2


Your program is stuck because you have made an infinite loop in it. By the time you get to the while(sw1_status != 0) line, sw1_status is guaranteed to be True, and True != 0 will always evaluate to True.

Perhaps you meant to wait until the button is released, in which case you want to update sw1_status in the loop, by replacing pass with sw1_status = GPIO.input(switch_1).


Also, I have used

while((GPIO.input(switch_1)) == 1):

instead of

while(sw1_status != 0): // here my program stuck.

It's working fine.

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.