1

I found a python script to control an RGB LED with GPIO output pins from a raspberry. I connected a common cathode LED to the GPIO Pins and added a 300 ohm resistor to the ground pin. When I run the script it works quite well. I can choose the RGB values with an input in the terminal python led.py 0 100 110 for example. As long as I choose 255 0 0 - 0 255 0 - 0 0 255 it works perfectly fine. The strange thing is that when I choose for a mix of values python led.py 0 20 20 and run the code the LED starts shining. However it looks like there's a delay between de colors (red, green, blue) that are displayed, so I can sort of see how the colours are mixed. I found this very weird because I don't really use delays in my script. Someone an idea?

UPDATE: when I remove the resistor and connect the ground directly I dont get the flicker, problem is: my pcb is set in this configuration. as soon as I add a resistor for example 15 ohm I get the flicker back

I posted a video so you can see it https://www.youtube.com/watch?v=N6J5zy38j38, no it's not my camera ;)

#! /usr/bin/env python
import RPi.GPIO as GPIO
import sys
import time

# RGB LED pinnen configureren.
pinRood = 17
pinGroen = 27
pinBlauw = 22

# GPIO setup.
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Zet de GPIO pinnen als uitgang.
GPIO.setup(pinRood, GPIO.OUT)
GPIO.setup(pinGroen, GPIO.OUT)
GPIO.setup(pinBlauw, GPIO.OUT)

# Gebruik PWM op de pinnen.
ROOD = GPIO.PWM(pinRood, 1000)
GROEN = GPIO.PWM(pinGroen, 1000)
BLAUW = GPIO.PWM(pinBlauw, 1000)
ROOD.start(0)
GROEN.start(0)
BLAUW.start(0)

if len(sys.argv) > 3:
    while True:
      # converteer de waarde 255 tot max 100 voor PWM.
      roodwaarde = (int(sys.argv[1]) * 100) / 255
      groenwaarde = (int(sys.argv[2]) * 100) / 255
      blauwwaarde = (int(sys.argv[3]) * 100) / 255

      ROOD.ChangeDutyCycle(roodwaarde)
      GROEN.ChangeDutyCycle(groenwaarde)
      BLAUW.ChangeDutyCycle(blauwwaarde)


GPIO.cleanup()

3 Answers 3

1

Don't continually call the dutycycle function. You are probably resetting its function many times per second.

Try something like

...
import time
...
if len(sys.argv) > 3:
   # converteer de waarde 255 tot max 100 voor PWM.
   roodwaarde = (int(sys.argv[1]) * 100) / 255
   groenwaarde = (int(sys.argv[2]) * 100) / 255
   blauwwaarde = (int(sys.argv[3]) * 100) / 255

   ROOD.ChangeDutyCycle(roodwaarde)
   GROEN.ChangeDutyCycle(groenwaarde)
   BLAUW.ChangeDutyCycle(blauwwaarde)

   time.sleep(1000)

For a pigpio equivalent try the following. Needs the pigpio daemon to be running (sudo pigpiod).

#! /usr/bin/env python

import sys
import time
import pigpio

# RGB LED pinnen configureren.
pinRood = 17
pinGroen = 27
pinBlauw = 22

pi = pigpio.pi()
if not pi.connected:
   exit()

if len(sys.argv) > 3:
   # max 255 voor PWM.
   roodwaarde = int(sys.argv[1])
   groenwaarde = int(sys.argv[2])
   blauwwaarde = int(sys.argv[3])

   pi.set_PWM_dutycycle(pinRood, roodwaarde)
   pi.set_PWM_dutycycle(pinGroen, groenwaarde)
   pi.set_PWM_dutycycle(pinBlauw, blauwwaarde)

   time.sleep(60)

   pi.set_PWM_dutycycle(pinRood, 0)
   pi.set_PWM_dutycycle(pinGroen, 0)
   pi.set_PWM_dutycycle(pinBlauw, 0)

pi.stop()
2
  • unfortunatly that doenst help... Mar 20, 2020 at 18:30
  • Loose connection? Have added a pigpio script.
    – joan
    Mar 20, 2020 at 18:55
0

Without connection details this is unanswerable.

If you are trying to use a single resistor in the common leg it is no wonder it is unreliable as each LED has a different voltage and only the lowest (usually Red) will work.

NOTE coloured LEDs often need >3V so won't work reliably on the Pi - you may need transistors.

PS running without a current limiting resistor is likely to damage the Pi.

0

I had the same issue with flickering and this code solved the issue. Apparently if the code works as intented "enough", even there is no errors, there can be small delaying stuff that piles up and human eye can see it as flickering from rgb lights. Confirming this flickering is code related. I'm also using my RGB with transistors.

from gpiozero import RGBLED, Device
from colorzero import Color
from gpiozero.pins.rpigpio import RPiGPIOFactory
from time import sleep
from signal import pause
import sys

 
# Use the RPi.GPIO pin factory
Device.pin_factory = RPiGPIOFactory()
# Define RGB LED pins
rgb_pins = {'red': 23, 'green': 22, 'blue': 24}
rgb_led = RGBLED(red=rgb_pins['red'], green=rgb_pins['green'], blue=rgb_pins['blue'])

def transition_color(start_color, end_color, duration=1, steps=100):
    for step in range(steps + 1):
        color = Color(
            start_color.red + (end_color.red - start_color.red) * (step / steps),
            start_color.green + (end_color.green - start_color.green) * (step / steps),
            start_color.blue + (end_color.blue - start_color.blue) * (step / steps)
        )
        rgb_led.color = color
        sleep(duration / steps)

def slow_transition():
    transition_color(Color('blue'), Color('red'))
    transition_color(Color('red'), Color('green'))
    transition_color(Color('green'), Color('blue'))

def reset_gpio():
    rgb_led.close()

if __name__ == "__main__":
    try:
        while True:
            slow_transition()  # Slowly transition between colors
            sleep(1)  # Wait for a second between transitions

    except KeyboardInterrupt:
        # Handle Ctrl+C gracefully
        print("\nKeyboardInterrupt: Resetting GPIO...")
        reset_gpio()

    finally:
        # Ensure GPIO is properly cleaned up on normal exit
        reset_gpio()

        # Use the pause function to wait for signals
        print("Waiting for signals...")
        pause()

    # Check if the script is called with a specific command
    if len(sys.argv) > 1 and sys.argv[1] == "reset_gpio":
        reset_gpio()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.