Giuseppe Parrello

 

Embedded LED RGB


Introduzione

Con questo progetto andremo a gestire un LED RGB a catodo comune, usando la scheda di sviluppo FTDI FT232H (di cui esiste una pagina dedicata su questo sito) e la scheda di sviluppo NXP PCA9685 (di cui esiste una pagina dedicata su questo sito).

 

Collegamento

Il LED RGB deve essere collegato alla scheda di sviluppo NXP PCA9685. I connettori di collegamento sono elencati qui di seguito:

Immagine Scheda FT232H Scheda PCA9685 LED RGB

FT232H - PCA9685 - RGB LED

AD0 SCL ------
AD1 + AD2 SDA ------
+3.3V VCC ------
GND GND ------
------ Canale 0 - PWM Rosso
------ Canale 0 - GND Catodo
------ Canale 1 - PWM Verde
------ Canale 2 - PWM Blu

 

Codice Python

Per gestire il LED RGB, è richiesta la presenza della libreria "PCA9685_FTDI".
Il primo esempio di codice Python agisce sui singoli colori Rosso-Verde-Blu. Il codice Python è il seguente:

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()

Il secondo esempio di codice Python crea un flusso elaborato di colori usando i singoli colori Rosso-Verde-Blu. Il codice Python è il seguente:

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()