How to Control WS2812B Addressable RGB LED Strip with ESP32

Introduction

WS2812B addressable RGB LED strips have gained popularity for their ability to create mesmerizing lighting effects in a wide range of projects. By combining these strips with the powerful ESP32 microcontroller and leveraging the Adafruit_NeoPixel library, you can effortlessly control the color and behavior of each LED in the strip.

How to Control WS2812B Addressable 5050 RGB LED Strip with ESP32
WS2812B Addressable RGB LED Strip with ESP32
The WS2812B LED strip offers endless possibilities for creative projects. You can choose the perfect size, sealant, or LED density, with various models available to suit your needs. These LED Strips are Encased in weatherproof silicone and can be used outdoors without worrying about damage from rain or dust.

What sets the WS2812B LED strip apart is its ability to control each LED's brightness and color. This feature enables you to create captivating lighting effects with ease. Each LED in the strip contains an integrated circuit (IC), allowing for seamless communication through a one-wire interface. You can control multiple LEDs using just one digital pin on your Arduino board.

WS2812 Addressable RGB LED IC
 WS2812 Addressable RGB LED IC
The flexibility of these LED strips is another advantage. As indicated, you can easily adjust their length by cutting the strip at designated intervals. 

Adjusting WS2812B addressable LED Strip length
Each segment of the strip houses one RGB LED, enabling customization to fit your specific project requirements. Connectors are conveniently provided at both ends of the strip, making it simple to connect to an Arduino or a breadboard.

Prerequisites

To follow along with this tutorial, you'll need the following:

  • An ESP32 development board.
  • A WS2812B addressable RGB LED strip.
  • Jumper wires for connecting the LED strip to the ESP32.
  • Arduino IDE is installed on your computer.
  • Adafruit_NeoPixel library installed in the Arduino IDE.

Step 1: Install the Adafruit_NeoPixel Library 

Before we begin, let's ensure we have the Adafruit_NeoPixel library installed in the Arduino IDE. This library simplifies the control of addressable RGB LED strips. Here's how you can install it:

  1. Open the Arduino IDE.
  2. Go to "Sketch" -> "Include Library" -> "Manage Libraries."
  3. In the Library Manager, search for "Adafruit NeoPixel."
  4. Click on the library and then click "Install" to install it.

Step 2: Wiring the WS2812B LED Strip to the ESP32 

To control the WS2812B LED strip, we must establish the connections between the strip and the ESP32. Follow these steps to wire them together:

WS2812B Addressable RGB LED Strip Pinout
WS2812B Addressable RGB LED Strip Pinout

  1. Connect the VCC pin of the LED strip to the 5v pin of the ESP32.
  2. Connect the GND pin of the LED strip to the GND pin of the ESP32.
  3. Connect the LED strip's DIN (Data Input) pin to any digital pin of the ESP32 (e.g., GPIO 4).
  4. Connect extra LED strips in the previous LED strip's DO (Data Output) pin.

Step 3: Setting Up the Arduino Sketch

Let's start coding the ESP32 to control the LED strip using the Adafruit_NeoPixel library. Follow the steps below:

1. Open a new Arduino sketch in the IDE.
2. Include the Adafruit_NeoPixel library at the top of your sketch:

#include <Adafruit_NeoPixel.h>code-box

3. Define the LED strip parameters. We need to specify the pin to which the LED strip is connected and the number of LEDs in the strip:

#define LED_PIN 4 // Digital pin connected to the LED strip
#define LED_COUNT 60 // Number of LEDs in the strip code-box

4. Create an instance of the Adafruit_NeoPixel class. We'll use this instance to control the LED strip:

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);code-box

5. In the setup() function, initialize the LED strip:

void setup() {
  strip.begin(); // Initialize the LED strip
  strip.show(); // Turn off all LEDs initially
}code-box

Step 4: Controlling the LED Strip 

It's time to control the LED strip behavior using the loop() function. Let's start with a simple example that sets all LEDs to red:

void loop() {
  // Set all LEDs to red
  for (int i = 0; i < LED_COUNT; i++) {
    strip.setPixelColor(i, strip.Color(255, 0, 0)); // Red color (RGB values)
  }
  strip.show(); // Send the updated pixel colors to the strip
  delay(1000); // Wait for 1 second before changing colors
}code-box

