Hi all.. I’m currently working with Module 3 Unit 3 of the ESP32 with Arduino IDE course.
I’m trying to understand the callback in the touchAttachInterrupt(T3, callback, Threshold); piece of code.
My understanding is that callback is a user defined function that is executed when the interrupt is triggered, is that correct? I ask because I put a simple Serial.println() statement inside the callback function and it doesn’t seem to execute. What am I missing?
Hi Charles, you are correct.
But, you need to take this into account:
“The callback() function will only be executed if the ESP32 is awake.
- If the ESP32 is asleep and you touch T3, the ESP will wake up – the callback() function won’t be executed if you just press and release the touch pin;
- If the ESP32 is awake and you touch T3, the callback function will be executed. So, if you want to execute the callback() function when you wake up the ESP32, you need to hold the touch on that pin for a while, until the function is executed.”
I hope this is clear.
Can you try it and see if it works?
Regards,
Sara
Thank you Sara for your answer. I was aware of this but I didn’t hold the tap long enough. Holding longer did the trick. While playing around with the touch pins I found them somewhat flaky. Phantom touches even when setting the threshold variable low.
Hi Charles.
Yes, that can happen.
Specially if you don’y have good quality jumper wires.
Instead of using the touch pins for wake up, you can consider using pushbuttons with the external wake up instead.
Regards,
Sara
Ciao Sara,
I’m having the same problem: I’m trying to execute a simple “serial.print” inside the callback function:
void callback(){
//placeholder callback function
Serial.println("*** inside callback function ***");
}
BUT, Holding the cable for more then one second, the ESP32 goes to sleep and wakes up itself again and again very fast, without calling the “callback” function.
Following the monitor outuput:
rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8
Boot number: 23
Wakeup caused by touchpad
Touch detected on GPIO 15
Going to sleep now
ets Jun 8 2016 00:22:57
rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8
Boot number: 24
Wakeup caused by touchpad
Touch detected on GPIO 15
Going to sleep now
ets Jun 8 2016 00:22:57
rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8
Boot number: 25
Wakeup caused by touchpad
Touch detected on GPIO 15
Going to sleep now
Hi.
As I explained previously:
The callback() function will only be executed if the ESP32 is awake.
- If the ESP32 is asleep and you touch T3, the ESP will wake up – the callback() function won’t be executed if you just press and release the touch pin;
- If the ESP32 is awake and you touch T3, the callback function will be executed. So, if you want to execute the callback() function when you wake up the ESP32, you need to hold the touch on that pin for a while, until the function is executed.”
You need to hold the touch for a while.
Additionally, you may need to put a delay() somewhere in your code.
What is exactly the code that you are trying to run?
Regards,
Sara
thanks for your answer Sara. I’m running the code you’ve shared in the unit, I did just copy and paste. As I’ve wrote, I’m already holding the touch for many seconds.
Now I’ve inserted a delay, as you’ve suggested:
//Setup interrupt on Touch Pad 3 (GPIO15)
touchAttachInterrupt(T3, callback, Threshold);
delay(1000);
Holding the cable now the callback function is executed! 🙂
Many thanks,
Mosè