ESP32 S3 UNO - Touch Sensor

This ESP32 S3 Uno touch sensor tutorial shows you how to connect a touch button to the ESP32 S3 Uno form board and read it with Arduino code. A touch switch works like a normal push button, but you only tap a smooth pad. That is why touch lamps and many modern devices use it for a cleaner look.

In this tutorial, you will:

  1. Learn the pinout of a capacitive touch sensor module
  2. Wire the touch sensor to the ESP32 S3 Uno
  3. Read the touch button state with digitalRead()
  4. Detect the moment the sensor is touched or released
  5. Print the results on the Arduino IDE Serial Monitor
ESP32 S3 Uno touch sensor

Hardware Preparation

1×ESP32 S3 Uno-form Board
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×Jumper Wires

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

Before you write any code, it helps to know what the module does. A touch sensor module has a small chip that feels the change when your finger comes close to the pad. It then turns that change into a clean digital signal that your board can read.

Pinout

The touch sensor module has only three pins, so wiring is quick.

Pin What It Does
GND Connect to GND (0V)
VCC Connect to 3.3V or 5V. With the ESP32 S3 Uno, use 3.3V
SIGNAL Gives LOW when untouched and HIGH when touched. Connect it to an input pin of the ESP32 S3 Uno
Touch Sensor Pinout

How It Works

The logic is very simple. When nothing touches the pad, the SIGNAL pin stays LOW. When you put your finger on the pad, the SIGNAL pin goes HIGH, and it stays HIGH until you lift your finger.

ESP32 S3 Uno - Touch Sensor

You connect the SIGNAL pin of the sensor to one input pin of the ESP32 S3 Uno. Then your code reads that pin. A HIGH value means someone is touching the sensor, and a LOW value means nobody is.

ESP32 S3 Uno Pinout

The image below shows the pinout of the ESP32 S3 Uno form board. Use it to find the Uno header pin D7 and its GPIO number before you connect the wires.

ESP32 S3 Uno pinout diagram

Wiring Diagram

Only three wires are needed. The SIGNAL pin goes to D7, which is GPIO14 on the ESP32 S3 Uno.

The wiring diagram between ESP32 S3 Uno Touch Sensor

This image is created using Fritzing. Click to enlarge image

Touch Sensor Pin ESP32 S3 Uno Pin
VCC 3.3V
GND GND
SIGNAL D7 (GPIO14)

WARNING

The ESP32 S3 Uno pins work at 3.3V and are NOT 5V tolerant. If you power the touch sensor from 5V, its SIGNAL pin also gives 5V when touched, and this can damage GPIO14. Power the sensor from the 3.3V pin. If you must use 5V, put a voltage divider or a level shifter on the SIGNAL line.

How To Program For Touch Sensor

Reading a touch sensor takes only two steps. You set the pin as an input once, then you read it as often as you need.

Step 1: Set the pin as an input

Call pinMode() in setup(). In this example the sensor is on D7, so the code uses GPIO14.

pinMode(14, INPUT);

Step 2: Read the pin state

Use the digitalRead() function to get the state of the pin. It returns HIGH or LOW.

int inputState = digitalRead(14);

※ NOTE THAT:

You will usually use the touch sensor in one of two ways:

  1. Act on the level. Do one thing while the input is HIGH and the opposite thing while it is LOW.
  2. Act on the change. Do something only at the moment the input goes from LOW to HIGH (or from HIGH to LOW).

Choose the way that fits your project. For example, to control an LED: if the LED should be ON only while your finger is on the pad, use the first way. If each tap should switch the LED ON or OFF, use the second way.

Touch Sensor - ESP32 S3 Uno Code

The two sketches below cover both ways of using the sensor. The first one shows the raw state, and the second one reports only the touch and release events.

Reads the value from the touch sensor and print to the Serial Monitor

This sketch sets GPIO14 as an input and reads it again and again inside loop(). Each reading, 0 or 1, is printed on its own line, so you can watch the value change as you touch the pad.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-touch-sensor */ #define SENSOR_PIN 14 // The ESP32 S3 Uno pin (D7) connected to the touch sensor's SIGNAL pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32 S3 Uno pin as an input 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 the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into Arduino IDE.
  6. Upload the code by clicking the Upload button.
  7. Open the Serial Monitor and set the baud rate to 9600.
  8. Test it by putting your finger on the sensor and then taking it away.
  9. Check the output on the Serial Monitor.
  10. Tip: Most touch modules have a small LED that lights up when touched. If the LED reacts but the Serial Monitor does not, check the SIGNAL wire.
