Sunday, March 12, 2023

Teensy 4.1 with WS2812 LED Strip using FastLED

 Overview

The FastLED library allows for the Teensy 4.1 to easily control WS2812 and related LED strips of arbitrary sizes. The FastLED library is included with the Teensyduino add-on installation. 


Hardware







Each WS2811 is made up of three RGB LEDs with 8-bit control, thus giving 24-bit colour control per LED. The entire LED strip, regardless of how many LEDs, can be controlled via a signal data pin from the Teensy. 

Connect Teensy ground to LED strip ground (black jumper cable). Connect Teensy 5V to LED strip 5V (orange jumper cable). Connect Teensy pin 0 to data input of the LED strip (white jumper cable). 

In the above photo, the LED strip connector has white for ground, green for data and red for 5V. 

In this case, as there are only 30 LEDs in this light strip, the USB power from the Teensy can be used to power the LEDs. If more LEDs are used, an external power supply is required - please see here for more details. A electrolytic capacitor would also be required if an external power supply is used. 



Software

Example 1 - Rotating hue over time

#include <FastLED.h>

#define DATA_PIN 0
#define NUM_LEDS 30

CRGB leds[NUM_LEDS];
elapsedMillis tic;
uint8_t hue = 0;

void setup() {
Serial.begin(115200);
Serial.println("FastLed Test");

FastLED.addLeds<WS2812, DATA_PIN, GBR>(leds, NUM_LEDS);
FastLED.setBrightness(255);
}

void loop() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].setHue(((i * 8) + hue) & 255);
}
FastLED.show();
hue++;
delay(10);
}


Example 2 - Using MIDI continuous controller 1 channel 1 to change the hue

#include <FastLED.h>
#define DATA_PIN 0
#define NUM_LEDS 30

CRGB leds[NUM_LEDS];
uint8_t hue = 0;

void setup() {
Serial.begin(115200);
Serial.println("FastLed Test");

FastLED.addLeds<WS2812, DATA_PIN, GBR>(leds, NUM_LEDS);
FastLED.setBrightness(255);

for (int i = 0; i < NUM_LEDS; i++) {
leds[i].setHue(((i * 8) + hue) & 255);
}
FastLED.show();

usbMIDI.setHandleControlChange(myCC);
}

void loop() {
usbMIDI.read();
}

void myCC(byte channel, byte control, byte value) {
if (channel == 1 && control == 1) {
hue = value * 2;

for (int i = 0; i < NUM_LEDS; i++) {
leds[i].setHue(((i * 8) + hue) & 255);
}
FastLED.show();
}
}

No comments:

Post a Comment