1

I have a simple circuit connected to my Raspberry Pi 4 with 3 push buttons and an LED. The buttons are connected to gpio2(pin 3), gpio17(pin 11), and gpio16(pin36), with the LED connected to gpio25(pin 22), all grounded through gnd(pin 6). When using a simple assignment with a button and LED the LED will turn completely off with LED.off(), but when I attempted to write its' functionality into a function the LED will remain on dimly lit.

Code for a single button with simple assignments, taken from the gpiozero library documentation:

from gpiozero import LED, Button
from signal import pause

led = LED(25)
button = Button(2)
button.when_pressed = led.on
button.when_released = led.off
pause()

Code for lighting LED in a function, using 3 buttons for testing:

from gpiozero import LED, Button
from signal import pause
from time import sleep

def blink_led():
    led = LED(25)
    led.on()
    sleep(0.5)
    led.off()

button1 = Button(2)
button2 = Button(17)
button3 = Button(16)
button1.when_pressed = blink_led
button2.when_pressed = blink_led
button3.when_pressed = blink_led
pause()

After any of the three buttons is pushed in the second code, the LED will turn on for the correct time interval, then it will "turn off" but still be dimly lit. I have to run a seperate small bit of code to turn it completely off:

from gpiozero import LED
led = LED(25)
led.off()

What is the reasoning behind the led still being dimly lit?

Note: there is a 330 Ohm resistor between the LED and gnd. gpiozero docs recommended a 220 Ohm but this is what I had. LED has a current rating of 20mA.

Edit: code is written in python 3.7.

5
  • You have no callback function for when_pressed in the first example.
    – Milliways
    Mar 16, 2021 at 5:21
  • @Milliways I don't think you need one. The library documentation has plenty of examples without callback functions. Also, the first example is directly from documentation.
    – tcolbert
    Mar 16, 2021 at 5:39
  • You are mistaken "when_pressed The function to run when the device changes state from inactive to active. This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like)."
    – Milliways
    Mar 16, 2021 at 5:41
  • What should the single "mandatory" paramater be in this case? The LED pin #? I still don't understand why this would cause the LED to remain on (albeit dimly lit), and allow the function to be called again with another button press.
    – tcolbert
    Mar 16, 2021 at 5:55
  • The LED presumably glows because of the pulldown. Your code will NEVER WRITE ANYTHING to the LED. I suggest you re-read the documentation. If you don't want to use a function use button.is_pressed.
    – Milliways
    Mar 16, 2021 at 6:01

0

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.

Browse other questions tagged or ask your own question.