2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Need help please - Python push button code

Thu Aug 29, 2019 2:09 am

Hi all-

I am brand new to Raspberry Pi and Python. I have an assignment I am working on and cannot get it to work. Any help would be greatly appreciated!!

The task is: "Build a circuit using your Raspberry Pi that causes an LED to blink when a push button is NOT pressed. However, the LED should stay on continually when the push button IS pressed."

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(22,GPIO.IN)

while True:

    if (GPIO.input(22)):
        GPIO.OUTPUT(12, TRUE)

    else:
        GPIO.input(12, TRUE)
        time.sleep(0.5)
        GPIO.OUTPUT(22, FALSE)
        time.sleep(0.5)
ERROR RECIEVED:
Traceback (most recent call last):
File "/home/pi/mu_code/ledbtn2.py", line 11, in <module>
GPIO.OUTPUT(12, TRUE)
AttributeError: module 'RPi.GPIO' has no attribute 'OUTPUT'
>>>

Thanks in advance!!!

cmrincon
Posts: 36
Joined: Thu May 24, 2018 7:39 pm

Re: Need help please

Thu Aug 29, 2019 5:34 am

Look at the error mensage

What python is trying to say you is that "OUTPUT" doesn't exist in the GPIO module, instead, "output" does. So GPIO.output(12, True) will work
Notice that python is case sensitive
Also notice that

Code: Select all

GPIO.OUTPUT(22, TRUE)
is not correct. #22 is not the pin conected to the led. so you will have to change it in order to succeed

drgeoff
Posts: 9821
Joined: Wed Jan 25, 2012 6:39 pm

Re: Need help please

Thu Aug 29, 2019 7:40 am

Indents matter in python. To preserve them in a post use the Code tags. For example

Code: Select all

if something:
  do this

DirkS
Posts: 9947
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Need help please

Thu Aug 29, 2019 8:27 am

AttributeError: module 'RPi.GPIO' has no attribute 'OUTPUT'
Python code is case sensitive:

Code: Select all

GPIO.output(22, False)

2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Re: Need help please - Python push button code

Thu Aug 29, 2019 10:42 pm

I cannot thank you guys enough! So the code does not have errors and when connected the LED blinks. What it is not doing is interacting with the button. I do have the bottom on board pin 22 and the LED on board pin 12. Thoughts?

There were a couple comments that the PIN was not on 22 but I have counted repeatedly and it is in fact on 22. So close yet so far away.

Again, thank a million for the assist!!!!!1

User avatar
neilgl
Posts: 1007
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Need help please - Python push button code

Fri Aug 30, 2019 12:04 am

Your code has two inputs, 22 and 12 - is that correct?

2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Re: Need help please - Python push button code

Fri Aug 30, 2019 1:37 am

That’s correct. LED is 12 and the button is on 22.

2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Re: Need help please - Python push button code

Fri Aug 30, 2019 2:58 am

So I edited the PIN’s slightly and again the LED flashes but does not remain solid when I push the button. Actually pushing the button does nothing. Here is the new code. No errors, just does not remain solid when pushing the button.

Code: Select all

import PRI.GPIO as GPIO
Import time

Ledpin = 8 # L in LedPin is not capitalized in code
ButPin = 22 # B in ButPin is not capitalized in code

GPIO.setwarnings(Fasle)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPin, GPIO.OUT)

GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO>PUD_UP)

GPIO.output(ledPin, GPIO.HIGH)

Try:
While 1:
If GPIO.input(butPin):
GPIO.output(ledPin, GPIO.HIGH)
Time.sleep(0,075)
GPIO.output(ledPin, GPIO.LOW)
Time.sleep(0.075)
Else:  # button is pressed:
GPIO.output(ledPin. GPIO.HIGH)

Except Keyboardinterrupt:
Pwm.stop()
GPIO.cleanup()
# I tried to type this using the code button but not sure if I did it right. The indentations are not showing up correctly on here but are on the actual code.

The LED is pin 8 and the button is on pin 22.

Hope someone can help me get this across the finish line.

