I am using hw-479 that looks like the picture below. I cannot find much of information for it but the Alibaba page says

The RGB LED module is made of a plug-in full-color LED. The PWM voltage input of the three pins of R G and B can adjust the intensity of three primary colors (red/blue/green) to achieve full-color color mixing. The Arduino's control of the module enables cool lighting effects.

Of course, since I am using a Pi, I cannot use that Arduino library. I wrote the following code, but it does not work. Turning individual colours by setting GPIO.output(pin, HIGH) was successful, so I do not think the pin connections are wrong.

I wonder what is wrong with the code. First, I do not understand what the frequency should be. The example was using 50, so I used 50. Secondly, it seems that the dc, the parameter of start() ranges from 0, to 100, and 0 means the maximum power and 100 means the minimum power. Is that correct?

import RPi.GPIO as GPIO
import time

redPin = 11
greenPin = 13
bluePin = 15

def setValue(pin, value):
    GPIO.setup(pin, GPIO.OUT)
    p = GPIO.PWM(pin, 50)
    if 0>value or value>255:
        raise Exception("0 to 255")
    #dc = 0 max, dc = 100 min
    dc = 100 - (float(value)/256.*100);
def setRGB(r,g,b):
    setValue(redPin, r)
    setValue(greenPin, g)
    setValue(bluePin, b)
if __name__ == "__main__":
    setRGB(0xFA, 0x80, 0x72)
    print("Dark slate gray")
    setRGB(0x2F, 0x4F, 0x4F)

enter image description here


RPi.GPIO dutycycles are in the range 0 to 100 and represent a percentage of total power. So a dutycycle of 0 is off, 100 is fully on, and 50 is half power.

There is quite a lot of changes I would make to the code. Basically I would have an initialisation section where all the stuff which should only be done once would be placed.


    GPIO.setup(redPin, GPIO.OUT)
    GPIO.setup(greenPin, GPIO.OUT)
    GPIO.setup(bluePin, GPIO.OUT)
    pR = GPIO.PWM(redPin, 50)
    pG = GPIO.PWM(greenPin, 50)
    pB = GPIO.PWM(bluePin, 50)

I would use pR.ChangeDutyCycle(), pG.ChangeDutyCycle(), and pB.ChangeDutyCycle() to update the dutcycles.

See https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/

  • It seems that if I create p like p=GPIO.PWM() inside of a function, the LED gets turned off once the control flow goes out of the function. I put the p in a global dictionary (if it exists, calls stop()) and then it did not turn off. Is it the best way to keep the returned object of the PWM() function in a global variable? Also, why 50 for the frequency? I had already seen the linked documentation, but it really did not give much of the details... – Damn Vegetables Jul 17 '20 at 17:03
  • When you create the object (p) within a function it is deleted when it goes out of scope (i.e. as soon as the function returns). 50 Hz is a reasonable frequency but something like 500 would be better. The problem is that requires more CPU as RPi.GPIO uses software timing for the PWM. – joan Jul 17 '20 at 17:12

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.