Build Your Own Capacitive Touch Sensor to Control Home Appliance using ESP32

Published  October 14, 2020   0
ESP32 Touch Sensor using Capacitive Touch Boards

In many cases,  ESP32 capacitive touch sensors are used instead of push buttons. The advantage is that we don't have to provide force to press a button, and we can activate a key without touching it using touch sensors. Touch sensing technology is becoming popular day by day. And within the last decade or so, it has become difficult to imagine the world without touch-sensitive electronics. Both resistive and capacitive touch methods can be employed to develop a touch sensor. In this article, we will discuss a basic method for creating a touch sensor. Previously, we have also built a capacitive touch pad with Raspberry pi.

Though application-specific touch sensors can be a little complicated, the fundamental principle underlying this technology stays the same, so in this article, we will be focusing on developing our capacitive touch sensor with the help of our favourite ESP32 and a piece of copper-clad board.

In the previous tutorial, we have done Arduino capacitive touch sensor control using TTP223 Touch Sensor, Now in this project, we are building a Touch sensor for ESP32 but the same can be used for Arduino as well. Also, we previously used touch-based input methods using capacitive touch pads with different microcontrollers such as Touch Keypad Interfacing with ATmega32 Microcontroller and Capacitive TouchPad with Raspberry Pi, you can also check them out if interested. We can even do a Capacitive touch sensor with ESP32  Arduino to control appliances via relays. 

 

ESP32 Capacitive Touch Sensor Project Overview

Difficulty Level: Intermediate | Time Required: 3–4 Hours | Cost: $20–30

What You'll Build: A 4-channel ESP32 capacitive touch sensor system capable of controlling home appliances through a touch interface without physical buttons.

Perfect for: Home automation enthusiasts, IoT projects, touch-based control systems

What is an ESP32 Capacitive Touch Sensor and How Does it Work?

Capacitors come in many forms. The most common ones come in the form of a leaded package or a surface mount package but to form a capacitance, we need conductors separated by a dielectric material. Thus, it's easy to create an ESP32 touch sensor design. A good example would be the one that we are going to develop in the following example. 

ESP32 capacitive touch sensor PCB etched board with copper traces

Considering the etched PCB as the conductive material, the sticker is acting as a dielectric material, so now the question remains, how does touching the copper pad cause the capacitance to change in such a way that the touch-sensor controller can detect? A human finger, of course.

This project detects finger contact through two main mechanisms:

  • Dielectric properties of human fingers: Our fingers have different electrical properties from air
  • Capacitance field interaction: Finger proximity changes the sensor's capacitive field

It's important to note that no actual electrical conduction occurs between your finger. The paper sticker acts as insulation, preventing any direct electrical contact while still allowing capacitive detection.

Finger Acting as Dielectric:

It's common knowledge that a capacitor has a constant value which can be realized by the area of the two conducting plates, the distance between the plates, and it's dielectric constant. We cannot change the area of the capacitor just by touching it but we can sure change the dielectric constant of the capacitor because a human finger has a different dielectric constant than the material displaying it. In our case, it's air, we are displacing air with our fingers. If you are asking how? It's because the dielectric constant of the air 1006 at sea level room temperature and the dielectric constant of the finger is much higher around 80 because a human finger consists of mostly water. So, the interaction of the finger with the capacitor's electric field causes an increase in dielectric constant hence the capacitance increases.

Now that we have understood the principal, let's move on to the making of actual PCBs.

Building a Four-Way ESP32 Capacitive Touch Sensor

The capacitive touch sensor used in this project has four channels, and it's easy to make. Below we have mentioned the detailed process to make one.

First, we made the PCB for the sensor with the help of the Eagle PCB design tool, which looks something like the image below.

Eagle PCB Design Tool

With the help of the dimensions and Photoshop, we made the template and finally the sticker for the sensor, which looks something like the image below,

Touch Sensor Template and Sticker

Now, as we are done with the sticker, we move on to making the actual clad board template which we are going to be using for making our PCB with, which looks something like the below image,

Clad Board

Now we can print this file and proceed with the processes of making a homemade PCB. IF you are new, you can check out the article on how to build PCB at home. You can also download the required PDF and Gerber files from link below

Once done, the actual Etched PCB looks like the image below.

Etched PCB

Now it's time to drill some holes, and we connect some wires with the PCB. So that we can connect it with the ESP32 board. Once done, it looks like the image below.

PCB Drilling and Soldering

As we did not put via’s in the PCB, solder got all over the place while soldering, we rectified our mistake by putting a drill hole on the PCB, which you can find in the above download section. Finally, it was time to put the sticker on and make it final. Which looks something like the image below.

Four-channel ESP32 capacitive touch sensor panel with labels

Now we are done with the Touch panel, it's time to move on to making the Control Circuit for the touch panel. 