Thanks again!!!

User avatar
rpdom
Posts: 15238
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Need help please - Python push button code

Fri Aug 30, 2019 3:55 am

2wheelz4me wrote:
Fri Aug 30, 2019 2:58 am
I tried to type this using the code button but not sure if I did it right. The indentations are not showing up correctly on here but are on the actual code.
Please don't type the code in by hand, use copy and paste instead. Indentations should show up.

Also, I see mistakes/typos in that code that mean it would not even run. These lines for example

Code: Select all

import PRI.GPIO as GPIO

Code: Select all

GPIO.setwarnings(Fasle)
and

Code: Select all

Ledpin = 8 # L in LedPin is not capitalized in code
ButPin = 22 # B in ButPin is not capitalized in code
You've even put comments on those two lines to say why they are wrong! Actually, a lot of the code as typed is capitalised when it shouldn't be.

2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Re: Need help please - Python push button code

Fri Aug 30, 2019 1:33 pm

Understood on typing the code by hand however i do not have WiFi setup so i cannot send or share the actual code. Again the code is working with no errors, its simply not responding to the button.

Thanks again!

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

Re: Need help please - Python push button code

Fri Aug 30, 2019 2:49 pm

So cleaning up your code

Code: Select all

import RPi.GPIO as GPIO
Import time

Ledpin = 8 
ButPin = 22 

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPin, GPIO.OUT)

GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO>PUD_UP)

GPIO.output(ledPin, GPIO.HIGH)


While 1:
    if GPIO.input(butPin):
        GPIO.output(ledPin, GPIO.HIGH)
        Time.sleep(0,075)
        GPIO.output(ledPin, GPIO.LOW)
        Time.sleep(0.075)
    else:
    	GPIO.output(ledPin. GPIO.HIGH)
    
when you program runs after doing the set up,

LED will be on this line does that

Code: Select all

GPIO.output(ledPin, GPIO.HIGH)

then your while true loop runs,

if the button is pressed your LED will flash, these lines do that

Code: Select all

    if GPIO.input(butPin):
        GPIO.output(ledPin, GPIO.HIGH)
        Time.sleep(0,075)
        GPIO.output(ledPin, GPIO.LOW)
        Time.sleep(0.075)
        
if the button is not pressed the the LED will just stay on, these lines do that

Code: Select all

    else:
        GPIO.output(ledPin. GPIO.HIGH)
        
So as far as I can see your code is doing exactly what you asked it to, when the button is not pressed then the LED is on , when the button is pressed it flashes the LED.
So what exactly is your problem or what do you want the code to actually do ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Re: Need help please - Python push button code

Fri Aug 30, 2019 3:29 pm

Thank you! Actually trying to:
LED to blink when a push button is NOT pressed. However, the LED should stay on continually when the push button IS pressed.

I am not getting any error messages and the LED does blink when I run the code but when I press the button it does not stay solid and simply continues to blink.

Thanks again!

User avatar
neilgl
Posts: 1007
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Need help please - Python push button code

Fri Aug 30, 2019 3:30 pm

To save a lot of typing, try this

Code: Select all

from gpiozero import LED, Button
from signal import pause
led = LED(23)
button = Button(16)
led.blink()
button.when_pressed = led.on
button.when_released = led.blink
pause()

2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Re: Need help please - Python push button code

Fri Aug 30, 2019 3:46 pm

I tried this latest suggestion and it did not work. The LED is sold but no blink with button pushed or not pushed. Here is my full code and the error is below that.

Code: Select all

import RPi.GPIO as GPIO
import time

from gpiozero import LED, Button
from signal import pause

ledPin = 8
butPin = 22

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPin, GPIO.OUT)

GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.output(ledPin, GPIO.HIGH)


led = LED(23)
button = Button(16)
led.blink()
button.when_pressed = led.on
button.when_released = led.blink
pause()

try:
    while 1:
        if GPIO.input(butPin):
            GPIO.output(ledPin, GPIO.HIGH)
            time.sleep(0.075)
            GPIO.output(ledPin, GPIO.LOW)
            time.sleep(0.075)
        else:  # button is pressed:
            GPIO.output(ledPin, GPIO.HIGH)

