Introduction: ESP32 With Capacitive Touch Button

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects.

Today, I'm going to talk about Touch Capacitive, a very interesting feature of ESP32. We use the example of a faucet, which opens and closes according to the touch of our hand. We are showing an imported example, but this technology already exists in Brazil, including other types of devices.

Our goal in this project today is to create a program to use capacitive touch sensors, which ESP32 has native support. We already have support for Arduino IDE today. With this feature, we can replace a button, for example. There is just one wire attached to the pin, and with this, our finger can activate it.

Step 1: Introduction

Capacitive sensors are present in much of our day-to-day equipment, such as your smartphone, your tablet, your microwave, some TVs, cars, and many other products. It is safe to say that virtually all appliances have some sort of touch function.

The touch function also favors the design, since it takes away the need of implantation of a mechanical button, which can eventually break. It is up to the designer to decide whether to use this feature or not.

Step 2: Operation

The capacitive sensors work based on the variation of the capacitance generated by touching the electrode. This variation is read and converted by the ADC. Below, we have a schematic of how the electrode, the substrate, shows the location of the ESP32 with the capacitance.

Step 3: WiFi NodeMCU-32S ESP-WROOM-32

I have the pinout here, and you can see that in PinOuts, you have pins that can be “Touch”. There are six on the left side of the image, and three on the right side.

Step 4: Assembly

In our assembly, we have the ESP32 powered by a USB, two LEDs, and two jumpers. The first green wire is responsible for turning the LEDs on and off. The second white wire causes the LEDs to blink when turned on.

The assembly is simple, as you simply stretch two wires and place two LEDs with resistors. This is how the software works.

Step 5: Code

Because ESP32 has native support for the Touch Capacitive, our key point will be the touchRead (pin) function, because it will return to us a positive integer value. And, when we touch the pin that refers to the sensor, the value generated must be the closest to zero.

It is essential to first calibrate the sensor in order to have a notion of the value that we can take to ensure that there was a touch. Why exactly is this essential? There are some factors that influence the function of the Touch, such as humidity, for example.

Step 6: Setup

Here, in the configuration part, we will connect the pins to the LEDs, as well as the pins with Touch sensor. We point out the value that gives us the certainty of touch (which is identified through the calibration), and the code identified as the maximum capacitance. In the Setup, we program the LEDs.

int pinoVermelho = 16; //pino que ligamos o LED vermelho
int pinoVerde = 17; //pino que ligamos o LED verde int pinoTouchOn = 4; //pino com sensor touch (pode-se usar a constante nativa T0) int pinoTouchBlink = 13; //pino com sensor touch (pode-se usar a constante nativa T4) int capacitanciaMaxima = 20; //valor que nos da a certeza de toque (ache esse valor através da calibragem) void setup() { Serial.begin(115200); delay(1000); pinMode(pinoVermelho, OUTPUT); pinMode(pinoVerde, OUTPUT); }

Step 7: Loop

In this part of the code, we have the variables mediaT0 and mediaT4, because they are the buttons where we press the Touch. So we take a hundred readings and do an average. This is the action that I consider most important.

When I check that the mediaT0 is less than the maximum capacitance, 20 for example, I light the red and green pins. When the mediaT4 is less than the maximum capacitance, we give a digitalWrite on these same pins to invert the current state of the LEDs, for example, make them blink.

I also point out here that if no Touch pins are being touched, the pins remain erased.

void loop()
{ Serial.println(touchRead(pinoTouchOn)); Serial.println(touchRead(pinoTouchBlink)); int mediaT0 = 0; int mediaT4 = 0; //faz 100 leituras de cada sensor touch e calcula a média do valor lido for(int i=0; i< 100; i++) { mediaT0 += touchRead(pinoTouchOn); mediaT4 += touchRead(pinoTouchBlink); } mediaT0 = mediaT0 / 100; mediaT4 = mediaT4 / 100; //verifica se o valor médio lido no pinoTouchOn é menor que a capacitância máxima definida //se verdadeiro, isso caracteriza um toque // os LEDs vemelho e verde ficarão acesos if(mediaT0 < capacitanciaMaxima) { digitalWrite(pinoVermelho, HIGH); digitalWrite(pinoVerde, HIGH); } //verifica se o valor médio lido no pinoTouchBlink é menor que a capacitância máxima definida //se verdadeiro, isso caracteriza um toque // os LEDs vemelho e verde ficarão piscando com um intervalo de 500 ms else if(mediaT4 < capacitanciaMaxima) { digitalWrite(pinoVermelho, !digitalRead(pinoVermelho)); //inverte o estado atual do LED (HIGH/LOW) digitalWrite(pinoVerde, !digitalRead(pinoVerde)); //inverte o estado atual do LED (HIGH/LOW) delay(500); } //se nenhum dos pinos touch estão sendo tocados, os LEDS ficam apagados else{ digitalWrite(pinoVermelho, LOW); digitalWrite(pinoVerde, LOW); } delay(10); }