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():

def 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.


  • You are asking us to guess how your button is wired up.
    – joan
    May 7, 2019 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.
    – user100971
    May 7, 2019 at 10:19
  • ...and by gpio5 pin you mean pin 29? See pinout.xyz
    – joan
    May 7, 2019 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, 2019 at 10:30
  • 1
    Unless I am mistaken that code will do nothing. It will run and immediately exit.
    – Milliways
    May 7, 2019 at 22:57

3 Answers 3



How to toggle a button ... ?


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?
    – user100971
    May 7, 2019 at 13:33
  • Well,let you show you a switch.
    – tlfong01
    May 7, 2019 at 14:04
  • I have shown the led and button picture. See you tomorrow.
    – tlfong01
    May 7, 2019 at 14:36

Are you asking to have the LED on when the button is pressed and off when the button is unpressed? If so, this section from the gpiozero docs explains how to do so:

"Turn on an LED when a Button is pressed:

from gpiozero import LED, Button 
from signal import pause 

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

button.when_pressed = led.on
button.when_released = led.off 



from gpiozero import LED, Button 
from signal import pause 

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

led.source = button 


There is a toggle function in GPIO zero. So if you want a single LED on pin 26 to change state every time a button on pin 5 is pressed:

from gpiozero import LED, Button
from time import sleep

led = LED(26)
button = Button(5)

while True:
    if button.is_pressed:

(The sleep is needed, otherwise it changes state while you're pressing the button, and all you get is a strobing LED when you push the button)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.