Chapter 10 ESP32 Development Guide --- PWM full-color LED light display

Learning goals and objectives

Learn LED light circuit and hardware principle

Learn the configuration of the PWM (ledc) function of the ESP32

Master PWM (ledc) control full-color LED light gradient program

Introduction to Full Color LED Lights

The full-color LED light is essentially an LED light that integrates red, green, and blue monochromatic illuminants into a small area. When controlling, the three-color lamps output different light intensities, which can be mixed to obtain For different colors, the principle of color mixing is the same as that of the three primary colors of light.

For example, if the red, green and blue lights can control the output light intensity to [0:255] levels, then the light can be mixed to obtain all colors represented by RGB888 (including pure black represented by all three RGB lamps being off). ).

Full color LED light control principle

The RGB light in the development board of this tutorial is a full-color LED light. When the basic control principle of LED was introduced earlier, it can only control the on-off of the RGB three-color light, that is, each RGB light has two kinds of [0:1]. level, so only 8 colors can be combined.

To use ESP32 to control LED lights to output various brightness levels, it can be achieved by controlling the duty cycle of the output pulses. It is the PWM function we are going to learn today, which is the ledc function in ESP32.

The above figure lists the pulse waveforms with the same period and duty cycle of 100%, 80%, 50 and 20% respectively. If such pulses are used to control the LED lights, the ratio of the length of the LED lights on and off can be controlled. If the frequency of the pulse is increased, the LED light will switch on and off at a high frequency. Due to the persistence of vision effect, the human eye cannot see the flickering phenomenon caused by the switching of the LED light. difference in brightness. That is, in a single control cycle, the longer the average time of the LED light on, the higher the brightness, and vice versa.

Divide the duty cycle of the pulse signal into 256 levels, which can be used to control the LED light to output 256 kinds of brightness, and use three such signals to control the RGB light to get the effect of 256*256*256 color mixing. To control the duty cycle, you can directly use the LEDC function of ESP32.

Hardware Design and Principle

Same as the schematic diagram of the LED chapter.

This experiment board is connected to an RGB color light. The RGB color light is actually composed of three red, green, and blue LED lights. By controlling the combination of RGB color intensities, various colors can be mixed.

LED label

LED color

Connect to the pins of the ESP32

LP2A

red

IO2

LP2B

green

IO18

LP2C

blue

IO19

 

If the connection method or pins of the LED lights of the experimental board are different, you only need to modify the pins according to our project, and the control principle of the program is the same.

software design

code logic

Introduction to PWM (ledc) interface of ESP32

LEDC configuration function: ledc_channel_config();

function prototype

esp_err_t ledc_channel_config

(

const ledc_channel_config_t* ledc_conf

)

function

LEDC configuration function

parameter

[in] ledc_conf: ledc configuration structure ledc_channel_config_t

typedef struct {undefined

int gpio_num;//IO number

ledc_mode_t speed_mode;//speed

ledc_channel_t channel;//channel

ledc_intr_type_t intr_type;//Interrupt enable

ledc_timer_t timer_sel; //Timer channel

uint32_t duty; //duty cycle

} ledc_channel_config_t;

return value

ESP_OK: success

ESP_ERR_INVALID_ARG : parameter error

LEDC gradient installation function: ledc_fade_func_install();

function prototype

esp_err_t ledc_fade_func_install

(

int_alloc_flags

)

function

LEDC Gradient Installation Enable Function

parameter

[in] intr_alloc_flags: allocation interrupt flags

return value

ESP_OK: success

ESP_ERR_INVALID_ARG : parameter error

LEDC gradient function: ledc_set_fade_with_time();

function prototype

esp_err_t ledc_set_fade_with_time

(

ledc_mode_t speed_mode,

ledc_channel_t channel,

uint32_t target_duty,

int max_fade_time_ms

)

function

LEDC gradient function

parameter

[in] speed_mode: speed

[in] channel: channel

[in] target_duty: target duty cycle

[in] max_fade_time_ms: The time it takes to reach the target duty cycle

return value

ESP_OK: success

ESP_ERR_INVALID_ARG : parameter error

ESP_ERR_INVALID_STATE: LEDC gradient not installed

ESP_FAIL: LEDC gradient installation failed

LEDC gradient start function: ledc_fade_start();

function prototype

esp_err_t ledc_fade_start

(

ledc_mode_t speed_mode,

ledc_channel_t channel,

ledc_fade_mode_t wait_done

)

function

LEDC gradient start function

parameter

[in] speed_mode: speed

[in] channel: channel

[in] wait_done: whether to wait

return value

ESP_OK: success

ESP_ERR_INVALID_ARG : parameter error

ESP_ERR_INVALID_STATE: LEDC gradient not installed

For more detailed interfaces, please refer to the official guide .

Gradient RGB lantern source code preparation

Get the button state, equal to 0 means the button is pressed, and then wait to let go to switch the light state.

Contains header files, IO macro definitions, ledc macro definitions

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#include <stdio.h>

#include "esp_system.h"

#include "esp_spi_flash.h"

#include "esp_wifi.h"

#include "esp_event_loop.h"

#include "esp_log.h"

