ESP32 Using the touch pads

The ESP32 has the capabilities to define up to 10 GPIO’s as Touchpads. There is no external hardware needed, it requires only to connect a pad (piece of metal or just an uninsulated wire) to the GPIO.

This is an example of a pad I used for testing:  

As you can see I just used a piece of scrap metal with a wire soldered to it. The wire then was directly connected to the GPIO of the ESP32.

I was curious how it works and was surprised how easy it is to implement simple user input buttons. To get the current status of the touchpad a simple

returns an integer value. This value is low if the pad is touched and higher if the pad is not touched. But the values are different, depending on the type of the pad used and the cable length between the ESP32 GPIO and the pad (found this out by playing around with the function). So once you know what kind of pad you will use you need to make a kind of calibration in your software to know when the pad is touched and when it is released. I made just a small endless loop, reading from the touch pad to see how the values are changing.

From the output I got the threshold value to determine whether the pad was touched or released.

Coming from Arduino style coding, one would most likely just poll the pad input inside the loop() and detect touch down and up from there. But within a multitasking – dual core system this is not a good practice.

The library for the touch pads actually offers a function that will trigger an interrupt whenever the value on the touch pad falls below a given threshold. To use it, the touch pad library is told which pad is used, which function should be called if the value is below the threshold and what the threshold value is

The threshold 20 in this call is valid for my type of pad (see picture above), it will be different for your application.

One problem I found here is that the ISR (interrupt service routine) was actually called frequently, while the pad was touched. So some SW countermeasure was necessary. I wanted as well to detect short touch events and long-touch events. This gives the possibility to add 2 functionalities to one pad.

Now here is how I did it:
Define the pad to be used, some global variables and the functions used

Initialize the touch pad interrupt

Write the ISR function

In the ISR routine we just set a flag that a touch was detected (this is used to filter out the frequent ISR calls while the pad is touched). Then we start a timer that will check in 50ms if the pad is still touched. The timer calls checkPad1TouchStatus()

As you can see this function just checks if the pad is still touched. If the pad is not touched anymore, it checks the time since the touch was detected to decide if it was a short touch or a long touch. For me a long touch would be if the duration is longer than 500ms. Depending on the touched time, then either the flag shortTouchPad1 is set to true or the flag longTouchPad1 is set to true. The status of this flags then can be checked within loop() to decide what to do

And thats all. I just added one more function to disable the touch pad in case an OTA update is running.

And here is the complete code block:



Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory