# 7. WS2812B

## 7.1. Overview
 This section focuses on understanding ws2812 and learning how to declare a library and instantiate objects through the
 library to light up RGB lights one by one in turn. We will use a library designed specifically for these sensors, which will
 keep our code short and easy to write.

## 7.2. Working principle

<img src="https://mkpark.iptime.org:10443/esp32S3_tscinbuny_tutorial/images/7_lession_ws2812b.png" width="150" height="150"/>
 
 WS2812B is an intelligent externally controlled LED light source that integrates control circuit and light-emitting circuit. Its
 appearance is the same as a 5050LED lamp bead, and each component is a pixel. There is an intelligent digital interface data
 latch signal shaping amplification drive circuit, a high-precision internal oscillator and a 12V high-voltage programmable
 fixed current control part inside the pixel, which effectively ensures that the pixel light color is highly consistent.
 The data protocol adopts single-line zero return code communication method. After the pixel is powered on and reset, the
 DIN client receives the data from the controller and first sends out the 24-bit data. After extracting the first pixel, it is sent to
 the pixel data latch. The remaining data is shaped and amplified by the internal shaping processing circuit. . It starts to be

 forwarded to the next cascade pixel through the DO output port. After each pixel is transmitted, the signal is reduced by 24
 bits.The pixels adopt automatic shaping and forwarding technology, so that the number of cascades of pixels is not limited
 by signal transmission, but only limited by the signal transmission speed requirements.
 
## 7.3.Connect the lines

<img src="https://mkpark.iptime.org:10443/esp32S3_tscinbuny_tutorial/images/7.lession_connect_line.png" width="800" height="600"/>

## 7.4.Upload code

### 7.4.1. Connect the main control board to the computer using a USB cable

<img src="https://mkpark.iptime.org:10443/esp32S3_tscinbuny_tutorial/images/upload_code_program.png" width="600" height="200"/>

### 7.4.2 Open the program file (path: 2_ESP32_S3_PLUS\ Lesson_7_WS2812B )

 Also select the board type as ESP32S3 Dev Module and select the COM number newly displayed when the USB is plugged
 in . Then click "Upload" to start compiling and uploading the program to the main control board.
 
## 7.5. Code analysis

Declare the FastLED library, define the ws2812 pin as 6, the number of lamp beads as 12 and the brightness value as 64.

```cpp
#include <FastLED.h>    // FastLED Declare the FastLED library

#define LED_PIN 6       // Define the WS2812 RGB lamp pin
#define NUM_LEDS 12     // Define the number of beads
#define BRIGHTNESS 64   // Set the light level from 0 to 255, with the higher the light
```

Instantiate ws2812 as LEDs, set the NEOPIXEL type and initialize the light brightness.

```cpp

CRGB leds[NUM_LEDS];    //An array of CRGB type with length NUM_LEDS is instantiated to control the WS2812B LED strip

void setup() {
  FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);  // Use the NEOPIXEL type
  FastLED.setBrightness(BRIGHTNESS);                   // Initialize the light level
}
```

The loop function lights up the LEDs one by one and displays green

```cpp
void loop() {
  // Light the leds one by one to show green
  for (int i = 0; i < NUM_LEDS; i++) {
    fill_solid(leds, NUM_LEDS, CRGB::Black);  // Turn off all leds
    leds[i] = CRGB::Green;                    // Light only the current LED
    FastLED.show();
    delay(100);
  }
}
```