Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 22: Touch Sensor Module
In this lesson, you’ll learn how to use a touch sensor with an ESP32 Development Board. We’ll see how touching the sensor sends a signal to the ESP32, triggering a response displayed through serial communication. This project is ideal for beginners and provides hands-on experience with digital inputs and serial output on the ESP32 platform. You’ll develop a foundational understanding of how sensors interact with microcontrollers, which is essential for building interactive hardware projects.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
Wiring
Code
Code Analysis
Setting Up the Pin and Serial Communication
The touch sensor is connected to pin 25 of the ESP32, and this pin is configured as an input.
The
Serial.begin(9600);initializes serial communication at a baud rate of 9600 bits per second.
const int sensorPin = 25; void setup() { pinMode(sensorPin, INPUT); // Set the sensor pin as input Serial.begin(9600); // Start the serial communication }
Reading the Sensor and Sending Data to Serial Monitor
The
loop()function continuously checks the state of the touch sensor.digitalRead(sensorPin)reads the digital value (1 or 0) from the sensor pin.If the sensor is touched (value 1), it prints “Touch detected!” to the Serial Monitor.
If not touched (value 0), it prints “No touch detected…”.
The
delay(100);helps in debouncing the sensor, preventing multiple rapid readings.
void loop() { if (digitalRead(sensorPin) == 1) { // If the sensor is touched Serial.println("Touch detected!"); } else { Serial.println("No touch detected..."); } delay(100); // Wait for a short period to avoid rapid reading of the sensor }