Chapter 9 GPIO Input Key Operation of ESP32

Learning goals and objectives

Learn the touch button detection circuit and hardware principle

Learn the configuration when ESP32 GPIO is used as input

Master the library function to read the GPIO state

Master the touch key detection procedure

Hardware Design and Principle

When the mechanical contact of the key is opened or closed, due to the elastic effect of the contact, the key switch will not be turned on stably or disconnected at once. When the key is used, it will generate a ripple signal as shown in the figure below, which needs to be filtered by software debounce processing. , inconvenient for input detection. The buttons connected to this experiment board have hardware debounce function, as shown in the figure below. It uses the delay of capacitor charging and discharging to eliminate ripples, thereby simplifying software processing. The software only needs to directly detect the level of the pins. Good, you can use software to debounce again.

It can be seen from the schematic diagram of the buttons that when these buttons are not pressed, the input state of the GPIO pin is high (the circuit where the button is located is blocked, and the pin is connected to 3.3V through a resistor), when the button is pressed, the GPIO The input state of the pin is low level (the circuit where the button is located is turned on, and the pin is connected to GND). As long as we detect the input level of the pin, we can determine whether the button is pressed. 1 user button occupies the pins of ESP32 as follows:

key label

Connect to the pins of the ESP32

K3

IO34

If the connection method or pins of the experiment board buttons you use 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 output value function: int gpio_get_level(gpio_num_t gpio_num);

function prototype

int gpio_get_level

(

gpio_num_t gpio_num

)

function

Read IO input value

parameter

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

return value

key input value

0: Input low

1: Input high

After the button is pressed, let go and switch the light source to write the source code

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, led light state variable definitions

1

2

3

4

5

6

7

8

9

10

11

12

13

#include <stdio.h>

#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 KEY_IO 34

unsigned char led_r_status = 0;

unsigned char led_g_status = 0;

 

 

key recognition function

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

void key_read(void)

{undefined

    if(gpio_get_level(KEY_IO)==0)//Key pressed

    {undefined

        //Wait to let go, the stupidest way

        while(gpio_get_level(KEY_IO)==0);

        if (led_r_status==1)

        {undefined

            led_r_status = 0;

            gpio_set_level(LED_R_IO, 1);//不亮

        }

        else

        {undefined

            led_r_status = 1;

            gpio_set_level(LED_R_IO, 0);//亮

        }

    }

}

 

Main function: configure IO and call key recognition

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

void app_main()

{undefined

//select IO

    gpio_pad_select_gpio (LED_R_IO);

    gpio_pad_select_gpio (LED_G_IO);

    gpio_pad_select_gpio (KEY_IO);

    //Set light IO as output

    gpio_set_direction(LED_R_IO, GPIO_MODE_OUTPUT);

    gpio_set_level(LED_R_IO, 1);//不亮

    gpio_set_direction(LED_G_IO, GPIO_MODE_OUTPUT);

    gpio_set_level(LED_G_IO, 1);//off

    //Set key IO input

    gpio_set_direction(KEY_IO, GPIO_MODE_INPUT);

    

    while(1) {undefined

        key_read();//Key recognition

    }

}

 

Press the button to switch the red light, release the green light to switch the source code

Create a new cache key_status[2] to save the real-time state of the key. When the key state sends a falling edge, it is the key press action, and when a rising edge occurs, the key is released.

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

void key_read1(void)

{undefined

    //Key recognition

    if(gpio_get_level(KEY_IO)==0){undefined

        key_status[0] = 0;

    }

    else{undefined

       key_status[0] = 1;

    }

    if(key_status[0]!=key_status[1]) {undefined

        key_status[1] = key_status[0];

        if(key_status[1]==0){//Key pressed

            if (led_r_status==1){undefined

                led_r_status = 0;

                gpio_set_level(LED_R_IO, 1);//不亮

            }else{undefined

                led_r_status = 1;

                gpio_set_level(LED_R_IO, 0);//亮

            }

        }else{//Button release

            if (led_g_status==1){undefined

                led_g_status = 0;

                gpio_set_level(LED_G_IO, 1);//off

            }else{undefined

                led_g_status = 1;

                gpio_set_level(LED_G_IO, 0);//亮

            }

        }

    }

}

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

 

hardware connection

The development board has already connected the buttons 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

Simple, on schedule.

button summary

Button debounce is best solved with hardware.

Dedicated key chips, such as CH452, AW9523, etc., may also be used in actual projects.

In addition to this scanning method (simple), there is also a method for interrupt recognition (common and practical), which we will explain later.

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

Click me -> more ESP32 development guide series catalog

Tags: Chapter 9 GPIO Input Key Operation of ESP32

ESP32 development ESP32 Development Guide ESP-IDF

Related: Chapter 9 GPIO Input Key Operation of ESP32