1

I can't seem to find a solution of doing this using gpiozero. I've done it using rpi.gpio with add_event_detect and call back to change state but I was under the impression gpiozero is much simpler.

This link https://projects.raspberrypi.org/en/projects/physical-computing/9 basically says a switch would turn the led on with a pressed and release then turn is off with another pressed and release but doesn't actually show how to do this.

from gpio import LEDBoard, Button

button = Button(5, pull_up=True, bounce_time=200)
leds = LEDBoard(26, 19, 13, 6)

def on():
leds.on()
print('On...')

def off():
leds.off()
print('off...')

button.when_pressed = on
button.when_released = off

It doesn't even turn off when released. Is that due to the pull_up = True? I thought this code would turn it on when pressed and off when released but it just turns on and stays on.

TIA!

  • You are asking us to guess how your button is wired up. – joan May 7 at 10:05
  • @joan Oh, I'm sorry! didn't think it was relevent. Honestly thought this was just a python issue. The button is connected to ground pin and gpio5 pin, leds are connected to ground and 4 gpio pins. – DaviebPrime May 7 at 10:19
  • ...and by gpio5 pin you mean pin 29? See pinout.xyz – joan May 7 at 10:25
  • 1
    In that configuration the button will normally read high (1) because of the pull-up to 3V3. When you press the button it is connected to ground and the button will read low (0). When you release the button it will again read high. – joan May 7 at 10:30
  • 1
    Unless I am mistaken that code will do nothing. It will run and immediately exit. – Milliways May 7 at 22:57
0

Question

How to toggle a button ... ?

Answer

Well, the OP's "question" is not a question,so I can give no answer. But I can explain why the OP's "question" is not a question.

  1. A human can press a button and then release the button. This is not a "toggle". Let us say if the human presses button and then releases, and after some time, the human presses the button again and releases. Now there are two sequences of press event and release event, or if you like, one sequence of four events: press, release, press, release. No "toggling" happened ever.

  2. Now the LED. Python can "toggle" a LED, ie, if the LED is on, then turn it off, and vice versa. This is the meaning of "toggling" a LED.

In other words, python can toggle a LED, but never a button, because only a human can change the status of a button: being pressed, being released. I know it is confusing. Perhaps a picture helps.

led toggle

The picture below tells (1) how to toggle LED, and (2) how to read button.

button and led

  • 1
    @tifong01: Ok. My terminology has been a bit off. I meant how do I turn a button into a switch? So I need 2 sequences to turn it off and on. Can you help me? the picture didn't help. button.wait_for_press() then led.toggle sleep(0.5). button.wait_for_press() `led.off(). That just turns the Leds on and they stay on. At the very least it should toggle? – DaviebPrime May 7 at 13:33
  • Well,let you show you a switch. – tlfong01 May 7 at 14:04
  • I have shown the led and button picture. See you tomorrow. – tlfong01 May 7 at 14:36

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.