Supplementary Chapter 1: Use of ESP32 Timer

 

Learning goals and objectives

  1. Mastering LED Light Circuit Design: Control Methods
  2. Master the library functions of the ESP32 timer
  3. Write LED flashing light program

Hardware Design and Principle

This experiment board is connected to an RGB color light. The RGB color light is actually composed of three LED lights, which are red, green, and blue. By controlling the combination of RGB color intensity, various colors can be mixed. This chapter only learns How to switch and toning are taught in the PWM chapter.

The cathodes of these LED lights are connected to the GPIO pins of ESP32 through 0 ohm resistors . As long as we control the level output state of the GPIO pins, we can control the on and off of the LED lights. Remove the 0 ohm resistor in the picture, you can cut off the connection with the microcontroller and release this GPIO. The 3 LED lights occupy the pins of the ESP32 as follows:

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 the soft timer interface of ESP32

Create timer function: esp_timer_create();

function prototype

esp_err_t esp_timer_create

(

const esp_timer_create_args_t* create_args,

esp_timer_handle_t* out_handle

)

function

Create timer function

parameter

[in]create_args: Timer structure

typedef struct {undefined

    esp_timer_cb_t callback; //Timer time to callback

    void* arg; //The parameter to be passed into the callback

esp_timer_dispatch_t dispatch_method; //call callback from task or ISR

 const char* name; //Timer name, used by esp_timer_dump function

} esp_timer_create_args_t;

[in]out_handle: Timer handle

return value

ESP_OK: success

ESP_ERR_INVALID_ARG : parameter error

ESP_ERR_INVALID_STATE: The timer has run

Start a single timer function: esp_timer_start_once(); basically the same as below

Start the periodic timer function: esp_timer_start_periodic();

function prototype

esp_err_t esp_timer_start_periodic

(

esp_timer_handle_t timer,

uint64_t period

)

function

Start cycle timer

parameter

[in]timer: Timer handle

[in]period: Timing period, in microseconds, 1000 means 1ms

return value

ESP_OK: success

ESP_ERR_INVALID_ARG : parameter error

ESP_ERR_INVALID_STATE: The timer has run


Stop timer function: esp_timer_stop();

function prototype

esp_err_t esp_timer_stop

(

esp_timer_handle_t timer

);

function

stop timer

parameter

[in]timer: Timer handle

return value

ESP_OK: success

ESP_ERR_INVALID_STATE: The timer has stopped

Delete timer function: esp_timer_delete();

function prototype

esp_err_t esp_timer_delete

(

esp_timer_handle_t timer

);

function

delete timer

parameter

[in] gpio_num: pin number, 0~34 (existing part)

[in]pull: IO mode, you can set:

0: output low

1: output high

 

return value

ESP_OK: success

ESP_ERR_INVALID_ARG : parameter error

Get timer time function: esp_timer_get_time();

 

function prototype

int64_t esp_timer_get_time()

function

Set IO output value

parameter

none

return value

The number of microseconds since the esp timer init was called (usually happens early in the application startup)

 

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

 

 

 

code writing

Timer configuration

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

void app_main() {undefined

  //select IO

    gpio_pad_select_gpio (LED_R_IO);

    //set IO as output

    gpio_set_direction(LED_R_IO, GPIO_MODE_OUTPUT);

 

//Initialize the timer structure

esp_timer_create_args_t fw_timer =

{undefined

.callback = &fw_timer_cb, //callback function

.arg = NULL, //argument

.name = "fw_timer" //Timer name

};

 

// timer creation, start

esp_err_t err = esp_timer_create(&fw_timer, &fw_timer_handle);

err = esp_timer_start_periodic(fw_timer_handle, 1000 * 1000);//1 second callback

if(err == ESP_OK)

{undefined

printf("fw timer cteate and start ok!\r\n");

}

}

 

Timer callback function

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

void fw_timer_cb(void *arg)

{undefined

//get timestamp

int64_t tick = esp_timer_get_time();

printf("timer cnt = %lld \r\n", tick);

 

if (tick > 50000000) //50 seconds end

{undefined

//Timer pause, delete

esp_timer_stop(fw_timer_handle);

esp_timer_delete(fw_timer_handle);

printf("timer stop and delete!!! \r\n");

// restart

esp_restart();

}

 

gpio_set_level(LED_R_IO, 0);

vTaskDelay(100 / portTICK_PERIOD_MS);

gpio_set_level(LED_R_IO, 1);

vTaskDelay(100 / portTICK_PERIOD_MS);

 

}

 

 

 

hardware connection

The development board has been connected to the LED 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 red light flashes once every 1000ms

 

 

Timer summary

Mainly learn the use of ESP32 soft timer, the method is very simple. Create a "start" callback, you can get the time, or you can stop "pause."

Source address: https://github.com/HX-IoT/ESP32-Developer-Guide

Click me -> more ESP32 development guide series catalog

Related: Supplementary Chapter 1: Use of ESP32 Timer