0

Im working on a university project with raspberry and gpiozero.

I need to manage the button response to take diferent actions.

The problem is that de function button.is_pressed its always true (even if nobody press the button). I tryed to read again and again the oficial gpiozero documentation but its the same as i type on my script.

The only thing I can imagine is that for some reason the button have a minimum voltage that python interpreter as a True in the is_pressed function.

import RPi.GPIO as GPIO
import random
from time import sleep
from gpiozero import LED
from gpiozero import Button

buttonRed = Button(13)
red       = LED(20)
while True:
    if buttonRed.is_pressed:
        sleep(.3)
        print('button pressed')
    else:
        print('nobody pressed')

this is the output:

%Run test1.py button pressed button pressed button pressed button pressed button pressed button pressed button pressed button pressed

Please if anybody have an idea of what is happening i would be glad to know about it.

Regards !!

  • Would it be possible to take a picture of the connections to your Raspberry Pi. And do you have a pull up or down resister? Please edit your original question and add these to the end. – NomadMaker Aug 4 '18 at 17:28
0

The pin number is NOT the physical board number but actually actually uses the BCM number (Broadcom). So if you want pin 13 you can see below that is BCM 27, the documentation says you can use GPIO number as pins but they do not work for me either. Image below is from https://pinout.xyz/

image from https://pinout.xyz/

#import RPi.GPIO as GPIO  <-this is not gpiozero library
import random
from time import sleep
from gpiozero import LED
from gpiozero import Button

buttonRed = Button(27)  #<-- 27 BCM is 13 on the Pi
red       = LED(20)
while True:
    if buttonRed.is_pressed:
        sleep(.3)
        print('button pressed')
    else:
        print('nobody pressed')
  • thanks for your answer but. it do not work. It is not a pin problem. In the script a forgot put this: GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False), even so it do not work. I can use the same base script and insted of that use led.on and led.off and it work like a charm. example: – m4verick Nov 2 '17 at 21:13
0

I can do this:

import RPi.GPIO as GPIO
import random
from time import sleep
from gpiozero import LED
from gpiozero import Button

## I forgot put this in the befor script !!!
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

buttonRed    = Button(13) 
buttonGreen  = Button(6)
buttonBlue   = Button(5)
buttonYellow = Button(22)

red      = LED(20)
green    = LED(12)
blue     = LED(25)
yellow   = LED(24)

while True:
    inputValue1 = GPIO.input(13)
    if (inputValue == True):  <<----(if buttonRed.is_pressed:)
        print('button pressed')
        red.on()
        sleep(.3)
        red.off()
    else:
        print('nobody pressed')

And the red (are using gpio LED and the BCM pin numbered) and work perfectly.The red led y have on my raspi conected to a protoboard start on and off as well i press boton. Insted of that, the buttonRed.is_pressed do not work with the same pin numbered, so i fixed by using something more basic "inputValue1 = GPIO.input(13)", but i do not want used it. If a have imported gpiozero library its just for using all the functions !!!

Finally, i forgot to mention that i' ve worked with a raspberry PI 2 so the pins number are right.

Regards !!!

  • that is not using GPIOZero library . GPIO.input(13) is using the RPI.GPIO library. If you want to use GPIOZero buttonRed.is_pressed then you need to use BCM – Dr.Rabbit Nov 2 '17 at 21:38
  • obiusly it is not gpiozero library, I just using to demostrate that if I solve the button pressed problem, then using gpiozero library to manage the leds conected to a raspi, the library and the pin numbered worked perfect, because the script resolve the red = LED(20) and then do red.on() and red.off() and work fine, the led start brighting and then it turn off and all of this its using gpiozero library. – m4verick Nov 2 '17 at 21:52

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.