ESP32 S3 - Touch Sensor - Relay

This tutorial shows you how to use the ESP32 S3 to control a relay based on the state of a touch sensor, letting you activate high-voltage devices with a simple finger touch. By the end, you will have a solid foundation for building touch-activated switching and automation projects with your ESP32 S3.

What you'll build:

  1. A circuit connecting a touch sensor and relay module to the ESP32 S3
  2. A sketch that activates the relay while the touch sensor is touched and deactivates it on release
  3. A system that can switch high-voltage loads like lamps, pumps, or motors via the relay
  4. A foundation for extending into home automation and touch-activated smart switching projects

We also have another tutorial: ESP32 S3 - Touch Sensor Toggles Relay, which toggles the relay's state between ON and OFF each time the touch sensor is touched.

We can extend this tutorial to use the touch sensor to control an LED strip, siren, light bulb, or motor by connecting them to the relay.

Overview of Relay and Touch Sensor

The relay and touch sensor are a powerful combination for building intuitive, buttonless control systems with the ESP32 S3. A touch sensor detects the presence of a human finger through capacitive sensing, providing a HIGH digital signal that the ESP32 S3 reads in real time, while a relay safely switches high-voltage or high-current loads that would otherwise damage the board's GPIO pins. Together they allow you to create sleek, touch-activated automation where a simple finger contact determines whether a device is active.

Key Specifications

We have dedicated tutorials covering each component in depth. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to the ESP32 S3, and Arduino code. Learn more about them at the following links:

Wiring Diagram

Connect the touch sensor and relay to your ESP32 S3 by following the diagram below. Double-check all connections before applying power, especially on the high-voltage side of the relay.

The wiring diagram between ESP32 S3 Touch Sensor Relay

This image is created using Fritzing. Click to enlarge image

For a project that lasts, screw terminals hold the wires better than jumper wires. Simply plug the ESP32 S3 into a Screw Terminal Block for ESP32 S3. The pins do not change. Just connect each wire to the matching screw terminal:

The wiring diagram between ESP32 S3 Touch Sensor Relay Screw Terminal

This image is created using Fritzing. Click to enlarge image

Here is the same circuit with a GPIO Breakout Board for ESP32 S3, which breaks out every GPIO to pin headers:

The wiring diagram between ESP32 S3 Touch Sensor Relay GPIO Breakout Board

This image is created using Fritzing. Click to enlarge image

Safety Notes

Always verify that the relay's COM and NO (or NC) terminals are securely fastened and that no bare wires are exposed before powering the circuit. Keep the low-voltage control wiring to the ESP32 S3 physically separated from any high-voltage load wiring to reduce the risk of accidental contact.

Component Pin ESP32 S3 Pin
Touch Sensor GND GND
Touch Sensor VCC 3.3V
Touch Sensor SIGNAL GPIO17
Relay DC- GND
Relay DC+ 5V
Relay IN GPIO13

ESP32 S3 Code

The following code reads the state of a touch sensor connected to the ESP32 S3 and controls a relay accordingly — activating the relay while the sensor is touched and deactivating it when released. All logic is handled with standard Arduino digitalRead() and digitalWrite() calls, so no external libraries are required.

/* * 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-relay */ /* * 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-relay */ #define TOUCH_SENSOR_PIN 17 // ESP32-S3 pin GPIO17 connected to touch sensor's pin #define RELAY_PIN 13 // ESP32-S3 pin GPIO13 connected to relay's pin void setup() { Serial.begin(9600); // initialize serial pinMode(TOUCH_SENSOR_PIN, INPUT); // set ESP32-S3 pin to input mode pinMode(RELAY_PIN, OUTPUT); // set ESP32-S3 pin to output mode } void loop() { int touchState = digitalRead(TOUCH_SENSOR_PIN); // read new state if (touchState == HIGH) { Serial.println("The sensor is being touched"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (touchState == LOW) { Serial.println("The sensor is untouched"); digitalWrite(RELAY_PIN, LOW); // turn off } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components following the wiring diagram shown above.
  3. Connect the ESP32 S3 board to your PC using a USB Type-C cable.
  4. Open Arduino IDE on your PC.
  5. Select the correct ESP32 S3 board and COM port from the Tools menu.
  6. Copy the above code and paste it into Arduino IDE.
  7. Click the Upload button to compile and upload the code to the ESP32 S3.
Arduino IDE Upload Code
  1. Touch and hold the touch sensor for several seconds.
  2. Observe the relay activating (you will hear a click) while your finger is on the sensor.
  3. Remove your finger and observe the relay deactivating.
  4. Pro Tip: Open the Serial Monitor at 115200 baud to print relay state messages in real time — this makes it much easier to debug unexpected behavior.

Serial Monitor Output

The following Serial Monitor output shows the ESP32 S3 responding to touch events and releases in real time. Each line is timestamped so you can verify the timing between touch and release events.

∞
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
[2026-06-16 10:23:41] The sensor is being touched - Relay: ON [2026-06-16 10:23:44] The sensor is untouched - Relay: OFF [2026-06-16 10:23:47] The sensor is being touched - Relay: ON [2026-06-16 10:23:50] The sensor is untouched - Relay: OFF
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application and Project Ideas

Once you master touch-sensor-controlled relay switching with the ESP32 S3, you can extend this pattern into a wide range of practical automation and smart-home projects.

  1. Touch-activated lamp: Tap a sensor pad to switch a table lamp or LED strip on while your finger remains in contact, and have it turn off automatically on release.
  2. Touch-based door lock: Use a capacitive touch pad to momentarily energise a solenoid lock relay for a sleek, buttonless entry system.
  3. Kitchen appliance control: Integrate a touch sensor into a countertop surface so a dishwasher or blender can be activated without a mechanical switch.
  4. Emergency stop panel: Wire a large touch pad to a relay that cuts power to a machine, providing an easy-to-activate safety shutoff.
  5. Fan or heater override: Allow manual relay activation via touch to override an automated temperature-control loop without pressing a physical button.
  6. Interactive exhibit trigger: Activate lighting effects, audio, or motors in museum or retail displays by simply having a visitor touch a sensor surface.

Video Tutorial

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

Challenge Yourself

Now that you can control a relay with a touch sensor on the ESP32 S3, try these progressively challenging extensions to deepen your understanding and build more capable projects.

  1. Beginner: Modify the code so the relay stays ON for exactly five seconds after the touch sensor is touched, regardless of how long the finger remains on the pad.
  2. Beginner: Add a second LED indicator that lights up whenever the relay is active so you have a visual confirmation alongside the audible relay click.
  3. Intermediate: Implement a toggle mode so the first touch turns the relay ON and the second touch turns it OFF, using state-change detection for reliable single-event triggering.
  4. Intermediate: Add a second touch sensor that activates a second relay independently, giving you two-channel touch control from a single ESP32 S3.
  5. Advanced: Build a timed relay controller where a short touch activates the relay for 10 seconds and a long touch (over 2 seconds) keeps it ON until touched again.
  6. Advanced: Extend the project with WiFi so the relay can be controlled both by the physical touch sensor and remotely via a web page served by the ESP32 S3.

※ 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!