Introduction
WS2812B Individually Addressable LEDs are a popular choice among hobbyists and professionals alike, due to their ability to create stunning visual effects. These LEDs can be controlled individually, allowing for a wide range of lighting options. Arduino, the open-source microcontroller platform, is also a popular choice for controlling WS2812B LEDs.
In this article, we will guide you through the process of interfacing WS2812B LEDs with Arduino and creating your own unique lighting effects.
What are WS2812B Individually Addressable LEDs?
WS2812B Individually Addressable LEDs are a type of RGB LED (Red, Green, Blue) that can be controlled individually with a single data line. Each LED contains a tiny microcontroller, which allows it to be addressed and controlled separately from the others in the same chain. This means that each LED can display a different color or brightness, enabling a wide range of lighting effects to be created. WS2812B LEDs are popular among hobbyists, artists, and designers for their versatility and ease of use. They can be used in a variety of applications, such as lighting up costumes, creating mood lighting, or even for use in large-scale installations.
Hardware Components
You will require the following hardware for Interfacing WS2812B Individually Addressable LEDs using Arduino.
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
USB Cable Type A to B | – | 1 |
DC Power for Arduino | – | 1 |
Individually Addressable LED | WS2812B | 1 |
Resistor | 220Ω | 1 |
Breadboard | – | 1 |
Jumper Wires | – | 1 |
WS2812B Individually Addressable LEDs Pinout
Sr. No. | Pin Name | Pin Description |
---|---|---|
1 | VDD | Power Supply for LED |
2 | DOUT | Control Data Signal Output |
3 | VSS | Ground |
4 | DIN | Control Data Signal Input |
WS2812B Individually Addressable LEDs Circuit
Make connections according to the circuit diagram given below.
Wiring / Connections
Arduino | WS2812B LED Strip |
---|---|
5V | VCC |
GND | GND |
D6 | DIN |
Installing Arduino IDE
First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.
Installing Libraries
Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.
Code
Now copy the following code and upload it to Arduino IDE Software.
#include <FastLED.h>
#define RGB_PIN 6 // LED DATA PIN
#define RGB_LED_NUM 10 // 10 LEDs [0...9]
#define BRIGHTNESS 200 // brightness range [0..255]
#define CHIP_SET WS2812B // types of RGB LEDs
#define COLOR_CODE GRB //sequence of colors in data stream
// Define the array of LEDs
CRGB LEDs[RGB_LED_NUM];
// define 3 byte for the random color
byte a, b, c;
#define UPDATES_PER_SECOND 100
char iByte = 0;
void setup() {
Serial.begin(9600);
Serial.println("WS2812B LEDs strip Initialize");
Serial.println("Please enter the 1 to 6 value.....Otherwise no any effect show");
FastLED.addLeds<CHIP_SET, RGB_PIN, COLOR_CODE>(LEDs, RGB_LED_NUM);
randomSeed(analogRead(0));
FastLED.setBrightness(BRIGHTNESS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.clear();
FastLED.show();
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
iByte = Serial.read();
switch (iByte) {
case '1': Toggle_RED_LED();
Serial.println("1.Toggle LED Complete");
break;
case '2': Scrolling_RED_LED();
Serial.println("2.Scrolling RED LED Complete");
break;
case '3': O_W_G_scroll();
Serial.println("3.Flag Show Complete");
break;
case '4': Rotate_color();
Serial.println("4.Rotate color Complete");
break;
case '5': r_g_b();
Serial.println("5.R_G_B color Complete");
break;
case '6': random_color();
Serial.println("6.Random color Show Complete");
break;
}
}
}
// RED LED TOGGLE
void Toggle_RED_LED(void) {
// Red Green Blue
for (int i = 0; i < RGB_LED_NUM; i++)
LEDs[i] = CRGB(255, 0, 0 );
FastLED.show();
delay(1000);
for (int i = 0; i < RGB_LED_NUM; i++)
LEDs[i] = CRGB(0, 0, 0 );
FastLED.show();
delay(1000);
}
// Move the Red LED
void Scrolling_RED_LED(void)
{
for (int i = 0; i < RGB_LED_NUM; i++) {
LEDs[i] = CRGB::Red;
FastLED.show();
delay(500);
LEDs[i] = CRGB::Black;
FastLED.show();
delay(500);
}
}
// Orange/White/Green color green
void O_W_G_scroll() {
for (int i = 0; i < RGB_LED_NUM; i++) {
LEDs[i] = CRGB::Orange;
delay(50);
FastLED.show();
}
for (int i = 0; i < RGB_LED_NUM; i++) {
LEDs[i] = CRGB::Black;
delay(50);
FastLED.show();
}
for (int i = 0; i < RGB_LED_NUM; i++) {
LEDs[i] = CRGB::White;
delay(50);
FastLED.show();
}
for (int i = 0; i < RGB_LED_NUM; i++) {
LEDs[i] = CRGB::Black;
delay(50);
FastLED.show();
}
for (int i = 0; i < RGB_LED_NUM; i++) {
LEDs[i] = CRGB::Green;
delay(50);
FastLED.show();
}
for (int i = 0; i < RGB_LED_NUM; i++) {
LEDs[i] = CRGB::Black;
delay(50);
FastLED.show();
}
}
// Red/Green/Blue color Rotate
void Rotate_color(void) {
for (int clr = 0; clr < RGB_LED_NUM; clr++) {
LEDs[clr] = CRGB::Red;
LEDs[clr + 1] = CRGB::Green;
LEDs[clr + 2] = CRGB::Blue;
FastLED.show();
delay(100);
for (int clr = 0; clr < RGB_LED_NUM; clr++) {
LEDs[clr] = CRGB::Black;
delay(5);
}
}
}
// Blue, Green , Red
void r_g_b() {
for (int i = 0; i < RGB_LED_NUM; i++) {
LEDs[i] = CRGB ( 0, 0, 255);
FastLED.show();
delay(50);
}
for (int i = RGB_LED_NUM; i >= 0; i--) {
LEDs[i] = CRGB ( 0, 255, 0);
FastLED.show();
delay(50);
}
for (int i = 0; i < RGB_LED_NUM; i++) {
LEDs[i] = CRGB ( 255, 0, 0);
FastLED.show();
delay(50);
}
for (int i = RGB_LED_NUM; i >= 0; i--) {
LEDs[i] = CRGB ( 0, 0, 0);
FastLED.show();
delay(50);
}
}
// random color show
void random_color(void) {
// loop over the NUM_LEDS
for (int i = 0; i < RGB_LED_NUM; i++) {
// choose random value for the r/g/b
a = random(0, 255);
b = random(0, 255);
c = random(0, 255);
// Set the value to the led
LEDs[i] = CRGB (a, b, c);
// set the colors set into the physical LED
FastLED.show();
// delay 50 millis
FastLED.delay(200);
}
}
Code Explanation
This is an Arduino code that controls WS2812B individually addressable LEDs using the FastLED library. The code defines the data pin, the number of LEDs, the brightness, the type of LED chip, and the color sequence. It also initializes the LED strip and sets up the serial communication.
The loop function waits for a character to be received over the serial port and performs a different action depending on the character received. Each action corresponds to a different LED animation. The available actions are:
- Toggle_RED_LED: toggles the LED strip between red and off every second.
- Scrolling_RED_LED: moves a red LED along the strip, one at a time, with a delay of 500ms between each movement.
- O_W_G_scroll: cycles through the colors orange, white and green, displaying each color for 50ms and turning the LEDs off for another 50ms.
- Rotate_color: rotates the colors red, green and blue along the LED strip, displaying each color for 100ms.
- r_g_b: cycles through the colors blue, green and red, displaying each color for 50ms and turning the LEDs off for another 50ms.
- random_color: displays a random color on the LED strip.
Overall, this code provides an example of how to control individually addressable LEDs using Arduino and FastLED library and showcases a few different LED animations.
Applications
Here are some applications of WS2812B Individually Addressable LEDs:
- Decorative lighting in homes, offices, and public spaces
- Mood lighting and ambiance creation in bars, restaurants, and hotels
- Stage lighting in concerts, theater productions, and events
- Automotive lighting in cars, motorcycles, and bicycles
- Wearable technology such as clothing and accessories
- Signage and advertisement boards
- Gaming peripherals and setups
- DIY projects and hobbyist electronics
- Visual displays and art installations
Conclusion
By following the steps outlined in this article, you can create your own customized lighting effects, from colorful patterns to complex animations. With a little creativity and some basic programming skills, the possibilities are endless. So go ahead and experiment with WS2812B LEDs and Arduino to bring your ideas to life!