Giuseppe Parrello

 

Embedded RGB LED


Introduction

With this project we will manage a common cathode RGB LED, using the FTDI FT232H development board (of which there is a dedicated page on this site) and the NXP PCA9685 development board (of which there is a dedicated page on this site).

 

Connection

This RGB LED must be connected to the NXP PCA9685 development board. The connection connectors are listed below:

Image Board FT232H Board PCA9685 RGB LED

FT232H - PCA9685 - RGB LED

AD0 SCL ------
AD1 + AD2 SDA ------
+3.3V VCC ------
GND GND ------
------ Channel 0 - PWM Red
------ Channel 0 - GND Cathode
------ Channel 1 - PWM Green
------ Channel 2 - PWM Blue

 

Python code

To manage this RGB LED, the presence of the "PCA9685_FTDI" library is required.
The first example of Python code acts on the individual Red-Green-Blue colors. The Python code is as follows:

import time
import pca9685_ftdi

# Create library object using our FTDI I2C port
pwm = pca9685_ftdi.PCA9685_FTDI()

STEP      = 4
FACTOR    = 4
SLEEP     = 0.05
CHANNEL_R = 0
CHANNEL_G = 1
CHANNEL_B = 2

try:
    while True:
 
        print("\nOnly Red")
        pwm.setPWM(CHANNEL_R, 0, 256 * FACTOR)
        pwm.setPWM(CHANNEL_G, 0, 0)
        pwm.setPWM(CHANNEL_B, 0, 0)
        time.sleep(2)

        print("Only Green")
        pwm.setPWM(CHANNEL_R, 0, 0)
        pwm.setPWM(CHANNEL_G, 0, 256 * FACTOR)
        pwm.setPWM(CHANNEL_B, 0, 0)
        time.sleep(2)

        print("Only Blue")
        pwm.setPWM(CHANNEL_R, 0, 0)
        pwm.setPWM(CHANNEL_G, 0, 0)
        pwm.setPWM(CHANNEL_B, 0, 256 * FACTOR)
        time.sleep(2)

        print("\nAll colors on")
        pwm.setPWM(CHANNEL_R, 0, 256 * FACTOR)
        pwm.setPWM(CHANNEL_G, 0, 256 * FACTOR)
        pwm.setPWM(CHANNEL_B, 0, 256 * FACTOR)
        time.sleep(2)

        print("All colors off")
        pwm.setPWM(CHANNEL_R, 0, 0)
        pwm.setPWM(CHANNEL_G, 0, 0)
        pwm.setPWM(CHANNEL_B, 0, 0)
        time.sleep(2)

        print("\nRED Color Flow")
        for fadeValue in range(0, 256, STEP):
            pwm.setPin(CHANNEL_R, fadeValue * FACTOR)
            time.sleep(SLEEP)

        for fadeValue in range(255, -1, -STEP):
            pwm.setPin(CHANNEL_R, fadeValue * FACTOR)
            time.sleep(SLEEP)

        pwm.setPin(CHANNEL_R, 0)

        print("GREEN Color Flow")
        for fadeValue in range(0, 256, STEP):
            pwm.setPin(CHANNEL_G, fadeValue * FACTOR)
            time.sleep(SLEEP)

        for fadeValue in range(255, -1, -STEP):
            pwm.setPin(CHANNEL_G, fadeValue * FACTOR)
            time.sleep(SLEEP)

        pwm.setPin(CHANNEL_G, 0)

        print("BLUE Color Flow")
        for fadeValue in range(0, 256, STEP):
            pwm.setPin(CHANNEL_B, fadeValue * FACTOR)
            time.sleep(SLEEP)

        for fadeValue in range(255, -1, -STEP):
            pwm.setPin(CHANNEL_B, fadeValue * FACTOR)
            time.sleep(SLEEP)

        pwm.setPin(CHANNEL_B, 0)

except KeyboardInterrupt:
    # Capture keyboard ^C to exit the program
    print('\nYou terminated the program. The program ends!')
    #pwm.clearAllPins()
    pwm.set_all_pwm(0, 0)
    pwm.close()

The second Python code example creates a developed flow of colors using the individual Red-Green-Blue colors. The Python code is as follows:

import time
import pca9685_ftdi

# Create library object using our FTDI I2C port
pwm = pca9685_ftdi.PCA9685_FTDI()

STEP      = 1
FACTOR    = 8
SLEEP     = None
CHANNEL_R = 0
CHANNEL_G = 1
CHANNEL_B = 2

def load(Red,Green,Blue):
    if (Red):
        pwm.setPWM(CHANNEL_R, 0, int(R * FACTOR))
    if (Green):
        pwm.setPWM(CHANNEL_G, 0, int(G * FACTOR))
    if (Blue):
        pwm.setPWM(CHANNEL_B, 0, int(B * FACTOR))
    if (SLEEP):
        time.sleep(SLEEP)
        
try:
    while True:
        R = 255
        G = 0
        B = 0
        load(R, G, B)
        for G in range(1, 256, STEP):
            load(Red=None,Green=G,Blue=None)
        for R in range(254, -1, -STEP):
            load(Red=R, Green=None, Blue=None)
        for B in range(1, 256, STEP):
            load(Red=None, Green=None, Blue=B)
        for G in range(254, -1, -STEP):
            load(Red=None,Green=G,Blue=None)
        for R in range(1, 256, STEP):
            load(Red=R, Green=None, Blue=None)
        for B in range(254, -1, -STEP):
            load(Red=None, Green=None, Blue=B)
 
except KeyboardInterrupt:
    # Capture keyboard ^C to exit the program
    print('\nYou terminated the program. The program ends!')
    pwm.set_all_pwm(0, 0)
    pwm.close()