In this example, we use a for loop to iterate through each LED in the strip. We set the color of each LED to red using the setPixelColor() method. The strip.Color() function takes three arguments representing the RGB values (255 for red, 0 for green, and 0 for blue) to create the desired color. Finally, we call strip.show() to update the LED strip with the new color RGB values and then delay(1000) to wait for 1 second before changing the colors.

Step 5: Upload the Code and Test

Copy the following Arduino code to control WS2812B Addressable 5050 RGB LED Strip with ESP32.

#include <Adafruit_NeoPixel.h>
#define LED_PIN    4       // Digital pin connected to the LED strip
#define LED_COUNT  60      // Number of LEDs in the strip
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
  strip.begin();     // Initialize the LED strip
  strip.show();      // Turn off all LEDs initially
}
void loop() {
  // Set all LEDs to red
  for (int i = 0; i < LED_COUNT; i++) {
    strip.setPixelColor(i, strip.Color(255, 0, 0));  // Red color (RGB values)
  }
  strip.show();  // Send the updated pixel colors to the strip
  delay(1000);   // Wait for 1 second before changing colors
}code-box

Upload the sketch to your ESP32 board by clicking the "Upload" button in the Arduino IDE. After uploading, the ESP32 will start controlling the WS2812B LED strip. You should see all the LEDs in the strip turn red and change color every second.

Additional Useful Functions of the Adafruit_NeoPixel Library for WS2812B Addressable RGB LED Strips

The Adafruit_NeoPixel library provides several useful functions to enhance your control over WS2812B addressable RGB LED strips. Here are a few additional functions that you can leverage:


  1. setBrightness(brightness): This function allows you to adjust the overall brightness of the LED strip. The brightness parameter accepts 0 (off) and 255 (maximum brightness). It is particularly useful when you want to create dynamic lighting effects or conserve power by reducing the overall brightness.
  2. clear(): The clear() function turns off all the LEDs in the strip, effectively setting them to black (no light emitted). This function is handy when you want to quickly reset the strip or turn off all the LEDs.
  3. fill(color, startLED, numLEDs): With the fill() function, you can set a specific color to a range of LEDs in the strip. The color parameter accepts a 32-bit RGB color value, and the startLED and numLEDs parameters determine the starting LED and the number of LEDs to fill with the specified color.
  4. setPixelColor(LED, color): This function allows you to set the color of an individual LED in the strip. The LED parameter specifies the index of the LED (starting from 0), and the color parameter accepts a 32-bit RGB color value.
  5. gamma32(color): The gamma32() function applies gamma correction to the specified color value. Gamma correction adjusts the perceived brightness of colors to achieve more accurate and pleasing visuals. This function is typically used when you want to fine-tune the color accuracy of your LED strip.
  6. show(): The show() function is crucial for updating the LED strip with the latest color values you've set. Once you've made changes to the LED colors using functions like setPixelColor() or fill(), calling show() will send the updated pixel data to the LED strip, causing the changes to take effect.

These additional functions give you greater control and flexibility when working with WS2812B LED strips using the Adafruit_NeoPixel library. You can explore and experiment with these functions to create captivating lighting effects and bring your projects to life with stunning visuals.

Conclusion

In this article, we've learned how to control a WS2812 or WS2812B addressable RGB LED strip using an ESP32 microcontroller and the Adafruit_NeoPixel library. We covered the wiring setup and library installation and provided an example code that sets the LEDs to red. With this foundation, you can explore and create custom lighting effects by modifying the code and experimenting with different colors and patterns. Have fun with your addressable RGB LED strip projects.

7/Post a Comment/Comments

  1. How to control addressable LED with Arduino? Is it possible?

    ReplyDelete
  2. We can modify the LED Strip code by creating custom lighting effects and experimenting with different colors and patterns.

    ReplyDelete
  3. Achei muito interessante aprender como é a codificação para criar efeitos de iluminação personalizados. Parabéns, foi muito claro nas explicações.

    ReplyDelete
  4. Is it very costly? Can I tech my student at School?

    ReplyDelete

Post a Comment