finally:
    ('DONE')

GPIO.cleanup()
Error message is:
/usr/lib/python3/dist-packages/gpiozero/devices.py:279: PinFactoryFallback: Falling back from rpigpio: A different mode has already been set!
'Falling back from %s: %s' % (name, str(e))))
/usr/lib/python3/dist-packages/gpiozero/devices.py:279: PinFactoryFallback: Falling back from rpio: No module named 'RPIO'
'Falling back from %s: %s' % (name, str(e))))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)

Did you start the pigpio daemon? E.g. sudo pigpiod

Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888

Did you specify the correct Pi host/port in the
pigpio.pi() function? E.g. pigpio.pi('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/usr/lib/python3/dist-packages/gpiozero/devices.py:279: PinFactoryFallback: Falling back from pigpio: failed to connect to localhost:8888
'Falling back from %s: %s' % (name, str(e))))

User avatar
B.Goode
Posts: 8611
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Need help please - Python push button code

Fri Aug 30, 2019 3:50 pm

I tried this latest suggestion and it did not work.

The code you have posted is not the same as that proposed by @neilgl in the preceding post.

His version has only 8 lines.

2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Re: Need help please - Python push button code

Fri Aug 30, 2019 3:59 pm

The code below works with no errors but the LED will not change from blinking to solid when I press the button:

Code: Select all

import RPi.GPIO as GPIO
import time

ledPin = 8
butPin = 22

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPin, GPIO.OUT)

GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.output(ledPin, GPIO.HIGH)

try:
    while 1:
        if GPIO.input(butPin):
            GPIO.output(ledPin, GPIO.HIGH)
            time.sleep(0.075)
            GPIO.output(ledPin, GPIO.LOW)
            time.sleep(0.075)
        else:  # button is pressed:
            GPIO.output(ledPin, GPIO.HIGH)

finally:
    ('DONE')

User avatar
B.Goode
Posts: 8611
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Need help please - Python push button code

Fri Aug 30, 2019 4:21 pm

Have you considered that this might be a 'hardware' issue?

Maybe your switch or button is faulty, or maybe you have wired it incorrectly?


For a simple standalone test, refer to gpiozero again:
https://gpiozero.readthedocs.io/en/stab ... tml#button

User avatar
neilgl
Posts: 1007
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Need help please - Python push button code

Fri Aug 30, 2019 4:47 pm

As you are using GPIO.setmode(GPIO.BOARD), I changed my wiring to match yours and used your pins (8, 22). Your code ran perfectly. Good.

Here is the example again, with pins changed to match your existing wiring.

Code: Select all

from gpiozero import LED, Button
from signal import pause
led = LED(14)
button = Button(25)
led.blink()
button.when_pressed = led.on
button.when_released = led.blink
pause()
What happens when you run the above code?

2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Re: Need help please - Python push button code

Fri Aug 30, 2019 7:11 pm

So when I ran the code nielgl provided nothing happens other than the blinking led. So i did the button tests provided and it appears maybe both my buttons are defective as nothing happens on any of the tests. I'll try to find a button somewhere and re-try. Hopefully that fixes things. Thanks so much everyone!

User avatar
joan
Posts: 14274
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Need help please - Python push button code

Fri Aug 30, 2019 7:19 pm

You may have wired them incorrectly. Clear photos of your set-up will almost always be helpful.

User avatar
neilgl
Posts: 1007
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Need help please - Python push button code

Fri Aug 30, 2019 7:35 pm

Is the button in a breadboard or connected to the GPIO using wires (DuPont cables)?

2wheelz4me
Posts: 10
Joined: Thu Aug 29, 2019 1:56 am

Re: Need help please - Python push button code

Fri Aug 30, 2019 10:26 pm

Finally got it! I went and picked up new button's and it works like a champ now. For whatever reason, both my buttons were defective. All set and again, really appreciate the help from everyone!!

Return to “Beginners”