nickc10
Posts: 26
Joined: Mon Oct 01, 2018 11:58 am

Python button script stopped working

Thu Jan 10, 2019 8:11 pm

Hi

The following script is designed to turn on an LED whilst a button is in a depressed state and turn the LED off when the button is released. When run however the LED comes on whether or not the button is held down and stays on regardless. I've tried all day to find out what the problem is but to no avail. Can anybody help me identify what might be the problem? Thanks!

Code: Select all

import RPi.GPIO as GPIO
import time
import os
from time import sleep
 
GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
GPIO.setup(10,GPIO.OUT)

print "LED ON"
GPIO.output(10,GPIO.HIGH)


while True:
    input_state = GPIO.input(7)
    if input_state == True:
        print "LED OFF"
        GPIO.output(10,GPIO.LOW)
        time.sleep(1)
        exit()

message = input("Press enter to quit\n\n") 

GPIO.cleanup()

pcmanbob
Posts: 6437
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python button script stopped working

Thu Jan 10, 2019 8:32 pm

Well running your program the LED turns on as soon as the program is run pressing the button turns it off then the program exits as per the program.

added notes

Code: Select all


print "LED ON"
GPIO.output(10,GPIO.HIGH)  #turns LED on


while True:
    input_state = GPIO.input(7)
    if input_state == True:
        print "LED OFF"  #turns LED off when button is pressed
        GPIO.output(10,GPIO.LOW)
        time.sleep(1)
        exit() # exits program 
        
don't forget you are also using board pin numbering. so pin 7 = gpio4 & pin 10 = gpio15

Edit....

this code will actually do what you say you want.

Code: Select all

import RPi.GPIO as GPIO
import time
import os
from time import sleep
 
GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
GPIO.setup(10,GPIO.OUT)

print "Program startLED OFF"
GPIO.output(10,GPIO.LOW)  #turns LED off


while True:
    input_state = GPIO.input(7)
    if input_state == True:
        print "LED ON"  #turns LED on when button is pressed
        GPIO.output(10,GPIO.HIGH)
        
    else:
        print "LED OFF"
        GPIO.output(10,GPIO.LOW)  #turns LED off when button is released
    time.sleep(1)
    
message = input("Press enter to quit\n\n") 

GPIO.cleanup()
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

nickc10
Posts: 26
Joined: Mon Oct 01, 2018 11:58 am

Re: Python button script stopped working

Thu Jan 10, 2019 9:02 pm

pcmanbob wrote:
Thu Jan 10, 2019 8:32 pm
Well running your program the LED turns on as soon as the program is run pressing the button turns it off then the program exits as per the program.

added notes

Code: Select all


print "LED ON"
GPIO.output(10,GPIO.HIGH)  #turns LED on


while True:
    input_state = GPIO.input(7)
    if input_state == True:
        print "LED OFF"  #turns LED off when button is pressed
        GPIO.output(10,GPIO.LOW)
        time.sleep(1)
        exit() # exits program 
        
don't forget you are also using board pin numbering. so pin 7 = gpio4 & pin 10 = gpio15

Edit....

this code will actually do what you say you want.

Code: Select all

import RPi.GPIO as GPIO
import time
import os
from time import sleep
 
GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
GPIO.setup(10,GPIO.OUT)

print "Program startLED OFF"
GPIO.output(10,GPIO.LOW)  #turns LED off


while True:
    input_state = GPIO.input(7)
    if input_state == True:
        print "LED ON"  #turns LED on when button is pressed
        GPIO.output(10,GPIO.HIGH)
        
    else:
        print "LED OFF"
        GPIO.output(10,GPIO.LOW)  #turns LED off when button is released
    time.sleep(1)
    
message = input("Press enter to quit\n\n") 

GPIO.cleanup()

Thanks however when I run your script all I get is LED OFF printing down the page. When I press the button nothing happens. The LED doesn't light at all.

I am definitely using the right pins. I checked using another button script I have and this turns the light on and off when pressing the button -

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(10, GPIO.OUT)  

try:
    while True:
         button_state = GPIO.input(7)
         if button_state == False:
             GPIO.output(10, True)
             print('Button Pressed...')
             time.sleep(0.2)
         else:
             GPIO.output(10, False)
except:
    GPIO.cleanup()

This is not what I want but at least it seems to show that things are working as they should.

pcmanbob
Posts: 6437
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python button script stopped working

Thu Jan 10, 2019 9:19 pm

The code you say works has a pull up on the button, your original code and my code has a pull down on the button

For your original code and my code to work wire button/LED like this.

Image

I would recommend adding a 1K protection resistor in the switch circuit in series with the switch.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

nickc10
Posts: 26
Joined: Mon Oct 01, 2018 11:58 am

Re: Python button script stopped working

Thu Jan 10, 2019 9:39 pm

pcmanbob wrote:
Thu Jan 10, 2019 9:19 pm
The code you say works has a pull up on the button, your original code and my code has a pull down on the button

For your original code and my code to work wire button/LED like this.

Image

I would recommend adding a 1K protection resistor in the switch circuit in series with the switch.

Thanks

Actually I have just modified an old door alarm script I had and got it to do what I was wanting. Both pins are connected to ground. I have a resistor on the led but not on the switch. You think that is OK?

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(10,GPIO.OUT)

# function for the door opening
def door_open():
    GPIO.output(10,GPIO.LOW)
    exit()

#initialise a previous input variable to 0 (assume button not pressed last)
prev_input = 0
while True:
  #take a reading
  input = GPIO.input(7)
  GPIO.output(10,GPIO.HIGH)
  #if the last reading was low and this one high, print
  if ((not prev_input) and input):
    door_open()
  #update previous input
  prev_input = input
  #slight pause to debounce
  time.sleep(0.1)

pcmanbob
Posts: 6437
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python button script stopped working

Thu Jan 10, 2019 9:57 pm

The 1K resistor is just there so that if you set the switch gpio pin to an output and your switch shorts it to a low/high voltage pin you don't damage the gpio pin due to high current flow.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

nickc10
Posts: 26
Joined: Mon Oct 01, 2018 11:58 am

Re: Python button script stopped working

Thu Jan 10, 2019 10:02 pm

pcmanbob wrote:
Thu Jan 10, 2019 9:57 pm
The 1K resistor is just there so that if you set the switch gpio pin to an output and your switch shorts it to a low/high voltage pin you don't damage the gpio pin due to high current flow.

Ok thanks. Sounds like a good idea.

So in my setup with the script I just posted I put the 1k on the switch and 220ohm on the led. Is that correct?

pcmanbob
Posts: 6437
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python button script stopped working

Thu Jan 10, 2019 10:15 pm

Updated my diagram to show 1K resistor added, does not matter which side of switch as log as its in series.

Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

nickc10
Posts: 26
Joined: Mon Oct 01, 2018 11:58 am

Re: Python button script stopped working

Thu Jan 10, 2019 10:24 pm

pcmanbob wrote:
Thu Jan 10, 2019 10:15 pm
Updated my diagram to show 1K resistor added, does not matter which side of switch as log as its in series.

Image
great thanks. that's really good to know. I was wondering earlier about whether there should be a resistor on the switch as many diagrams don't show one. very reassuring to know the answer to that as well as the reasons

Return to “Automation, sensing and robotics”