∞
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
0 0 1 1 1 1 1 0 0 0 0
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Detects the sensor is touched or released

This sketch remembers the last reading and compares it with the new one. When the state goes from LOW to HIGH, it prints that the sensor is touched. When it goes from HIGH to LOW, it prints that the sensor is released, so you get one message per event instead of a stream of numbers.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-touch-sensor */ #define SENSOR_PIN 14 // The ESP32 S3 Uno pin (D7) connected to the touch sensor's SIGNAL pin int touch_state; // the current reading from the input pin int prev_touch_state = LOW; // the previous state from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32 S3 Uno pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: touch_state = digitalRead(SENSOR_PIN); if(prev_touch_state == LOW && touch_state == HIGH) Serial.println("The sensor is touched"); else if(prev_touch_state == HIGH && touch_state == LOW) Serial.println("The sensor is is released"); // save the the last state prev_touch_state = touch_state; }

Detailed Instructions

  1. Copy the code above and open it in Arduino IDE.
  2. Upload the code to the ESP32 S3 Uno with the Upload button.
  3. Touch the sensor and keep your finger on it.
  4. Check the Serial Monitor.
∞
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
The sensor is touched
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Lift your finger off the sensor.
  2. Check the Serial Monitor again.
∞
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
The sensor is touched The sensor is is released
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: Use this event style when one tap should start one action, such as toggling an LED or playing a sound.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

The video demo plays a song on a buzzer when you touch one of two touch sensors, and blinks two LEDs in time with the notes. It uses the code below:

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-touch-sensor */ // --- Hardware Pin Definitions --- #define BUZZER_PIN 21 // ESP32 S3 Uno pin D8 #define TOUCH_1_PIN 18 // Happy Birthday trigger (ESP32 S3 Uno pin D2) #define TOUCH_2_PIN 17 // Jingle Bells trigger (ESP32 S3 Uno pin D3) #define LED_A_PIN 19 // First indicator LED (Active-Low, ESP32 S3 Uno pin D4) #define LED_B_PIN 20 // Second indicator LED (Active-Low, ESP32 S3 Uno pin D5) // --- Musical Note Frequencies (Hz) --- #define NOTE_C4 262 #define NOTE_D4 294 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_G4 392 #define NOTE_A4 440 #define NOTE_A5 880 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_D5 587 #define NOTE_E5 659 // --- Melody 1: Happy Birthday --- const int happyBirthdayMelody[] = { NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_B4, NOTE_B4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4 }; const int happyBirthdayDurations[] = { 8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 4, 4, 8, 8, 4, 4, 4, 2 }; const int happyBirthdayLength = sizeof(happyBirthdayMelody) / sizeof(happyBirthdayMelody[0]); // --- Melody 2: Jingle Bells --- const int jingleBellsMelody[] = { NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4 }; const int jingleBellsDurations[] = { 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4, 4, 4, 8, 8, 4, 4, 4, 4, 2 }; const int jingleBellsLength = sizeof(jingleBellsMelody) / sizeof(jingleBellsMelody[0]); // --- Function Prototypes --- void playSong(const int melody[], const int durations[], int songLength); void updateLeds(int noteIndex, bool isQuietNote); void turnOffBothLeds(); /** * @brief Initializes peripheral pins and system communication. */ void setup() { Serial.begin(115200); pinMode(TOUCH_1_PIN, INPUT); pinMode(TOUCH_2_PIN, INPUT); pinMode(BUZZER_PIN, OUTPUT); pinMode(LED_A_PIN, OUTPUT); pinMode(LED_B_PIN, OUTPUT); // For Active-Low LEDs, setting pins HIGH turns them OFF on startup turnOffBothLeds(); Serial.println(F("Active-Low LED Music Box Jukebox Ready.")); } /** * @brief Main execution loop checking for touch sensor triggers. */ void loop() { if (digitalRead(TOUCH_1_PIN) == HIGH) { Serial.println(F("Touch 1 detected: Playing Happy Birthday...")); playSong(happyBirthdayMelody, happyBirthdayDurations, happyBirthdayLength); } else if (digitalRead(TOUCH_2_PIN) == HIGH) { Serial.println(F("Touch 2 detected: Playing Jingle Bells...")); playSong(jingleBellsMelody, jingleBellsDurations, jingleBellsLength); } } /** * @brief Iterates through the provided melody arrays to play the full song with LED sync. * @param melody Array containing note frequencies. * @param durations Array containing corresponding note lengths. * @param songLength Total number of notes in the song. */ void playSong(const int melody[], const int durations[], int songLength) { for (int thisNote = 0; thisNote < songLength; thisNote++) { bool isQuietNote = (melody[thisNote] == 0); int noteDuration = 1000 / durations[thisNote]; // Trigger Active-Low LED effect updateLeds(thisNote, isQuietNote); tone(BUZZER_PIN, melody[thisNote], noteDuration); int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(BUZZER_PIN); turnOffBothLeds(); // Drive both pins HIGH to turn off the LEDs } Serial.println(F("Song finished. LEDs reset to standby (HIGH).")); } /** * @brief Logic switch designed for Active-Low configurations (LOW = ON, HIGH = OFF). * @param noteIndex Current position in the melody array. * @param isQuietNote Evaluation flag determining if the note produces no sound. */ void updateLeds(int noteIndex, bool isQuietNote) { if (isQuietNote) { turnOffBothLeds(); return; } // Alternating Active-Low pattern if (noteIndex % 2 == 0) { digitalWrite(LED_A_PIN, LOW); // LED A turns ON digitalWrite(LED_B_PIN, HIGH); // LED B turns OFF } else { digitalWrite(LED_A_PIN, HIGH); // LED A turns OFF digitalWrite(LED_B_PIN, LOW); // LED B turns ON } } /** * @brief Forces both LED control pins to HIGH, turning them OFF in Active-Low logic. */ void turnOffBothLeds() { digitalWrite(LED_A_PIN, HIGH); digitalWrite(LED_B_PIN, HIGH); }

