seanspotatobusiness
Posts: 185
Joined: Tue May 22, 2012 11:19 pm

RGB rainbow transition by PWM not working for me

Wed May 22, 2019 11:48 am

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
 
RUNNING = True
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
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)
RED.start(100)
GREEN = GPIO.PWM(green, Freq)
GREEN.start(100)
BLUE = GPIO.PWM(blue, Freq)
BLUE.start(100)

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
    else:
        b = 127 - position % 128
        r = position % 128
        g = 0

    return r, g, b

try:
    while(True):
        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)
            RED.ChangeDutyCycle(percenttestr)
            GREEN.ChangeDutyCycle(percenttestg)
            BLUE.ChangeDutyCycle(percenttestb)
            time.sleep(0.1)

            
except KeyboardInterrupt:
    GPIO.cleanup()
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.setmode(GPIO.BOARD)
    GPIO.setup(PINS, GPIO.OUT, initial=GPIO.LOW)
 
 
def color_test(channel, frequency, speed, step):
    p = GPIO.PWM(channel, frequency)
    p.start(50)
    while True:
        for dutyCycle in range(0, 101, step):
            p.ChangeDutyCycle(dutyCycle)
            time.sleep(speed)
        for dutyCycle in range(100, -1, -step):
            p.ChangeDutyCycle(dutyCycle)
            time.sleep(speed)
 
 
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
        t.start()
    for t in threads:
        t.join()
 
 
def main():
    try:
        initialize_gpio()
        print("\nPress ^C (control-C) to exit the program.\n")
        color_test_thread()
    except KeyboardInterrupt:
        pass
    finally:
        GPIO.cleanup()
 
 
if __name__ == '__main__':
    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?
Last edited by seanspotatobusiness on Wed May 22, 2019 11:58 am, edited 1 time in total.
Raspberry Pi 3 B and B+
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"

Brandon92
Posts: 773
Joined: Wed Jul 25, 2018 9:29 pm
Location: Netherlands

Re: RGB rainbow transition by PWM not working for me

Wed May 22, 2019 11:54 am

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

GPIO.setmode(GPIO.BCM)
Into this

Code: Select all

GPIO.setmode(GPIO.BOARD)

Brandon92
Posts: 773
Joined: Wed Jul 25, 2018 9:29 pm
Location: Netherlands

Re: RGB rainbow transition by PWM not working for me

Wed May 22, 2019 1:18 pm

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?

seanspotatobusiness
Posts: 185
Joined: Tue May 22, 2012 11:19 pm

Re: RGB rainbow transition by PWM not working for me

Thu May 23, 2019 10:19 am

Thanks very much! I thought colorsys provided the color wheel functionality? In any case, changing BCM to BOARD fixed the script.
Raspberry Pi 3 B and B+
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"

Brandon92
Posts: 773
Joined: Wed Jul 25, 2018 9:29 pm
Location: Netherlands

Re: RGB rainbow transition by PWM not working for me

Thu May 23, 2019 10:46 am

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.

Return to “Python”