#include "esp_err.h"

#include "nvs_flash.h"

#include "freertos / FreeRTOS.h"

#include "freertos/task.h"

#include "driver/ledc.h"

#include <stdio.h>

#include "freertos / FreeRTOS.h"

#include "freertos/task.h"

#include "driver/ledc.h"

#define LED_R_IO 2

#define LED_G_IO 18

#define LED_B_IO 19

#define LEDC_MAX_DUTY (8191) //2 to the 13th power -1 (13-bit PWM)

#define LEDC_FADE_TIME (1000) //Fade time (ms)

 

RGB lamp PWM (ledc) configuration and enable gradient function

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

/*

* void ledc_init(void): Timer 0 is used in PWM mode and outputs 3-channel LEDC signal

*/

void ledc_init(void)

{undefined

//Timer configuration structure

ledc_timer_config_t ledc_timer;

//Timer configuration->timer0

ledc_timer.duty_resolution = LEDC_TIMER_13_BIT; //PWM resolution

ledc_timer.freq_hz = 5000; //frequency

ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;   //速度

ledc_timer.timer_num = LEDC_TIMER_0; // select timer

ledc_timer_config(&ledc_timer); //Set timer PWM mode

//PWM channel 0 configuration->IO2->red light

g_ledc_ch_R.channel = LEDC_CHANNEL_0; //PWM channel

g_ledc_ch_R.duty = 0; //duty cycle

g_ledc_ch_R.gpio_num = LED_R_IO; //IO mapping

g_ledc_ch_R.speed_mode = LEDC_HIGH_SPEED_MODE; //速度

g_ledc_ch_R.timer_sel = LEDC_TIMER_0; //Select timer

ledc_channel_config(&g_ledc_ch_R); //配置PWM

//PWM channel 1 configuration->IO18->green light

g_ledc_ch_G.channel = LEDC_CHANNEL_1; //PWM channel

g_ledc_ch_G.duty = 0; //duty cycle

g_ledc_ch_G.gpio_num = LED_G_IO; //IO mapping

g_ledc_ch_G.speed_mode = LEDC_HIGH_SPEED_MODE; //速度

g_ledc_ch_G.timer_sel = LEDC_TIMER_0; //Select timer

ledc_channel_config(&g_ledc_ch_G); //配置PWM

//PWM channel 2 configuration->IO19->Blue light

g_ledc_ch_B.channel = LEDC_CHANNEL_2; //PWM channel

g_ledc_ch_B.duty = 0; //duty cycle

g_ledc_ch_B.gpio_num =LED_B_IO ; //IO mapping

g_ledc_ch_B.speed_mode = LEDC_HIGH_SPEED_MODE; //速度

g_ledc_ch_B.timer_sel = LEDC_TIMER_0; //Select timer

ledc_channel_config(&g_ledc_ch_B); //配置PWM

//Enable ledc gradient function

ledc_fade_func_install(0);

}

 

Main function: Gradient RGB light

RGB light gradient from 0% to 100% (dark to bright), lasting 1 second

RGB light gradient from 100% to 0% (bright to dark), lasting 1 second

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/*

 * Function entry of the application

*/

void app_main()

{    

ledc_init();//ledc initialization

    while(1)

{undefined

//ledc red light fades to 100%, time LEDC_FADE_TIME

ledc_set_fade_with_time(g_ledc_ch_R.speed_mode,

                                     g_ledc_ch_R.channel,

         LEDC_MAX_DUTY,

                                     LEDC_FADE_TIME);

//start gradient

ledc_fade_start(g_ledc_ch_R.speed_mode,

      g_ledc_ch_R.channel,

      LEDC_FADE_NO_WAIT);

// skip

           vTaskDelay(LEDC_FADE_TIME / portTICK_PERIOD_MS);

 

//ledc red light fades to 0%, time LEDC_FADE_TIME

ledc_set_fade_with_time(g_ledc_ch_R.speed_mode,

                                      g_ledc_ch_R.channel,

          0,

                                      LEDC_FADE_TIME);

//start gradient

ledc_fade_start(g_ledc_ch_R.speed_mode,

      g_ledc_ch_R.channel,

      LEDC_FADE_NO_WAIT);

// skip

           vTaskDelay(LEDC_FADE_TIME / portTICK_PERIOD_MS);

}

}

 

hardware connection

The development board has already connected the RGB lights by default, and you can download the program. To use other development boards, you need to modify the program or modify the hardware connection.

Show results

The RGB lights go from dark to bright, then from bright to dark, and so on.

PWM (ledc) summary

Gradients are more commonly used at indicator lights.

It can be used when there is no gradient, and the duty cycle function can be set directly.

Set duty cycle ledc_set_duty();

There are also several commonly used ledc library functions, please refer to the ledc.h file, it is too simple to introduce.

Source address: https://github.com/xiaolongba/wireless-tech

Click me -> more ESP32 development guide series catalog

Tags: Chapter 10 ESP32 Development Guide --- PWM full-color LED light display

ESP32 development ESP32 Development Guide ESP-IDF

Related: Chapter 10 ESP32 Development Guide --- PWM full-color LED light display