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()
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()
| improve this answer | |
  • unfortunatly that doenst help... – Sebastiaan Maes Mar 20 at 18:30
  • Loose connection? Have added a pigpio script. – joan Mar 20 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.

| improve this answer | |

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.