Materials Required for ESP32 Touch Controlled Circuit

The components required to build the controller section using ESP32 is given below, you should be able to find most of them in the local hobby store.

Materials Required for ESP32 Touch Controlled Circuit

I have also listed the components in the table below with type and quantity required. Since we are interfacing a four-channel touch sensor and controlling four AC loads, we will be using 4 relays to switch the AC load and 4 transistors to build the relay driver circuits.

Sl.NoPartsTypeQuantity
1RelaySwitch4
2BD139Transistor4
3Screw TerminalScrew Terminal 5mmx24
41N4007Diode5
50.1uFCapacitor1
6100uF,25VCapacitor2
7LM7805Voltage Regulator1
81KResistor4
9560RResistor4
10Amber LEDLED4
11Male HeaderConnector4
12Female HeaderConnector30
13Red LEDLED1
14ESP32 Dev Board V1ESP32 Board1
12Clad BoardGeneric 50x 50mm1
13Jumper WiresWires4
14Connecting WiresWires5

Control Circuit for our Capacitive Touch Sensor

The below image shows the complete circuit diagram for our ESP32 based touch sensor.

As you can see, it is a very simple capacitive touch sensor circuit diagram with very minimum components required. The PCB layout has been optimized for minimal interference between touch channels. The touch sensor circuit diagram is shown below.

ESP32 based Touch Sensor Circuit Diagram

As it’s a simple touch sensor circuit, it can be useful in places where you want to interact with a device via touch. For example, instead of using a typical board-mounted switch, you can turn on/off your appliances with a touch.

In the schematic, A DC barrel jack is used as an input where we provide the necessary power required to power the circuit. From there, we have our 7805 Voltage regulator, which converts the unregulated DC input to a constant 5V DC through which we are providing the power to the ESP32 module.

Next, in the schematic, we have our touch connectors on pin 25, 26, 27, 28, where we are going to connect the touchpad.

Next, we have our relays which are switched via a BD139 transistor, the diode D2, D3, D4, D5 is there to protects the circuit from any transient’s voltage which is generated when the relay toggles, the diodes in this configuration are known as the fly-back diode/freewheeling diode. The 560R resistors at the base of each transistor are used to limit the flow of current through the base.

PCB Design for the Capacitive Touch Sensor Circuit

The ESP32 capacitive touch PCB sensor circuit was designed for a single-sided board. We have used Eagle to design my PCB, but you can use any Design software of your choice. The 2D image of our Capacitive touch pad PCB design is shown below.

Capacitive Touch Sensor PCB Board

A sufficient trace diameter was used to make the power tracks, which is used to flow the current through the circuit board. We put the screw terminal at the top because it's much easier to connect your load that way, and the power connector, which is a DC barrel jack was placed at the side, which also gives easy access. The complete Design file for Eagle along with the Gerber can be downloaded from the link below.

Now that our Design is ready, its time to etch and solder the board. After the etching, drilling, and soldering process is finished, the board looks like the image shown below,

Capacitive Touch Sensor with ESP32

 

ESP32 vs Arduino Touch Sensor Comparison

FeatureESP32Arduino (Uno/Nano)
Built-in Capacitive Touch SupportComes with 10 dedicated touch pins—ready to go!Need an external chip like the TTP223 to add touch.
Number of Touch InputsUp to 10 (labeled T0–T9), perfect for multi-touch projects.Just 1 touch input per external module—pretty limited.
Library/Code ComplexitySuper easy with the touchRead() function. Needs extra libraries or digitalRead with a touch module. 
Sensitivity ControlYou can tweak sensitivity in software for precise control.Fixed sensitivity—depends on the external module you pick.
Power EfficiencySupports deep sleep mode with touch wake-up, great for battery-powered projects.No touch wake-up; power management is less flexible.
PCB IntegrationTouch pads connect directly to ESP32 pins, keeping your board compact.Requires space for external touch modules, which can clutter your PCB.
C Cost EfficiencyPacks tons of features for a similar price.Can get pricier when you add touch modules to match ESP32’s capabilities.
Response TimeLightning-fast since it’s all built-in.Slight lag due to external chip processing not a dealbreaker, but noticeable.
Use Case SuitabilityMulti-touch, compact, or power-efficient projects like wearables or IoT devices.Simple projects with just one or two touch inputs, like a basic button.

For more advanced touch sensor applications, you can also explore our guide on capacitive touch sensor interfacing with microcontrollers or learn about PCB design best practices for touch sensors.

Arduino Code for ESP32 Based Capacitive Touch Sensor

For this project, we will program the ESP32 with a custom code which we will describe shortly. The code is very simple and easy to use,

We start by defining all the required pins, in our case, we define the pins for our touch sensors and relays.

