Python Code

The rgb_led.py example uses the PCA9685's channels 6, 5 and 4 by default. These channels can be modified by changing the RED_PIN, GREEN_PIN, and BLUE_PIN variables. You can use any three channels on the PCA (0 to 15).

Download: file
# PWM Pins
RED_PIN = 6
GREEN_PIN = 5
BLUE_PIN = 4

After importing our system libraries, Adafruit Blinka, the PCA9685, and the Adafruit IO REST client, we need to set the ADAFRUIT_IO_KEY and ADAFRUIT_IO_USERNAME variables to our account credentials. 

Download: file
# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'

# Set to your Adafruit IO username.
# go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'

In the setup of this code, we create an instance of the Adafruit IO Client, create a color feed, and create a PCA9865 class instance which is used to control the PWM outputs. 

Download: file
# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

try: # if we have a 'color' feed
    color = aio.feeds('color')
except RequestError: # create an `color` feed
    feed = Feed(name='color')
    color = aio.create_feed(feed)

# Create the I2C bus interface.
i2c_bus = I2C(SCL, SDA)

# Create a simple PCA9685 class instance.
pca = PCA9685(i2c_bus)
pca.frequency = 60

Whenever the color feed receives a new message, we'll first convert the feed (in hexadecimal) to separate red, green, and blue values compatible with the PCA using the toRed(), toGreen(), and toBlue() functions. 

Then, we use the map_range()method to map the values to 16-bit values for the PCA9685:

Download: file
red = map_range(int(red), 0, 255, 0, 65535)
green = map_range(int(green), 0, 255, 0, 65535)
blue = map_range(int(blue), 0, 255, 0, 65535)

Then, we're going to send set each pin's duty cycle. Because the RGB LED we are using is common anode, we'll need to flip the incoming RGB values by subtracting them from 65535.

Download: file
pca.channels[RED_PIN].duty_cycle = 65535 - int(red)
pca.channels[GREEN_PIN].duty_cycle = 65535 - int(green)
pca.channels[BLUE_PIN].duty_cycle = 65535 - int(blue)

Save the code to your Raspberry Pi. Run the code by entering the following into your terminal:

python3 rgb_led.py

You can now use the color block on Adafruit IO to set the color feed. You should see something resembling the following in the terminal when you change this feed:

Download: file
Received Color:
	 - R:  255
	 - G:  0
	 - B:  0
	 - HEX:  #ff0000
Received Color:
	 - R:  0
	 - G:  0
	 - B:  0
	 - HEX:  #000000

You should also see the RGB LED update with the color you picked in the color block.

Code

"""
`rgb_led.py`
=======================================================================
Control a RGB LED using
Adafruit IO and Python

Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-color

Adafruit invests time and resources providing this open source code.
Please support Adafruit and open source hardware by purchasing
products from Adafruit!

Author(s): Brent Rubell for Adafruit Industries
Copyright (c) 2018 Adafruit Industries
Licensed under the MIT license.
All text above must be included in any redistribution.

Dependencies:
    - Adafruit_Blinka
        (https://github.com/adafruit/Adafruit_Blinka)
    - Adafruit_CircuitPython_PCA9685
        (https://github.com/adafruit/Adafruit_CircuitPython_PCA9685)
"""
# import system libraries
import time

# import Adafruit Blinka
from board import SCL, SDA
from busio import I2C

# import the PCA9685 module.
from adafruit_pca9685 import PCA9685

# import Adafruit IO REST client
from Adafruit_IO import Client, Feed, RequestError

# PWM Pins
RED_PIN = 6
GREEN_PIN = 5
BLUE_PIN = 4

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'

# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

try: # if we have a 'color' feed
    color = aio.feeds('color')
except RequestError: # create an `color` feed
    feed = Feed(name='color')
    color = aio.create_feed(feed)

# Create the I2C bus interface.
i2c_bus = I2C(SCL, SDA)

# Create a simple PCA9685 class instance.
pca = PCA9685(i2c_bus)
pca.frequency = 60
prev_color = '#000000'

def map_range(x, in_min, in_max, out_min, out_max):
    """re-maps a number from one range to another."""
    mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min
    if out_min <= out_max:
        return max(min(mapped, out_max), out_min)
    return min(max(mapped, out_max), out_min)

while True:
    # grab the `color` feed
    color_val = aio.receive(color.key)
    if color_val != prev_color:
        # print rgb values and hex value
        print('Received Color: ')
        red = aio.toRed(color_val.value)
        print('\t - R: ', red)
        green = aio.toGreen(color_val.value)
        print('\t - G: ', green)
        blue = aio.toBlue(color_val.value)
        print('\t - B: ', blue)
        print('\t - HEX: ', color_val.value)
        # map color values (0-255) to  16-bit values for the pca
        red = map_range(int(red), 0, 255, 0, 65535)
        green = map_range(int(green), 0, 255, 0, 65535)
        blue = map_range(int(blue), 0, 255, 0, 65535)
        # invert RGB values for common anode LEDs.
        pca.channels[RED_PIN].duty_cycle = 65535 - int(red)
        pca.channels[GREEN_PIN].duty_cycle = 65535 - int(green)
        pca.channels[BLUE_PIN].duty_cycle = 65535 - int(blue)
    prev_color = color_val
    # let's wait a bit so we don't flood adafruit io's servers...
    time.sleep(1)
This guide was first published on Feb 20, 2017. It was last updated on Feb 20, 2017. This page (Python Code) was last updated on Aug 18, 2019.