Page 1 of 1

RGB rainbow transition by PWM not working for me

Posted: Wed May 22, 2019 11:48 am
by seanspotatobusiness
I'm trying to use the code kindly provided by another forum user here: viewtopic.php?t=126405

The code is for driving RGB LEDs through the rainbow via PWM.

The problem is that the code doesn't seem to work for me. I get the output printing to the terminal window but I don't get the expected colour transitions. I know that my pins are connected correctly because some other code (which does random transitions rather than deliberate rainbow transitions) does work.

Here's the code that I'm having a problem with:

Code: Select all

import random, time
import RPi.GPIO as GPIO
import colorsys
red = 8
green = 10
blue = 12
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)

Freq = 100
RED = GPIO.PWM(red, Freq)
GREEN = GPIO.PWM(green, Freq)
BLUE = GPIO.PWM(blue, Freq)

outval = 128

def wheel_color(position):
    """Get color from wheel value (0 - 384)."""
    if position < 0:
        position = 0
    if position > 384:
        position = 384

    if position < 128:
        r = 127 - position % 128
        g = position % 128
        b = 0
    elif position < 256:
        g = 127 - position % 128
        b = position % 128
        r = 0
        b = 127 - position % 128
        r = position % 128
        g = 0

    return r, g, b

        for pos in range(0,385):
            r, g, b = wheel_color(pos)
            print (r, g, b)
            percenttestr = (r/128.0)*100.0
            percenttestg = (g/128.0)*100.0
            percenttestb = (b/128.0)*100.0
            print (percenttestr)
            print (percenttestg)
            print (percenttestb)

except KeyboardInterrupt:
and here's the code that is working okay for me:

Code: Select all

import RPi.GPIO as GPIO
import threading
import time
import random
R = 8
G = 10
B = 12
PINS = [R,G,B]

def initialize_gpio():
    GPIO.setup(PINS, GPIO.OUT, initial=GPIO.LOW)
def color_test(channel, frequency, speed, step):
    p = GPIO.PWM(channel, frequency)
    while True:
        for dutyCycle in range(0, 101, step):
        for dutyCycle in range(100, -1, -step):
def color_test_thread():
    threads = []
    threads.append(threading.Thread(target=color_test, args=(R, 200, 0.02, 1)))
    threads.append(threading.Thread(target=color_test, args=(G, 200, 0.035, 1)))
    threads.append(threading.Thread(target=color_test, args=(B, 200, 0.045, 1)))
    for t in threads:
        t.daemon = True
    for t in threads:
def main():
        print("\nPress ^C (control-C) to exit the program.\n")
    except KeyboardInterrupt:
if __name__ == '__main__':

Can anyone advise what I should do to find the cause of the problem?

Edit: could it be because I don't have colorsys? If that was the case, the code wouldn't run at all, right? When I type "pip install colorsys", I get the error:

Code: Select all

[email protected]:~ $ pip install colorsys
Collecting colorsys
  Could not find a version that satisfies the requirement colorsys (from versions: )
No matching distribution found for colorsys
so I don't actually know whether I have it installed but I would expect an error when I try to run the script if it was unable to import a required library, right?

Re: RGB rainbow transition by PWM not working for me

Posted: Wed May 22, 2019 11:54 am
by Brandon92
The problem is that you not using the same pins on the header in your code.
So change this is you not working code

Code: Select all

Into this

Code: Select all


Re: RGB rainbow transition by PWM not working for me

Posted: Wed May 22, 2019 1:18 pm
by Brandon92
Response to your edit:
I think you don't need to include the colorsys at all. I can not see where you are using that module. So, if you try the code without that module, does it work then?

Re: RGB rainbow transition by PWM not working for me

Posted: Thu May 23, 2019 10:19 am
by seanspotatobusiness
Thanks very much! I thought colorsys provided the color wheel functionality? In any case, changing BCM to BOARD fixed the script.

Re: RGB rainbow transition by PWM not working for me

Posted: Thu May 23, 2019 10:46 am
by Brandon92
seanspotatobusiness wrote:
Thu May 23, 2019 10:19 am
I thought colorsys provided the color wheel functionality?
Well, I don't that execly, because I did't use that function before. However, I saw that you only imported that functie but not using it in your code.