CircuitPython Code

You can use analog RGB LED strips with CircuitPython's built-in analog/PWM output modules.  Just like with dimming a LED's brightness using CircuitPython you can use PWM outputs to control the brightness of the strip's red, green, and blue LEDs.  This allows you to set the color of the strip to anything you can imagine!

Just like with wiring to an Arduino you need to use transistors to buffer and control the higher current sent to the LED strips (your board can't supply all that current itself!).  Be sure to follow the usage page to wire the LED strip to your board with power transistors as shown.  This guide assumes the same wiring with board pin 5 connected to the strip's red LEDs, board pin 6 connected to green, and board pin 3 connected to blue.  As mentioned on the usage page too be sure to use a powerful 12V external power supply with enough current to drive all the LEDs.

Here's an example of a Metro M0 Express wired to MOSFETs that drive a strip of LEDs:

Note: Be sure to use PWM output pins from your board!  Not all digital inputs/outputs support PWM output.  Check your board's guide and pinout to confirm.  Typically a PWM-capable output will have a dot or wavy signal line next to it on the board's silkscreen.

Also be very careful to ensure your board can be powered by 12 volts!  If you're driving your board from the same 12 volt supply as your LED strip you need to make sure you connect the 12 volt power to the appropriate pin on your board so it is properly regulated down to the necessary logic levels for your board.  In particular be careful about using a Feather and 12 volt power, there's no Vin line like on Metro/Arduino boards so it's trickier to power them with higher voltage levels like 12 volts and you will damage the boardsDon't power a Feather, Trinket or Gemma from 12V or it can damage the board, use a Metro board instead!

Now connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Import the necessary pulseio and board modules by running:

import board
import pulseio

Then create a PWM output for each LED connection (red, green, blue):

red = pulseio.PWMOut(board.D5)
green = pulseio.PWMOut(board.D6)
blue = pulseio.PWMOut(board.D3)

Like the analog I/O guide mentions you can change the duty cycle of each PWM output to control the brightness of the LEDs.  A larger duty cycle means the LEDs are turned on for longer and appear brighter.  Remember the duty cycle is a value that goes from 0 (0% / not turned on) to 65535 (100% / turned on all the time).

To set the strip to a purple color with 100% red, 0% green, and 50% blue you could run:

red.duty_cycle = 65535   # 100% red
green.duty_cycle = 0     # 0% green
blue.duty_cycle = 32767  # 50% blue

Like the analog I/O guide mentions though it might be easier to make a function that converts a percent duty cycle value to the numeric value and use that to simplify your code.  Here's how to define and use the function to set the same purple color:

def duty_cycle(percent):
    return int(percent / 100.0 * 65535.0)

red.duty_cycle = duty_cycle(100)  # 100% red
green.duty_cycle = duty_cycle(0)  # 0% green
blue.duty_cycle = duty_cycle(50)  # 50% blue

Here's a complete example of using CircuitPython to control the LED strip and fade it between different colors just like with the similar Arduino code from the previous page.  Save this as a main.py on your board's root filesystem and it will fade the colors of the strip when the board starts:

import board
import pulseio
import time

RED_PIN = board.D5    # Red LED pin
GREEN_PIN = board.D6  # Green LED pin
BLUE_PIN = board.D3   # Blue LED pin

FADE_SLEEP = 10  # Number of milliseconds to delay between changes.
                 # Increase to slow down, decrease to speed up.

# Define PWM outputs:
red = pulseio.PWMOut(RED_PIN)
green = pulseio.PWMOut(GREEN_PIN)
blue = pulseio.PWMOut(BLUE_PIN)

# Function to simplify setting duty cycle to percent value.
def duty_cycle(percent):
    return int(percent / 100.0 * 65535.0)

# Fade from nothing up to full red.
for i in range(100):
    red.duty_cycle = duty_cycle(i)
    time.sleep(FADE_SLEEP)

# Now fade from violet (red + blue) down to red.
for i in range(100, -1, -1):
    blue.duty_cycle = duty_cycle(i)
    time.sleep(FADE_SLEEP)

# Fade from red to yellow (red + green).
for i in range(100):
    green.duty_cycle = duty_cycle(i)
    time.sleep(FADE_SLEEP)

# Fade from yellow to green.
for i in range(100, -1, -1):
    red.duty_cycle = duty_cycle(i)
    time.sleep(FADE_SLEEP)

# Fade from green to teal (blue + green).
for i in range(100):
    blue.duty_cycle = duty_cycle(i)
    time.sleep(FADE_SLEEP)

# Fade from teal to blue.
for i in range(100, -1, -1):
    green.duty_cycle = duty_cycle(i)
    time.sleep(FADE_SLEEP)
Last updated on 2017-11-03 at 05.30.09 AM Published on 2012-11-26 at 04.21.02 PM