Touch Pins as Wakeup Source
Deep sleep is great to save power, but you can't let the ESP32 sleep forever. At some point, an external interrupt is needed to wake it up. One way you can do that is with the touch sensors.
An example could be that you have a battery-powered device with a display. To save power, you turn the display off and put the ESP32 into deep sleep. However, you want to wake up when someone touches your device, so you can turn on the display and show something useful.
Now to allow the touch sensor to wake the ESP32 up, we have to do three things:
- Configure a threshold value (once this value is crossed, the ESP32 will wakeup)
- Enable the touch interrupt (so the ESP32 knows that it has to keep that powered on)
- Actually go into deep sleep.
Translated into Arduino code, you'll get:
#define TOUCH_THRESHOLD 40
Serial.println("ESP32 has started");
// Setup interrupt on Touch Pad 0 (GPIO4)
touchAttachInterrupt(T0, callback, TOUCH_THRESHOLD);
//Configure Touchpad as wakeup source
Serial.println("Going to deepsleep");
//This will never be reached
Normally, when you enter deep sleep, the ESP32 powers down as many peripherals as it can to save power. Here we want to make an exception for the touch sensor, and that's what we do with
esp_sleep_enable_touchpad_wakeup(). This tells the ESP32 to keep the touch sensor on when going to sleep.
Small side note: using the touch sensor as a wakeup source does not require the ULP. Instead, this is run on the RTC so you'll have very little power consumption when you enter deep sleep and have one of the touch pins configured as wakeup source. Great!