2. Chapter WS2812
In this chapter, we will learn to use the onboard WS2812. Make the RGB LEDs to emit various color via a pin.
2.1. Project WS2812
Learn the basic usage of WS2812 and use it to flash red, green, blue and white.
2.1.1. Component List
ESP32-S3 WROOM x1 |
USB cable x1 |
|---|---|
|
|
2.1.3. Circuit
Connect Freenove ESP32-S3 to your computer using the USB cable.
2.1.4. Sketch
This code uses a library named “Freenove_WS2812_Lib_for_ESP32”. If you have not yet installed it, please do so first.
2.1.4.1. How to install the library
Open Arduino IDE, click Sketch -> Include Library -> Add .ZIP Library. In the pop-up window, find the file named “./Libraries/Freenove_WS2812_Lib_for_ESP32.Zip” which locates in this directory, and click OPEN.
2.1.4.2. Sketch_02_WS2812
Download the code to ESP32-S3 WROOM and LED WS2812 begins to light up in red, green, blue, white and bla
The following is the program code:
1/**********************************************************************
2 Filename : WS2812
3 Description : The control board carries ws2812, showing colors such as red, green, blue and white.
4 Auther : www.freenove.com
5 Modification: 2023/02/22
6**********************************************************************/
7#include "Freenove_WS2812_Lib_for_ESP32.h"
8
9#define LEDS_COUNT 1
10#define LEDS_PIN 48
11#define CHANNEL 0
12
13Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(LEDS_COUNT, LEDS_PIN, CHANNEL, TYPE_GRB);
14
15int m_color[5][3] = { {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 255}, {0, 0, 0} };
16int delayval = 100;
17
18void setup() {
19 strip.begin();
20 strip.setBrightness(10);
21}
22void loop() {
23 for (int j = 0; j < 5; j++) {
24 for (int i = 0; i < LEDS_COUNT; i++) {
25 strip.setLedColorData(i, m_color[j][0], m_color[j][1], m_color[j][2]);
26 strip.show();
27 delay(delayval);
28 }
29 delay(500);
30 }
31}
To use some libraries, first you need to include their header file.
1#include "Freenove_WS2812_Lib_for_ESP32.h"
Define the pins connected to the ws2812, the number of LED,s and RMT channel values.
1#define LEDS_COUNT 1
2#define LEDS_PIN 48
3#define CHANNEL 0
Use the above parameters to create a WS2812 object strip.
1Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(LEDS_COUNT, LEDS_PIN, CHANNEL, TYPE_GRB);
Define the color values to be used, such as red, green, blue, white, and black.
1int m_color[5][3] = { {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 255}, {0, 0, 0} };
Define a variable to set the time interval for each led to light up. The smaller the value is, the faster it will light up.
1int delayval = 100;
Initialize strip() in setup() and set the brightness.
1strip.begin();
2strip.setBrightness(10);
In the loop(), there are two “for” loops, the internal for loop is to light the LED one by one, and the external one to switch colors. strip.setLedColorData() is used to set the color, but it does not change immediately. Only when strip.show() is called will the color data be sent to the LED to change the color.
1for (int j = 0; j < 5; j++) {
2 for (int i = 0; i < LEDS_COUNT; i++) {
3 strip.setLedColorData(i, m_color[j][0], m_color[j][1], m_color[j][2]);
4 strip.show();
5 delay(delayval);
6 }
7 delay(500);
8}
2.1.4.3. Reference
- Freenove_ESP32_WS2812(u16 n = 8, u8 pin_gpio = 2, u8 chn = 0, LED_TYPE t = TYPE_GRB)
Constructor to create a ws2812 object.
Before each use of the constructor, please add “#include “Freenove_WS2812_Lib_for_ESP32.h”
Parameters
n: The number of led.
pin_gpio: A pin connected to an led.
Chn: RMT channel, which has eight channels, 0-7, and uses channel by default. This means that you can use eight ws2812 modules for the display at the same time, and these modules do not interfere with each other
t: Types of LED.
YPE_RGB: The sequence of ws2812 module loading color is red, green and blue.
TYPE_RBG: The sequence of ws2812 module loading color is red, blue and green.
TYPE_GRB: The sequence of ws2812 module loading color is green, red and blue.
TYPE_GBR: The sequence of ws2812 module loading color is green, blue and red.
TYPE_BRG: The sequence of ws2812 module loading color is blue, red and green.
TYPE_BGR: The sequence of ws2812 module loading color is blue, green and red.
- void begin(void);
Initialize the LEDPixel object
- void setLedColorData (u8 index, u8 r, u8 g, u8 b);
- void setLedColorData (u8 index, u32 rgb);
- void setLedColor (u8 index, u8 r, u8 g, u8 b);
- void setLedColor (u8 index, u32 rgb);
Set the color of led with order number n.
- void show(void);
Send the color data to the led and display the set color immediately.
- void setBrightness(uint8_t);
Set the brightness of the LED.
If you want to learn more about this library, you can visit the following website: https://github.com/Freenove/Freenove_WS2812_Lib_for_ESP32

