HOW to use Capacitive Sensor with ESP32
Description
In this kit, there is a capacitive touch module which mainly uses a TTP223-BA6 chip. It is a touch detection chip, which provides a touch button, and its function is to replace the traditional button with a variable area button. When we power on, the sensor needs about 0.5 seconds to stabilize.
Do not touch the keys during this time period. At this time, all functions are disabled, and self-calibration is always performed. The calibration period is about 4 seconds. We display the test results in the shell.
Working Principle
When our fingers touch the module, the signal S outputs high levels, the red LED on the module flashes. We can determine if the button is pressed or not by reading high and low levels on the sensor.
Required Components
|
|
|
|
|
---|---|---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Keyestudio DIY Capacitive Module*1 |
3P Dupont Wire*1 |
Micro USB Cable*1 |
Connection Diagram
Test Code
from machine import Pin
import time
touch = Pin(15, Pin.IN, Pin.PULL_UP)
while True:
if touch.value() == 1:
print("You pressed the button!") #Press to print the corresponding information.
else:
print("You loosen the button!")
time.sleep(0.1) #delay0.1s
Code Explanation
When we touch the sensor, the Shell monitor will show“You pressed the button!”, if not,“You loosen the button!”will be shown on the monitor.
Test Result
Connect the wires according to the experimental wiring diagram and power on. Click “Run current script”, the code starts executing, the string will be displayed in the ”Shell“ window. when the button is pressed, the red LED lights up and val is 1. Then the shell shows“You pressed the button!”; if the button is released, the red LED is off and val is 0;“You loosen the button!”will be displayed, as shown below. Press “Ctrl+C”or click
“Stop/Restart backend”to exit the program.