#define Relay_PIN_1 15
#define Relay_PIN_2 2
#define Relay_PIN_3 4
#define Relay_PIN_4 16
#define TOUCH_SENSOR_PIN_1 13
#define TOUCH_SENSOR_PIN_2 12
#define TOUCH_SENSOR_PIN_3 14
#define TOUCH_SENSOR_PIN_4 27

Next, in the setup section, we start by initializing the UART for debugging, next we have introduced a delay of 1S which gives us a little time for opening a Serial Monitor window. Next, we use the Arduinos pinMode function to make the Relay pins as output, which marks the end of the Setup() section.

void setup() {
  Serial.begin(115200);
  delay(1000);
  pinMode(Relay_PIN_1, OUTPUT);
  pinMode(Relay_PIN_2, OUTPUT);
  pinMode(Relay_PIN_3, OUTPUT);
  pinMode(Relay_PIN_4, OUTPUT);
}

We start our loop section with an if statement, the builtin function touchRead(pin_no) is used to determine if a pin was touched or not. The touchRead(pin_no) function returns an integer value ranges (0 - 100), the value stays near 100 all the time, but if we touch the selected pin, the value drops to near zero, and with the help of the changing value, we can determine if the particular pin was touched by a finger or not.

In the if statement, we are checking for any change in the integer values, and if the value reaches below 28, we can be sure that we have acknowledged a touch. Once the if statement becomes true, we wait for 50ms and check the parameter again, this will help us to determine if the sensor value was trigger falsely, after that, we invert the status of the pin by using the digitalWrite(Relay_PIN_1, !digitalRead(Relay_PIN_1))  method, and the rest of the code stays the same.

if ( touchRead(TOUCH_SENSOR_PIN_1) < 28) {
    if ( touchRead(TOUCH_SENSOR_PIN_1) < 28) {
      Serial.println("Sensor one is touched");
      digitalWrite(Relay_PIN_1, !digitalRead(Relay_PIN_1));
    }
  }
  else if (touchRead(TOUCH_SENSOR_PIN_2) < 28) {
    if (touchRead(TOUCH_SENSOR_PIN_2) < 28) {
      Serial.println("Sensor Two is touched");
      digitalWrite(Relay_PIN_2, !digitalRead(Relay_PIN_2));
    }
  }
  else if ( touchRead(TOUCH_SENSOR_PIN_3) < 28) {
    if (touchRead(TOUCH_SENSOR_PIN_3) < 28) {
      Serial.println("Sensor Three is touched");
      digitalWrite(Relay_PIN_3, !digitalRead(Relay_PIN_3));
    }
  }
  else if (touchRead(TOUCH_SENSOR_PIN_4) < 28) {
    if (touchRead(TOUCH_SENSOR_PIN_4) < 28) {
      Serial.println("Sensor Four is touched");
      digitalWrite(Relay_PIN_4, !digitalRead(Relay_PIN_4));
    }
  }

Finally, we end our code with another 200 ms of blocking delay. 

Testing the ESP32 Based Touch Sensor Circuit 

ESP32 Based Touch Sensor Circuit

As this is a very simple project, the test set is very simple, as you can see, I have connected 4 LEDs with resistors, which are acting as loads, as it's connected with the relay, you can easily connect any load up to 3Amps.

 

ESP32 Touch Sensor Calibration and Sensitivity Tips

 ¤ Adjust the threshold value (currently set to 28) based on your specific PCB design

¤ Use proper grounding techniques to reduce false triggers

¤ Consider environmental factors like humidity that may affect sensitivity

¤ Implement debouncing delays to prevent multiple rapid triggers

 

Further Enhancements

Although the PCB design is simple, there is room for improvement.

On the bottom side of the actual PCB, multiple resistors were manually added to connect four indicator LEDs.

The overall size of the PCB can be reduced if needed for compact applications.

If you have any questions, you can leave them in the comment section below or use our forums to post other technical questions.

 

Project Summary and GitHub Repository 

ESP32 touch sensor  ESP32 capacitive touch sensors

 

Frequently Asked Questions

⇥ How can the ESP32 touch sensor's sensitivity be increased?
By changing the threshold values in your code, you can increase the ESP32 touch sensor's sensitivity. Sensitivity can also be increased by employing larger touch pads, appropriate grounding, and noise-source shielding.

⇥ How many ESP32 capacitive touch pins are accessible to use in a project?
Ten specific capacitive touch pins (T0–T9) on the ESP32 allow touch input without the need for additional hardware. It is perfect for small, interactive designs because all 10 can be used for multi-touch applications.

⇥ How can I resolve the inconsistent sensitivity of my ESP32 touch sensor?
Noise, incorrect grounding, or miscalibrated thresholds can all contribute to inconsistent touch sensor sensitivity on the ESP32 capacitive touch pins. To stabilize performance, make sure the PCB is designed correctly, reduce interference, and adjust the sensitivity in the code.

