Neopixel

We can set the brightness of a single color of LED using the analogWrite() function. How can we control a full color RGB LED, or a lot of LEDs, without using all the pins of the Teensy? A Neopixel is a brand of WS2812B RGB LEDs that can be controlled with a single pin, and chained so that a whole strip can be controlled individually for brightness and color, still using just one pin. Neopixels can be purchased individually, on long strands, and in all kinds of arrays of shapes.

Wire the Neopixel

The Neopixel board in the kit takes 5V, ground, and any pin for DIN (I used pin 34). The other end of the board has pads to solder connections to the next set of Neopixels, connecting DOUT to the next DIN. I have pre-soldered header pins on so you can plug the board into your breadboard.

Get the library

The Arduino IDE does not come with the code that the Teensy needs to control the Neopixels, but the Teensyduino add-on does. You can find the example code in File->Examples->OctoWS2811. Try Teensy4_PinList first.

Run the example code

The Teensy 4.1 can drive many, many Neopixels, but we have only 8, so we need to change the example code to match. Change numPins to 1, edit the array pinList to just the number of the pin you are using for DIN, and change ledsPerStrip to 8. Run the code and the LEDs should flash through a variety of bright colors.

The Neopixels can be harshly bright, so I like to put something over them to diffuse the color, like a sheet of white paper, tissue, or cotton.

Image of diffused neopixels Neopixels with a tissue diffuser

The code boils down to the ability to set the color of each LED individually using leds.setPixel(), and then telling all of the LEDs to update at the same time using leds.show().

Take a look at this sample code to see how to flash all the LEDs red, then green, then blue:

Solution

Set the brightness and color of each LED

You can also specify the raw RGB value of each LED. In this example, the value of the pot determines how bright white the entire strip of LEDs are:

Solution