0

I am using the gpiozero library to controll a LED strip and a relay connected to my raspberry pi 4. This is the way I connected the LED strip to the pins. I use a type SMD5050 LED strip similar to the one in this blog post (you can chose English in the top right corner). Below is the code that controlls my LED, from which I call toggle_led() in a small Flask app (python3).

Now when I call the command the LED strip changes color, so that's good. But for some reason the color fades after 7-8 seconds back to white light without any further interaction. Also, I can only toggle one color at a time. If I switch on red and then green, red is switched off instead of switching both on... I use these MOSFETs, is that maybe the wrong type? I'm fairly new to this, sorry.

What i want to achieve: Switch more than one color on/off at the same time ("mixing" them) and have them stay on until I send another command. Thanks for your help.

The code:

from gpiozero import LED

def toggle_led(color, pinr=6, ping=26, pinb=16):
    #print(color)
    red = LED(pinr)
    green = LED(ping)
    blue = LED(pinb)
    
    if color == 'red':
        red.toggle()
    elif color == 'green':
        green.toggle()
    elif color == 'blue':
        blue.toggle()
    elif color == 'off':
        blue.off()
        red.off()
        green.off()

if __name__ == "__main__":
    toggle_led('off')

My system info:

➜  ~ uname -a
Linux raspberrypi 5.4.79-v7l+ #1373 SMP Mon Nov 23 13:27:40 GMT 2020 armv7l GNU/Linux
➜  ~ cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
8
  • The code published will do nothing. Don't expect people to follow 3 links to see what you have done. – Milliways Jan 10 at 22:06
  • What's the smallest amount of code you can write to reproduce the problem? You're indicating that it's just this simple bit of isolated code, but there's other parts of the system that you've left out. – ben_nuttall Jan 10 at 22:58
  • @Milliways I don't quite understand. The code does something just not what I expect it to do and I directly link to a wiring scheme of my setup...? What do you mean? – Jan Jan 11 at 11:57
  • @ben_nuttall This is all the code. Nothing else controls the LED, I am calling this function you see in my original post. If I run this script on its own, it switches the LED strip of and then the fading thing happens. – Jan Jan 11 at 12:00
  • Like I said, what's the smallest amount of code you can write to reproduce the problem? Surely pasting that code into a Python shell and calling the function doesn't fail. There is nothing wrong with your GPIO code - so it must be something else. – ben_nuttall Jan 11 at 20:30
0

Your LED might be turning off when the end of the script is reached, for example the following script will turn the LED on for 5 seconds, then end, and restore the pin back to its original state (LED off):

from gpiozero import LED
from time import sleep

led = LED(5)

led.on()
sleep(5)

In order to keep the LED on indefinitely, the script must be kept alive. One way to do this is using signal.pause():

from gpiozero import LED
from signal import pause

led = LED(5)

led.on()

pause()

See https://gpiozero.readthedocs.io/en/latest/faq.html#how-do-i-keep-my-script-running

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.