Projects using Touch Sensor to Control Home Appliances

Explore our exciting range of projects utilizing the Touch Sensor! We've crafted numerous innovative applications with this versatile component. Check out the links below for detailed insights into these projects.

 Design and Build a Touch Capacitive Based PCB Light Panel to Control NeoPixel LED Strip

Design and Build a Touch Capacitive Based PCB Light Panel to Control NeoPixel LED Strip

In this tutorial we are going to make a Touch Capacitive PCB using an ATMega328P IC to control neo pixel led strip. We will include some features such as music reactive mode, random animation mode, and RGB controlling mode on our PCB.

Control Home Lights with Touch using TTP223 Touch Sensor and Arduino UNO

Control Home Lights with Touch using TTP223 Touch Sensor and Arduino UNO

Touch sensor is an important and widely used input device to interface with a microcontroller and it has made inputting data simpler. There are individual places where the touch sensor can be used, whether it can be a mobile phone or an LCD monitor switch. 

Controlling Light using Touch Sensor and 8051 Microcontroller

Controlling Light using Touch Sensor and 8051 Microcontroller

This ESP32 project is an exciting and interactive way to control LEDs using hand gestures. We will use an ESP32 board, Python, MediaPipe, and OpenCV to create a system that can detect specific hand gestures and translate them into actions that control LEDs. 

Controlling Light using Touch Sensor and 8051 Microcontroller

Controlling Light using Touch Sensor and 8051 Microcontroller

This ESP32 project is an exciting and interactive way to control LEDs using hand gestures. We will use an ESP32 board, Python, MediaPipe, and OpenCV to create a system that can detect specific hand gestures and translate them into actions that control LEDs. 

Complete Project Code

#define Relay_PIN_1 15
#define Relay_PIN_2 2
#define Relay_PIN_3 4
#define Relay_PIN_4 16
//All relay pin definitions
#define TOUCH_SENSOR_PIN_1 13
#define TOUCH_SENSOR_PIN_2 12
#define TOUCH_SENSOR_PIN_3 14
#define TOUCH_SENSOR_PIN_4 27
// all touch sensor pin definitions
void setup() {
  Serial.begin(115200); //Begin the UART
  delay(1000); // give me time to bring up serial monitor
  pinMode(Relay_PIN_1, OUTPUT);
  pinMode(Relay_PIN_2, OUTPUT);
  pinMode(Relay_PIN_3, OUTPUT);
  pinMode(Relay_PIN_4, OUTPUT);
  // all the relay pins set as output
}
void loop() {
  /*
     Uncomment for debug
      Serial.println(" ");
      Serial.println("#############################");
      Serial.print("Button1: ");
      Serial.println(touchRead(TOUCH_SENSOR_PIN_1));  // get value of Touch 0 pin = GPIO 4
      Serial.print("Button2: ");
      Serial.println(touchRead(TOUCH_SENSOR_PIN_2));
      Serial.print("Button3: ");
      Serial.println(touchRead(TOUCH_SENSOR_PIN_3));
      Serial.print("Button4: ");
      Serial.println(touchRead(TOUCH_SENSOR_PIN_4));
      Serial.println("#############################");
      Serial.println(" ");
  */
  if ( touchRead(TOUCH_SENSOR_PIN_1) < 28) { // check if the value reaches belo 28
    delay(50);
    if ( touchRead(TOUCH_SENSOR_PIN_1) < 28) { //again check if the value reaches below 28
      Serial.println("Sensor one is touched");
      digitalWrite(Relay_PIN_1, !digitalRead(Relay_PIN_1));
    }
  }
  else if (touchRead(TOUCH_SENSOR_PIN_2) < 28) {
    delay(50);
    if (touchRead(TOUCH_SENSOR_PIN_2) < 28) {
      Serial.println("Sensor Two is touched");
      digitalWrite(Relay_PIN_2, !digitalRead(Relay_PIN_2));
    }
  }
  else if ( touchRead(TOUCH_SENSOR_PIN_3) < 28) {
    delay(50);
    if (touchRead(TOUCH_SENSOR_PIN_3) < 28) {
      Serial.println("Sensor Three is touched");
      digitalWrite(Relay_PIN_3, !digitalRead(Relay_PIN_3));
    }
  }
  else if (touchRead(TOUCH_SENSOR_PIN_4) < 28) {
    delay(50); // works as a debounce delay.
    if (touchRead(TOUCH_SENSOR_PIN_4) < 28) {
      Serial.println("Sensor Four is touched");
      digitalWrite(Relay_PIN_4, !digitalRead(Relay_PIN_4));
    }
  }
  delay(200);
}
Video

Have any question related to this Article?

Add New Comment

Login to Comment Sign in with Google Log in with Facebook Sign in with GitHub