bw1pi
Posts: 17
Joined: Mon May 18, 2015 6:13 pm

GPIOzero button Migrating from RPi.GPIO

Tue Mar 12, 2019 7:48 pm

I have a simple RPi.GPIO that Blinks an output and reads that as an input.

Code: Select all

import RPi.GPIO as GPIO						
import time

GPIO.setmode(GPIO.BCM)							
# Variable for the BCM GPIO pin number
READ_1 = 2
LED_1 = 16	
# Setup the GPIO pins for output
GPIO.setup(READ_1, GPIO.IN)
GPIO.setup(LED_1, GPIO.OUT)
# Loop to blink our LEDs
try:
  while True:
    print("On 1")
    GPIO.output(LED_1, GPIO.HIGH)
    time.sleep(1)
    IN1 = GPIO.input(READ_1)
    print("On 1 reads : ",IN1)
    GPIO.output(LED_1, GPIO.LOW)
    time.sleep(1)
    IN1 = GPIO.input(READ_1)   
    print("Off 1 reads : ",IN1)
except KeyboardInterrupt: # Ctrl-C to stop    
    GPIO.cleanup(LED_1)
    pass
Now I am trying to migrate to GPIOzero.
The LED outputs work fine, but the button value has had issue.

The First gpiozero button issue with error "Failed to add edge detection"
is fixed thanks to : bcroston commented on Nov 3, 2018

Now the code is the issue.
I do not want to wait, just read the current state, so I think the correct usage is:
button.value
Using the button as a switch from GND to pin2 works.
Using the button as a LED output pin sense stay FALSE with the output turning on an LED through a resistor?
I tried pull_up=True/False with no difference?

Code: Select all

from gpiozero import LED, Button 
from time import sleep
						
# Variable for the BCM GPIO pin number
button = Button(2)
LED_1 =	LED(16)	
# Loop to blink our LEDs
try:
  while True:
    print("On 1")
    LED_1.on()
    sleep(1)
    read1 = button.value
    print("On 1 read ",read1)
    print("Off 1")
    LED_1.off()
    sleep(1)
    read1 = button.value
    print("Off 1 read ",read1)
    
except KeyboardInterrupt: # Ctrl-C to stop
    LED_1.off()   
    print("Clean up done")
    pass

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: GPIOzero button Migrating from RPi.GPIO

Fri Mar 15, 2019 12:36 am

You can't pull-down GPIO2 or GPIO3 as there are physical pull-up resistors on those pins.

Try just using the button on its own and see if you can get the right results:

Code: Select all

from gpiozero import Button

button = Button(2)

while True:
    if button.is_pressed:
        print("pressed")
    else:
        print("released")
If that works, you're good. If not, try another pin (not 2 or 3) and see if pull_up makes a difference.

You can use:

- button.is_pressed
- button.value
- button.wait_for_press()
- button.wait_for_release()

Also try led.blink() to save yourself from trying to blink in a loop.

Your button doesn't seem to do anything in the script except print when it's pressed. Try:

Code: Select all

from gpiozero import LED, Button

led = LED(16)
button = Button(2)

while True:
    if button.is_pressed:
        print("pressed")
        led.on()
    else:
        print("released")
        led.off()
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

Return to “Python”