Chapter 10 Touch Sensor

ESP32 offers up to 10 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’s built-in touch sensor. In addition, on the service life, the touch switch also has advantages that mechanical switch is completely incomparable.

Project 10.1 Read Touch Sensor

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

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

Jumper M/M x1

Chapter01_05

Circuit

Schematic diagram

Chapter10_01

Hardware connection

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

Chapter10_02

Sketch

Sketch_10.1_TouchRead

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

Download the code to ESP32-WROVER, open the serial monitor, and set the baud rate to 115200. As shown in the following figure.

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

Touched by hands, the value of the touch sensor will change. The closer the value is to zero, the more obviously the touch action will be detected. The value detected by the sensor may be different in different environments or when different people touch it. The code is very simple, just look at Reference.

Reference

uint16_t touchRead(uint8_t pin);

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

Project 10.2 Touch Lamp

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

Component List

ESP32-WROVER 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

Circuit

Schematic diagram

Chapter10_06

Hardware connection

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

Chapter10_07

Sketch

Sketch_10.2_TouchLamp

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

Download the code to ESP32-WROVER, open the serial monitor, and set the baud rate to 115200. As shown in the following figure,

../../../_images/Chapter10_09.png

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:

 1/**********************************************************************
 2  Filename    : TouchLamp
 3  Description : Control led by touch button.
 4  Auther      : www.freenove.com
 5  Modification: 2024/06/18
 6**********************************************************************/
 7#define PIN_LED   2
 8#define PRESS_VAL 14	  //Set a threshold to judge touch
 9#define RELEASE_VAL 25	//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(T0) < PRESS_VAL) {
18    if (!isProcessed) {
19      isProcessed = true;
20      Serial.println("Touch detected! ");
21      reverseGPIO(PIN_LED);
22    }
23  }
24
25  if (touchRead(T0) > RELEASE_VAL) {
26    if (isProcessed) {
27      isProcessed = false;
28      Serial.println("Released! ");
29    }
30  }
31}
32
33void reverseGPIO(int pin) {
34  digitalWrite(pin, !digitalRead(pin));
35}

The closer the return value of the function touchRead() is to 0, the more obviously the touch is detected. This is not a fixed value, so you need to define a threshold that is considered valid (when the value of the sensor is less than this threshold). Similarly, a threshold value is to be defined in the release state, and a value in between is considered an invalid disturbance value.

1#define PRESS_VAL 14	  //Set a threshold to judge touch
2#define RELEASE_VAL 25	//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(T0) < 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(T0) > RELEASE_VAL) {
2  if (isProcessed) {
3    isProcessed = false;
4    Serial.println("Released! ");
5  }
6}