FAQ

Should I power the touch sensor with 3.3V or 5V on the ESP32 S3 Uno?

Use 3.3V. The touch module works fine at 3.3V, and its SIGNAL output then matches the 3.3V logic of the ESP32 S3 Uno. At 5V, the SIGNAL pin outputs 5V when touched, which is not safe for the ESP32 S3 Uno pins.

Which pin should I use for the touch sensor?

Any normal digital pin works, because the sensor gives a simple HIGH or LOW. This tutorial uses D7 (GPIO14). In code, always write the GPIO number, not the Uno label. Avoid D0 and D1 (USB serial) and the boot strapping pins D6 (GPIO3) and D9 (GPIO46).

Can I use the ESP32 S3 Uno built-in touch pins instead of a touch module?

Yes, the ESP32 S3 Uno chip has its own capacitive touch inputs that you read with touchRead(). But you then need to pick a threshold and handle noise yourself. The touch module is easier for beginners, because it already gives a clean HIGH or LOW signal.

Is the code different from the Arduino UNO R4 version?

The logic is exactly the same. Only the pin number changes: D7 is pin 7 on the UNO R4, but on the ESP32 S3 Uno you write 14 (GPIO14) in code. In the demo sketch, tone() also works on the ESP32 S3 Uno with the esp32 core.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs: GPIO15 and GPIO16 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to use them and write the GPIO number in code. For extra touch sensors, GPIO35 to GPIO42 or GPIO15/16 are good picks. Do not use GPIO47/GPIO48 for touch sensors, because they are output only. Also note two warnings. First, GPIO47/GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, try another USB cable, or hold BOOT while you press RESET
Upload fails or board does not start Wrong board selected Choose ESP32S3 Dev Module and the right COM port
Serial Monitor shows strange characters Wrong baud rate Set the Serial Monitor to 9600 for the first two sketches and 115200 for the demo
Value always 0 SIGNAL wire on the wrong pin or loose Connect SIGNAL to D7 (GPIO14) and check that the code uses 14
Value always 0 and the module LED is off Sensor has no power Check VCC to 3.3V and GND to GND
Value changes randomly with no touch SIGNAL pin not connected so the input floats Push the jumper wires in firmly and check every wire
Sensor triggers by itself Pad is near metal or a noisy power line Move the sensor away from metal and power it from the 3.3V pin
Board resets or a pin gets hot Sensor powered from 5V so SIGNAL outputs 5V Power the sensor from 3.3V or add a voltage divider on SIGNAL

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!