Touch Pad error on the ESP32 Capacitive Touch Pins

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
misaalanshori
Posts: 26
Joined: Sat Jun 22, 2019 6:07 am

Touch Pad error on the ESP32 Capacitive Touch Pins

Post by misaalanshori » Sat May 09, 2020 3:20 pm

I've been playing around with the Touch Pins on the ESP32, but it looks like after using it for a while the script would stop because of a "ValueError: Touch pad error". The weird thing is that the error only happens when the touchpad is being touched, so my current workaround is to just return "True" as the touchpad state when the error happens, here's the function I call to get the touchpad state

Code: Select all

def touchState(touchPin):

    try: #Try getting Touch state
        ts = touchPin.read()
    except: #If error, return True
        return True
        
    if ts < 400: #If touchpad value is under 400, return true
        return True
    elif ts > 400:#If touchpad value is over 400, return false
        return False
    else: #if everything is false just return false
        return False
The board I'm using is the ESP32 Heltec Wifi Kit 32, the pins I'm using are pins 12, 14, and 27.
Another thing that I'm noticing is that I only get the error if the board has been running for a while, If I haven't turned it on for a couple of minutes and then turn it on again, everything will work just fine without errors for maybe 1-2 minutes after that I will start getting the Touchpad Error again, and will only stop if I unplug the board for a while.

Has anyone else experienced this error? Are there any fix for this? Is it hardware related or software related?

User avatar
tve
Posts: 197
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: Touch Pad error on the ESP32 Capacitive Touch Pins

Post by tve » Sat May 09, 2020 4:18 pm

I have not used the touchpad feature at all. That being said, I see that the driver doesn't report the error details. It should be changed to use `check_esp_err`. Sigh.

But the ESP-IDF docs state:

Code: Select all

Return:
ESP_OK Success
ESP_ERR_INVALID_ARG Touch pad parameter error
ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of “touch_value” is 0.
ESP_FAIL Touch pad not initialized
So assuming you've initialized it correctly, the "only possible" error is ESP_ERR_INVALID_STATE, which isn't terribly helpful. I also notice that the intro to the touch functionality says:
If measurements are noisy and capacity changes are small, hardware touch detection might be unreliable. To resolve this issue, instead of using hardware detection / provided interrupts, implement measurement filtering and perform touch detection in your own application. For sample implementation of both methods of touch detection, see peripherals/touch_pad_interrupt.
BTW, when you write

Code: Select all

if ts < 400: #If touchpad value is under 400, return true
        return True
that comment is not useful at all because it just restates what the code says. It would be more helpful to have a comment like "A value of 400 was found to be a good threshold on the Heltec so-and-so board." or something else that is relevant and not evident from the code.

misaalanshori
Posts: 26
Joined: Sat Jun 22, 2019 6:07 am

Re: Touch Pad error on the ESP32 Capacitive Touch Pins

Post by misaalanshori » Sat May 09, 2020 9:49 pm

From what I remember the value when not touched is around 600, and it usually drops to 100-200 when it is touched which is a pretty big difference and that is why I choose 400 as the "center" point. This is how I initialize the touchpad

Code: Select all

t1 = TouchPad(Pin(12))
t2 = TouchPad(Pin(14))
t3 = TouchPad(Pin(27))
and then I could call the function with something like
touchState(t1)
and it should return a True or False

Also, the error only happens if I try to read the state while the touchpad is being touched, so maybe, the longer the esp32 board is running, the value reading shifts lower from normally 600-100 to maybe 500-0 or even from 600 to a negative number and that's why its a "ValueError"? That is just my guess, but I haven't seen how the library reads the touchpad values and I'm not sure if that is the problem

Also, that comment you mentioned is not in the actual code. For some reason, I decided to put it there while writing the post last night. But sometimes I do put redundant comments on my code just to make it easier for future me to read the code again, just in case I left the code for a while and I want to reuse it in a couple of weeks or months from now

User avatar
ghayne
Posts: 41
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

Re: Touch Pad error on the ESP32 Capacitive Touch Pins

Post by ghayne » Sun May 17, 2020 8:50 pm

Also bear in mind that the touch inputs are calibrated at boot, so don't hold the board while booting!

Post Reply