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 x2

Chapter01_05

Circuit

Schematic diagram

Chapter10_01

Hardware connection

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

Chapter10_02

Code

Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.

Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “10.1_Read_Touch_Sensor” and double click “Read_Touch_Sensor.py”.

10.1_Read_Touch_Sensor

../../../_images/Chapter10_10.png

Click “Run current script”, touch the jumper wire with your finger and observe the messages printed in “Shell”.

../../../_images/Chapter10_11.png

Messages printed at “Shell”:

../../../_images/Chapter10_12.png

Reference

Class TouchPad

Before each use of TouchPad module, please add the statement “ from machine import TouchPad “to the top of the python file.

TouchPad(pin): Initialize the TouchPad object and associate it with ESP32 pins.

pin: Pin(4)、Pin(15)、Pin(13)、Pin(12)、Pin(14)、Pin(32)、Pin(33)

TouchPad.read(): Read the capacitance of touchpad. If your fingers touch TouchPad pins, the capacitance decreases; Otherwise, it will not change.

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

Code

Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.

Open “Thonny, click “This computer” -> “D:” -> “Micropython_Codes” -> “10.2_TouchLamp”and double click “TouchLamp.py”.

10.2_TouchLamp

../../../_images/Chapter10_13.png

Click “Run current script” and then touch the jumper wire with your finger. The state of LED will change with each touch and the detection state of the touch sensor will be printed in the “Shell”

../../../_images/Chapter10_14.png ../../../_images/Chapter10_15.png

LED displays as follows:

../../../_images/Chapter10_16.png

The following is the program code:

 1from machine import TouchPad, Pin
 2import time
 3
 4PRESS_VAL=70    #Set a threshold to judge touch
 5RELEASE_VAL=200 #Set a threshold to judge release
 6
 7led=Pin(2,Pin.OUT)
 8tp = TouchPad(Pin(4,Pin.IN,Pin.PULL_UP))
 9isPressed = 0
10
11def reverseGPIO():
12    if led.value():
13        led.value(1)
14        print("Turn off led")
15    else:
16        led.value(0)
17        print("Turn on led")
18
19while True:
20    if tp.read() < PRESS_VAL:
21        if not isPressed:
22            isPressed = 1
23            reverseGPIO()
24            print("Touch detected!")
25            time.sleep_ms(100)
26    if tp.read() > RELEASE_VAL:
27        if isPressed:
28            isPressed = 0
29            print("Touch released!")
30            time.sleep_ms(100)

Import Pin and TouchPad modules.

1from machine import TouchPad, Pin
2import time

The closer the return value of the function read() is to 0, the more obviously the touch action is detected. As this is not a fixed value, a threshold value needs to be defined. When the value of the sensor is less than the threshold, it is considered a valid touch action. Similarly, define a threshold value for the released state, and the value between the sensor value and the threshold is regarded as an invalid interference value.

1PRESS_VAL=70    #Set a threshold to judge touch
2RELEASE_VAL=200 #Set a threshold to judge release

First, decide whether the touch is detected. If yes, print some messages, reverse the state of LED and set the flag bit isProcessed to 1 to avoid repeatedly executing the program after a touch is detected.

1if tp.read() < PRESS_VAL:
2    if not isPressed:
3        isPressed = 1
4        reverseGPIO()
5        print("Touch detected!")
6        time.sleep_ms(100)

And then decide whether the touch key is released. If yes, print some messages, and set isProcessed to 0 to avoid repeatedly executing the program after a touch is released and to prepare for the next touch detector.

1if tp.read() > RELEASE_VAL:
2    if isPressed:
3        isPressed = 0
4        print("Touch released!")
5        time.sleep_ms(100)

Customize a function that reverses the output level of the LED each time it is called.

1def reverseGPIO():
2    if led.value():
3        led.value(1)
4        print("Turn off led")
5    else:
6        led.value(0)
7        print("Turn on led")