WS2812B - Initialization and Setting Colors
This page contains an example of initializing the WS2812B LED strip and examples of setting colors for different effects.
Initialization
To use the WS2812B LEDs, include the required library.
Create the pixels object and initialize the LEDs in the setup() function using pixels.begin().
// Include needed libraries
#include "WS2812-SOLDERED.h"
// Which pin on the Dasduino is connected to the NeoPixels?
#define PIN 6 // If you are using Dasduino Lite, you must specify that pin as PA6
#define NUMPIXELS 10
WS2812 pixels(NUMPIXELS, PIN);
void setup()
{
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}
pixels.begin()
Initializes the WS2812 pixels, setting up communication and verifying its presence.
Returns value: None.
Setting the LED Colors One by One
To control the colors of the LEDs, use functions such as clear() and show(), along with the primary function setPixelColor(). The delay() function can also be used for timing adjustments.
#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
void loop()
{
pixels.clear();
for (int i = 0; i < NUMPIXELS; i++)
{ // For each pixel...
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright green color:
pixels.setPixelColor(i, pixels.Color(0, 150, 0));
pixels.show();
delay(DELAYVAL); // Pause before next pass through loop
}
}
pixels.clear()
Sets all pixel colors to 'off'.
Returns value: None
pixels.show()
Sends the updated pixel colors to the hardware.
Returns value: None
pixels.setPixelColor()
Sets the color of a specific LED in the NeoPixel strip by assigning red, green, and blue values.
Returns value: None
Function parameters:
| Type | Name | Description |
|---|---|---|
uint16_t | n | Index of the LED to set the color for. |
uint8_t | r | Red component of the color (0-255). |
uint8_t | g | Green component of the color (0-255). |
uint8_t | b | Blue component of the color (0-255). |
Creating a Rainbow Effect
To produce a smooth rainbow animation along the entire LED strip, the rainbow() function continuously updates pixel colors using the HSV color wheel. It cycles through different hues to create a flowing rainbow effect. The show() function updates the strip, while delay() controls the animation speed.
Make sure to call the rainbow() function with a delay parameter somewhere! Example:
void loop()
{
rainbow(10);
}
void rainbow(int wait)
{
for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256)
{
for (uint16_t i = 0; i < strip.numPixels(); i++)
{
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show();
delay(wait);
}
}
pixels.ColorHSV()
Generates a color based on the HSV color model, with a hue ranging from 0 to 65535.
Returns value: A 32-bit color value representing the requested hue.
Function parameters:
| Type | Name | Description |
|---|---|---|
uint16_t | hue | Hue value (0-65535). |
pixels.gamma32()
Applies gamma correction to improve the visual accuracy of colors.
Returns value: A 32-bit color value with gamma correction applied.
Function parameters:
| Type | Name | Description |
|---|---|---|
uint32_t | color | The original 32-bit color value. |
pixels.setPixelColor()
Sets the color of a specific LED in the NeoPixel strip using a 32-bit color value.
Returns value: None
Function parameters:
| Type | Name | Description |
|---|---|---|
uint16_t | n | Index of the LED to set the color for. |
uint32_t | color | 32-bit color value representing the desired color. |
Cylon / Knight Rider Effect
A single bright LED moves back and forth, similar to the iconic scanner from Knight Rider.
void cylonEffect()
{
int maxBrightness = 255;
// Move forward
for (int i = 0; i < NUMPIXELS; i++)
{
pixels.clear();
pixels.setPixelColor(i, pixels.Color(75, 0, 130));
pixels.setBrightness(maxBrightness);
pixels.show();
delay(50);
}
// Move backward
for (int i = NUMPIXELS - 1; i >= 0; i--)
{
pixels.clear();
pixels.setPixelColor(i, pixels.Color(75, 0, 130));
pixels.setBrightness(maxBrightness);
pixels.show();
delay(50);
}
}
void loop()
{
while (true)
{
cylonEffect();
}
}
Simple.ino
Example file for initializing and using the WS2812B LEDs.
Strand_Test.ino
Example file for using the WS2812B LEDs to create different color effects.