ESP32 S3 - Touch Sensor

This tutorial instructs you how to use the ESP32 S3 with a touch sensor (also known as a touch switch or touch button). By the end, you will be able to detect touch events in real time and print the sensor state to the Serial Monitor.

ESP32 S3 - Touch Sensor

What you'll build:

  1. A circuit connecting a touch sensor to the ESP32 S3
  2. Code that reads the touch sensor state using digitalRead()
  3. Serial Monitor output showing LOW/HIGH state changes
  4. A state-change detector that triggers only on the rising edge of a touch event

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Touch Sensor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Touch Sensor

A touch sensor detects the presence of a human finger through capacitive sensing, making it an intuitive alternative to mechanical buttons. When touched, the sensor's SIGNAL pin outputs a HIGH state; when released, it returns to LOW. This predictable behavior makes it straightforward to integrate with the ESP32 S3's digital input pins.

Key Specifications

Touch Sensor Pinout

Touch sensor has 3 pins:

  1. GND pin: connect this pin to GND (0V)
  2. VCC pin: connect this pin to VCC (5V or 3.3V)
  3. SIGNAL pin: is an output pin — LOW when NOT touched, HIGH when touched. This pin needs to be connected to the ESP32 S3's input pin.
Touch Sensor Pinout

How Touch Sensor Works

  • The state of the SIGNAL pin is LOW when the touch sensor is NOT touched
  • The state of the SIGNAL pin is HIGH when the touch sensor is touched

ESP32 S3 - Touch Sensor

We can connect the touch sensor's SIGNAL pin to an ESP32 S3 input pin and use the ESP32 S3 code to read the state of the touch sensor.

Wiring Diagram between Touch Sensor and ESP32 S3

Connect the touch sensor to the ESP32 S3 by linking the sensor's GND to board GND, VCC to 3.3V, and the SIGNAL pin to a GPIO input pin such as GPIO8. Double-check all connections before powering the board to avoid damage.

Safety Notes

Always connect the GND wire first and disconnect it last when working with the circuit. Ensure the VCC voltage matches the touch sensor's rated input voltage — most modules support both 3.3V and 5V, but verify the datasheet before applying power.

The wiring diagram between ESP32 S3 Touch Sensor

This image is created using Fritzing. Click to enlarge image

How To Program Touch Sensor

Programming the ESP32 S3 to read a touch sensor involves two steps: configuring the GPIO pin as an input and then polling its state inside the main loop. The following snippets show each step individually before the full sketch ties them together.

  • Initializes the ESP32 S3 pin to the digital input mode by using pinMode() function. For example, pin GPIO8
pinMode(8, INPUT_PULLUP);
  • Reads the state of the ESP32 S3 pin by using digitalRead() function.
int inputState = digitalRead(8);

Touch Sensor - ESP32 S3 Code

The following code reads the state of the touch sensor and prints it to the Serial Monitor. It continuously polls GPIO8 and outputs a 0 when the sensor is not touched and a 1 when it is touched, letting you observe changes in real time.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-touch-sensor */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-touch-sensor */ #define SENSOR_PIN 8 // ESP32-S3 pin GPIO8 connected to the sensor's SIGNAL pin of touch sensor void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: int state = digitalRead(SENSOR_PIN); // print state to Serial Monitor Serial.println(state); }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Copy the above code and paste it to Arduino IDE.
  3. Compile and upload code to the ESP32 S3 board by clicking Upload button on Arduino IDE.
  4. Touch your finger to the sensor and release.
  5. See the result on Serial Monitor. It looks like the below:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
COM6 Autoscroll Show timestamp 2026-01-15 08:23:10.112 -> 0 2026-01-15 08:23:10.215 -> 0 2026-01-15 08:23:10.318 -> 0 2026-01-15 08:23:10.421 -> 1 2026-01-15 08:23:10.524 -> 1 2026-01-15 08:23:10.627 -> 1 2026-01-15 08:23:10.730 -> 1 2026-01-15 08:23:10.833 -> 0 2026-01-15 08:23:10.936 -> 0 2026-01-15 08:23:11.039 -> 0
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Pro Tip: Try touching the sensor pad lightly versus firmly — the capacitive sensor responds to even a gentle finger contact, making it ideal for touch-sensitive UI designs.

How to detect the state change from LOW to HIGH

The following code detects only the rising-edge transition — the moment the touch sensor changes from not touched to touched — so your program reacts once per touch rather than continuously while the finger remains on the pad.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-touch-sensor */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-touch-sensor */ #define SENSOR_PIN 8 // ESP32-S3 pin GPIO8 connected to the sensor's SIGNAL pin of touch sensor // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: currentState = digitalRead(SENSOR_PIN); if (lastState == LOW && currentState == HIGH) Serial.println("The sensor is touched"); // save the the last state lastState = currentState; }

Applications

Touch sensors are widely used in modern electronics wherever a sleek, buttonless interface is desired. Here are some practical use cases:

  1. Smart Lamp Control: Tap the base of a lamp to toggle it on or off without a physical switch.
  2. Touch-Based Security Panel: Use a touch pad as part of a PIN-entry system for door locks.
  3. Interactive Display: Trigger animations or screen transitions in a kiosk or museum exhibit.
  4. Kitchen Timer: Build a touch-activated countdown timer that avoids mechanical wear in humid environments.
  5. Capacitive Keyboard: Chain multiple touch sensors together to create a custom input device.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenges

Push your skills further by extending the basic touch sensor project in new directions. Each challenge below builds on the foundation you have already established.

  1. Beginner: Modify the code so an LED connected to GPIO2 turns on when the touch sensor is touched and turns off when released.
  2. Intermediate: Use the state-change detection sketch to count the total number of touches and display the count on the Serial Monitor.
  3. Advanced: Combine two touch sensors so that touching both simultaneously triggers a special event, such as activating a buzzer for two seconds.

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!