9. Chapter Touch Sensor

ESP32-S3 offers up to 14 capacitive touch GPIO, and as you can see from the previous section, mechanical switches are prone to jitter that must be eliminated when used, which is not the case with ESP32-S3’s built-in touch sensor. In addition, on the service life, the touch switch also has advantages that mechanical switch is completely incomparable.

9.1. Project Read Touch Sensor

This project reads the value of the touch sensor and prints it out.

9.1.1. Component List

ESP32-S3 WROOM x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

Jumper M/M x1

Chapter01_05

9.1.3. Circuit

Schematic diagram

Chapter10_01

Hardware connection.

If you need any support, please feel free to contact us via: support@freenove.com

Chapter10_02

9.1.4. Sketch

9.1.4.1. Sketch_TouchRead

../../../_images/Chapter10_03.png

Download the code to ESP32-S3 WROOM, open the serial monitor, and set the baud rate to 115200. Touch jumper with hand. As shown in the following figure,

../../../_images/Chapter10_04.png ../../../_images/Chapter10_05.png

9.1.4.2. Reference

uint16_t touchRead(uint8_t pin);

Read touch sensor value. (values close to 0 mean touch detected)

9.2. Project Touch Lamp

In this project, we will use ESP32-S3’s touch sensor to create a touch switch lamp.

9.2.1. Component List

ESP32-S3 WROOM x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

LED x1

Chapter01_03

Resistor 220Ω x1

Chapter01_04

Jumper M/M x3

Chapter01_05

9.2.2. Circuit

Schematic diagram

Chapter10_06

Hardware connection.

If you need any support, please feel free to contact us via: support@freenove.com

Chapter10_07

9.2.3. Sketch

9.2.3.1. Sketch_TouchLamp

../../../_images/Chapter10_08.png

Download the code to ESP32-S3 WROOM, open the serial monitor, and set the baud rate to 115200. Touch jumper with hand. As shown in the following figure,

fnk0084/codes/_static/imgs/10_Touch_Sensor/Chapter10_09png

With a touch pad, the state of the LED changes with each touch, and the detection state of the touch sensor is printed in the serial monitor.

The following is the program code:

fnk0084/codes/_static/imgs/10_Touch_Sensor/Chapter10_09png
 1/**********************************************************************
 2  Filename    : TouchLamp
 3  Description : Control led by touch button.
 4  Auther      : www.freenove.com
 5  Modification: 2022/10/21
 6**********************************************************************/
 7#define PIN_LED     21
 8#define PRESS_VAL   200000	  //Set a threshold to judge touch
 9#define RELEASE_VAL 60000	    //Set a threshold to judge release
10
11bool isProcessed = false;
12void setup() {
13  Serial.begin(115200);
14  pinMode(PIN_LED, OUTPUT);
15}
16void loop() {
17  if (touchRead(T14) > PRESS_VAL) {
18    if (!isProcessed) {
19      isProcessed = true;
20      Serial.println("Touch detected! ");
21      reverseGPIO(PIN_LED);
22    }
23  }
24  if (touchRead(T14) < RELEASE_VAL) {
25    if (isProcessed) {
26      isProcessed = false;
27      Serial.println("Released! ");
28    }
29  }
30}
31
32void reverseGPIO(int pin) {
33  digitalWrite(pin, !digitalRead(pin));
34}

Due to different operating environments, the return value of the function touchRead() may not be the same or similar. Therefore, with the help of Project 09.1, we can know the return values of touchRead() in different states, and based on these return values, we can set a valid threshold range for the touch function.

For example, when touchRead() returns a value greater than 200000, we consider the touch function to be triggered by a human. Similarly, when the return value of touchRead() is less than 60000, we consider that the touch function has not been triggered by someone. Note that the threshold range here can be modified by users according to their own conditions

1#define PRESS_VAL   200000	  //Set a threshold to judge touch
2#define RELEASE_VAL 60000	    //Set a threshold to judge release

In loop(), first determine whether the touch was detected. If yes, print some messages, flip the state of the LED, and set the flag bit isProcessed to true to avoid repeating the program after the touch was successful.

1if (touchRead(T14) > PRESS_VAL) {
2  if (!isProcessed) {
3    isProcessed = true;
4    Serial.println("Touch detected! ");
5    reverseGPIO(PIN_LED);
6  }
7}

It then determines if the touch key is released, and if so, prints some messages and sets the isProcessed to false to avoid repeating the process after the touch release and to prepare for the next touch probe.

1if (touchRead(T14) < RELEASE_VAL) {
2  if (isProcessed) {
3    isProcessed = false;
4    Serial.println("Released! ");
5  }
6}