ESP32-C3, WS2812 and rainbow effect

Hey So last time I was playing with the esp and the example of the WS2812

After reading a little bit more the library I found how to change the colors and it’s quite easy, so the first thing I did was a fade with a single color.

Everything else in the main program remain the same, the while loop was modified with the following code

while (true) {
	for (int i = 255; i != 0; i-=3) {
		// Write RGB values to strip driver
		ESP_ERROR_CHECK(strip->set_pixel(strip, 0, 0, 0, i));
		ESP_ERROR_CHECK(strip->refresh(strip, 100));
		vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
	}
}

And as you can see it’s pretty easy to get a fade, but I’ll get a little deeper on the library and explain what it is and how it works. At the end I’ll make another program for you to see how it works.

Let’s dissect the code

	#define RMT_TX_CHANNEL RMT_CHANNEL_0
	#define EXAMPLE_CHASE_SPEED_MS (10)

the RMT_TX_CHANNEL defines in which channel of the RMT peripheral will be the signal routed (the RMT peripheral is specific hardware, designed for IR communication, but it can be used as a one-wire protocol, neat huh ?) And the next definition is just to have a common variable for all the delays.

The next chunk of code is something that you’ll probably see a lot on many of the example codes of Espressif, this is error checking, this part of the code is in place to give you a feedback of whether the operation was implemented sucessfully.

    ESP_ERROR_CHECK(rmt_config(&config));
    ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));

Let’s see the library and it’s elements.

if you go to the subfolder \components\led_strip\include you will find a file named led_strip.h this file has the class definitions for the library of the WS2812 driver.

Now let’s see which elements contains.

esp_err_t (*set_pixel)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue);

esp_err_t (*refresh)(led_strip_t *strip, uint32_t timeout_ms);

esp_err_t (*clear)(led_strip_t *strip, uint32_t timeout_ms);

esp_err_t (*del)(led_strip_t *strip);

this gives us an inside of how the program works, first all of the methods use pointers, why? by using pointer you’re actually using less memory for temporary variables because you modify the variable in the address it’s stored instead of retrieving the variable, modifying in a temp variable, then returning it to the original address and continuing with the next one (its’s easier to use pointers, from the point of view of programming efficiency, this way of programming is widely used in high performance coding and STM32 cube Mx uses this same paradigm).

esp_err_t (*set_pixel)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue);

this line of code tells us how the driver works, we parse the pointer of the led strip, the index of the pixel you want to modify (0 to n-1), then the value of Red, Green and Blue color than range from 0 to 255.

esp_err_t (*refresh)(led_strip_t *strip, uint32_t timeout_ms);

this line of code tells the ws2812 to refresh the values of the color and you specify an timeout, if the proces takes longer than this timeout to complete an error is raised, this will let you know that something went wrong (maybe a loose wire or bad connection)

esp_err_t (*clear)(led_strip_t *strip, uint32_t timeout_ms);

this line of code deletes all the values of color for the pixel, essentially turning it off

esp_err_t (*del)(led_strip_t *strip);

this line of code deletes the led_strip object this will release all the resources for other task of the processor

Now you know a little bit more about the library of the WS2812, now I’ll leave you with a rainbow effect.

you can find the main program here.

bye