Introduction: Controlling a WS2812B Based LED Strip Using Arduino

About: Love to share knowledges about IC industry, from car to smart home, which is around our daily life. Visit My Site!

Controlling a WS2812B based LED strip using Arduino Step by Step.

Supplies

​​1. Arduino UNO R3 Development Board​​

​​2. LED light strip based on WS2812B​​

​​3. Several Dupont lines​​

4. Arduino IDE​​

5. LED light strip driver library​​

Step 1: Hardware Connection-1

The hardware connection method is shown in the figure below:

Step 2: Hardware Connection-2

When wiring, be sure to pay attention to the pins of the interface. Do not connect the positive and negative poles incorrectly.

Step 3: Light Up the Light Strip

The light strip I purchased has 30 small lamp beads. Next, I will drive the light strip through Arduino to cycle through the three colors of red, green, and blue.


The code to light up the small lamp beads is actually very simple. First, you need to import the driver library:

#include <FastLED.h>

#define LED_PIN   7

#define NUM_LEDS  30

CRGB leds[NUM_LEDS];


The color of the lamp is determined by the three primary colors. Therefore, to control the color of the lamp, you only need to configure the intensity of the three colors. To brighten white light, you only need to configure the following:


CRGB ( 255, 255, 255)

Finally, string the code together:

#include <FastLED.h>

#define LED_PIN   7

#define NUM_LEDS  30

CRGB leds[NUM_LEDS];

void setup() {

 FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

}

void loop() {

 // Red

 for (int i = 0; i <= 29; i++) {

  leds[i] = CRGB ( 255, 0, 0);

  FastLED.show();

  delay(40);

 }


 // Green

 for (int i = 0; i <= 29; i++) {

  leds[i] = CRGB ( 0, 255, 0);

  FastLED.show();

  delay(40);

 }


 // Blue

 for (int i = 0; i <= 29; i++) {

  leds[i] = CRGB ( 0, 0, 255);

  FastLED.show();

  delay(40);

 }


}