Chapter 8 ESP32 Driving LED Lights

Learning goals and objectives

Mastering LED Light Circuit Design: Control Methods

Master the relevant parameter settings of IO configuration by ESP32 library functions

Master the operation of ESP32 library functions for IO control

Write LED blinking and running lights 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 GPIO interface of ESP32

Set IO mode function: gpio_set_pull_mode();

function prototype

esp_err_t gpio_set_pull_mode

(

gpio_num_t gpio_num,

gpio_pull_mode_t pull

)

function

Set IO mode

parameter

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

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

GPIO_MODE_DISABLE: no input and no output

GPIO_MODE_INPUT: input mode

GPIO_MODE_OUTPUT: output mode

GPIO_MODE_OUTPUT_OD: Open-drain output mode

GPIO_MODE_INPUT_OUTPUT_OD: Open-drain input-output mode

GPIO_MODE_INPUT_OUTPUT: Input output mode

 

return value

ESP_OK: success

ESP_ERR_INVALID_ARG : parameter error

Set IO output value function: gpio_set_level();

function prototype

esp_err_t gpio_set_level

(

gpio_num_t gpio_num,

uint32_t level

)

function

Set IO output value

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

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

 

code writing

flash code

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

#include <stdio.h>

#include "freertos / FreeRTOS.h"

#include "freertos/task.h"

#include "driver/gpio.h"

#include "sdkconfig.h"

#define LED_R_IO 2

#define LED_G_IO 18

#define LED_B_IO 19

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);

    while(1) {undefined

        // red light on

        gpio_set_level(LED_R_IO, 0);

        vTaskDelay(100 / portTICK_PERIOD_MS);

// red light off

        gpio_set_level(LED_R_IO, 1);

        vTaskDelay(100 / portTICK_PERIOD_MS);

    }

}

 

Source code of running water lamp

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

void app_main()

{undefined

//select IO

    gpio_pad_select_gpio (LED_R_IO);

    gpio_pad_select_gpio (LED_G_IO);

    gpio_pad_select_gpio (LED_B_IO);

    //set IO as output

    gpio_set_direction(LED_R_IO, GPIO_MODE_OUTPUT);

    gpio_set_direction(LED_G_IO, GPIO_MODE_OUTPUT);

    gpio_set_direction(LED_B_IO, GPIO_MODE_OUTPUT);

    while(1) {undefined

        //Only turn on the red light

        gpio_set_level(LED_R_IO, 0);

        gpio_set_level(LED_G_IO, 1);

        gpio_set_level(LED_B_IO, 1);

        vTaskDelay(100 / portTICK_PERIOD_MS);

        // only turn on the green light

        gpio_set_level(LED_R_IO, 1);

        gpio_set_level(LED_G_IO, 0);

        gpio_set_level(LED_B_IO, 1);

        vTaskDelay(100 / portTICK_PERIOD_MS);

        //Only turn on the blue light

        gpio_set_level(LED_R_IO, 1);

        gpio_set_level(LED_G_IO, 1);

        gpio_set_level(LED_B_IO, 0);

        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

Flashing light: the red light is reversed once every 100ms

Running water light: red, green and blue 100ms cycle lighting

Summary of driving LED lights

The above method is to use the library function to configure the GPIO, or you can use the structure to configure, first set the structure parameters, and then use the structure to configure the IO, to achieve the same function.

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

Click me -> more ESP32 development guide series catalog

Tags: Chapter 8 ESP32 Driving LED Lights

ESP32 development ESP32 Development Guide

Related: Chapter 8 ESP32 Driving LED Lights