ESP32 Touch Wake Up from Deep Sleep

This guide shows how to wake up the ESP32 from deep sleep using the touch-sensitive pins. The ESP32 will be programmed using Arduino IDE.

ESP32 Touch Wake Up from Deep Sleep

Updated 8 October 2024.

The ESP32 can be awake from deep sleep using several wake-up sources: timer, external wake-up, and touch wake-up. This article shows how to use touch wake-up.

To learn more about deep sleep and other wake-up sources, you can follow the next tutorials:

Writing a Deep Sleep Sketch

To write a sketch to put your ESP32 into deep sleep mode, and then wake it up, you need to:

  1. First, configure the wake-up sources. This means configuring what will wake up the ESP32. You can use one or combine more than one wake-up source. This tutorial covers how to use the touch pins as a wake-up source. For that, use the touchSleepWakeUpEnable() function and pass as arguments the touch pin and the touch threshold.
  2. You can decide what peripherals to shut down or keep on during deep sleep. However, by default, the ESP32 automatically powers down the peripherals that are not needed with the wake-up source you define.
  3. Finally, you use the esp_deep_sleep_start() function to put your ESP32 into deep sleep mode.

Touch Wake Up

The ESP32 board has 10 capacitive touch GPIOs (that might change depending on the ESP32 chip). These GPIOs can sense variations in anything that holds an electrical charge, like the human skin. So they can detect variations induced when touching the GPIOs with a finger. These ESP32 touch pins can be used to wake up the ESP32 from deep sleep.

Touch Wake Up

Learn more about the ESP32 touch pins: ESP32 Capacitive Touch Sensor Pins with Arduino IDE.

Touch Pins

The ESP32 touch pins are highlighted in pink color in the following diagram.

You can see that touch sensor 0 corresponds to GPIO 4, touch sensor 2 to GPIO 2, and so on.

Learn everything you need to know about the ESP32 GPIOs: ESP32 Pinout Reference: Which GPIO pins should you use?

If you’re using an ESP32 with the S3 chip, check this GPIOs guide instead.

Enable Touch Wake-Up

Enabling the ESP32 to wake up using a touchpin is simple. In the Arduino IDE, you need to use the following function—pass as argument the touch pin and the touch threshold:

touchSleepWakeUpEnable(TOUCH_PIN, THRESHOLD);

Code – Touch Wake-Up

To program the ESP32 we’ll use Arduino IDE. So, you need to make sure you have the ESP32 core installed. Follow the next tutorial if you haven’t already:

Let’s see how touch wake-up works using an example from the ESP32 Arduino core. Open your Arduino IDE, and go to File Examples ESP32 Deep Sleep, and open the TouchWakeUp example sketch.

/*
Deep Sleep with Touch Wake Up
=====================================
This code displays how to use deep sleep with
a touch as a wake up source and how to store data in
RTC memory to use it over reboots

ESP32 can have multiple touch pads enabled as wakeup source
ESP32-S2 and ESP32-S3 supports only 1 touch pad as wakeup source enabled

This code is under Public Domain License.

Author:
Pranav Cherukupalli <[email protected]>
*/

#if CONFIG_IDF_TARGET_ESP32
#define THRESHOLD 40   /* Greater the value, more the sensitivity */
#else                  //ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted) */
#define THRESHOLD 5000 /* Lower the value, more the sensitivity */
#endif

RTC_DATA_ATTR int bootCount = 0;
touch_pad_t touchPin;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason() {
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch (wakeup_reason) {
    case ESP_SLEEP_WAKEUP_EXT0:     Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1:     Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER:    Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP:      Serial.println("Wakeup caused by ULP program"); break;
    default:                        Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
  }
}

/*
Method to print the touchpad by which ESP32
has been awaken from sleep
*/
void print_wakeup_touchpad() {
  touchPin = esp_sleep_get_touchpad_wakeup_status();

#if CONFIG_IDF_TARGET_ESP32
  switch (touchPin) {
    case 0:  Serial.println("Touch detected on GPIO 4"); break;
    case 1:  Serial.println("Touch detected on GPIO 0"); break;
    case 2:  Serial.println("Touch detected on GPIO 2"); break;
    case 3:  Serial.println("Touch detected on GPIO 15"); break;
    case 4:  Serial.println("Touch detected on GPIO 13"); break;
    case 5:  Serial.println("Touch detected on GPIO 12"); break;
    case 6:  Serial.println("Touch detected on GPIO 14"); break;
    case 7:  Serial.println("Touch detected on GPIO 27"); break;
    case 8:  Serial.println("Touch detected on GPIO 33"); break;
    case 9:  Serial.println("Touch detected on GPIO 32"); break;
    default: Serial.println("Wakeup not by touchpad"); break;
  }
#else
  if (touchPin < TOUCH_PAD_MAX) {
    Serial.printf("Touch detected on GPIO %d\n", touchPin);
  } else {
    Serial.println("Wakeup not by touchpad");
  }
#endif
}

void setup() {
  Serial.begin(115200);
  delay(1000);  //Take some time to open up the Serial Monitor

  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  //Print the wakeup reason for ESP32 and touchpad too
  print_wakeup_reason();
  print_wakeup_touchpad();

#if CONFIG_IDF_TARGET_ESP32
  //Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
  touchSleepWakeUpEnable(T3, THRESHOLD);
  touchSleepWakeUpEnable(T7, THRESHOLD);

#else  //ESP32-S2 + ESP32-S3
  //Setup sleep wakeup on Touch Pad 3 (GPIO3)
  touchSleepWakeUpEnable(T3, THRESHOLD);

#endif

  //Go to sleep now
  Serial.println("Going to sleep now");
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}

void loop() {
  //This will never be reached
}

View raw code

Setting the Threshold

The first thing you need to do is setting a threshold value for the touch pins.

#define THRESHOLD 40 // Greater the value, more the sensitivity

The values read by a touch pin decrease when you touch it. The threshold value means that the ESP32 will wake up when the value read on the touch pin is below 40. You can adjust that value depending on the desired sensitivity.

Important: however, if you’re using an ESP32-S2 or ESP32-S3 model, things work a little
differently. That’s why there’s a different section in the code defining a different threshold for those boards. In this case, the lower the value, the more the sensitivity.

#if CONFIG_IDF_TARGET_ESP32
#define THRESHOLD 40 // Greater the value, more the sensitivity
#else // ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted)
#define THRESHOLD 5000 // Lower the value, more the sensitivity
#endif

Setting Touch Pins as a Wake-up Source

To set a touch pin as a wake-up source, you can use the touchSleepWakeUpEnable() function that accepts as arguments the touch pin and the threshold value that will wake up the board.

In this example, it sets GPIO 15 (T3) and GPIO 27 (T7) as wake-up sources with the same threshold value.

#if CONFIG_IDF_TARGET_ESP32
  // Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
  touchSleepWakeUpEnable(T3, THRESHOLD);
  touchSleepWakeUpEnable(T7, THRESHOLD);

If you’re using an ESP32-S2 or S3 model, the example just defines wake-up on GPIO 3 (T3). Note that the touch pin numbering might be different depending on the board model you’re using.

#else // ESP32-S2 + ESP32-S3
  // Setup sleep wakeup on Touch Pad 3 (GPIO3)
  touchSleepWakeUpEnable(T3, THRESHOLD);

Finally, call the esp_deep_sleep_start() to put the ESP32 in deep sleep mode.

esp_deep_sleep_start();

These were the general instructions to set up touch pins as a wake-up source. Now, let’s take a look at the other sections of the code.

setup()

When the ESP32 wakes up from sleep, it will run the code from the start until it finds the esp_deep_sleep_start() function.

In this particular example, we have a control variable called bootCount that will be increased between each sleep cycle so that we have an idea of how many times the ESP32 woke up.

// Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));

We also call the print_wakeup_reason() and print_wakeup_touchpad() functions defined earlier in the code to print the wake-up reason and the pin that caused the wake-up.

// Print the wakeup reason for ESP32 and touchpad too
print_wakeup_reason();
print_wakeup_touchpad();

Wiring the Circuit

To test this example, wire a jumper wire to GPIO 15 and/or GPIO 27, as shown in the schematic below.

Touch wake up Wiring the Circuit

If you’re using an ESP32-S2 or ESP32-S3 model, please check the location of your touch pins.

Parts Required:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Testing the Example

Upload the code to your ESP32, and open the Serial Monitor at a baud rate of 115200.

The ESP32 goes into deep sleep mode.

You can wake it up by touching the wire connected to Touch Pin 3.

ESP32 Touch Sensor Pins Arduino IDE

When you touch the pin, the ESP32 displays on the Serial Monitor: the boot number, the wake-up cause, and which touch-sensitive GPIO caused the wake-up.

Testing ESP32 touch pins as a wake-up source Serial Monitor Demonstration

Wrapping Up

In this article, we’ve shown you how to wake up the ESP32 using the touch-sensitive GPIOs. In summary:

  • First, define the wake-up source by calling the touchSleepWakeUpEnable() function and pass as arguments the touch pin and the threshold value.
  • Then, use the esp_deep_sleep_start() function to put the ESP32 in deep sleep mode.

You can learn more about deep sleep with the ESP32 with our complete guide: ESP32 Deep Sleep with Arduino IDE and Wake Up Sources.

If you like ESP32, you may also like the following resources:

This is an excerpt from our course: Learn ESP32 with Arduino IDE. If you like ESP32 and you want to learn more, we recommend enrolling in Learn ESP32 with Arduino IDE course.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

15 thoughts on “ESP32 Touch Wake Up from Deep Sleep”

      • thank you very much Sara I get Guru Meditation Error: Core 1 panic’ed (Coprocessor exception)
        instead of the function, why does this happen?

        Reply
        • Hi David.
          It depends on the code you’re trying to run.
          I think to avoid those kind of errors, you should add your code before the esp_deep_sleep_start(); function instead of putting it inside the callback function.
          Regards,
          Sara

          Reply
          • Oh I will make that im making a temperature sensor and the code is a simple :
            Serial.begin(115200);
            sensors.begin(); //Se inicia el sensor
            sensors.requestTemperatures(); //Se envía el comando para leer la temperatura
            float temp= sensors.getTempCByIndex(0); //Se obtiene la temperatura en ºC
            Serial.print(“Temperatura= “);
            Serial.print(temp);
            Serial.println(” C”);

          • I think it worked, not sure how to tell if its really sleeping but it displayed the temperature 🙂 thank you so much!

  1. This is nice Sara (I presume), thanks!

    Have you (or anyone) had any testing with the current draw to see what the deep sleep actually pulls?
    I’ve read as low as 10uA, but curious how that looks with the touch sensor enabled.

    Reply
  2. Hi Sara
    Thanks for all theses informations.
    I’ve a problem when I use touch pins with interrupts with an ESP32 TTGO :
    as soon as i touch a pin the ESP re-starts like a boot.
    Do you know this disorder?

    Reply
  3. hi
    can we use esp32 as a touch key ?
    place a pad on pcb and then put a glass (3mm) on it and then touch the area above pad and sense the touch .
    Is it possible ?

    Reply
  4. Hi
    The Sketch work on ESP32 S2 bat not on ESP32 S3
    After going sleep it wake up instantly witch error:

    Boot number: 2
    Wakeup caused by touchpad
    E (1021) TOUCH_SENSOR: touch_pad_get_wakeup_status(222): Touch channel error

    assert failed: esp_sleep_get_touchpad_wakeup_status sleep_modes.c:931 (ret == ESP_OK && “wakeup reason is RTC_TOUCH_TRIG_EN but SENS_TOUCH_MEAS_EN is zero”)

    Wy thir error?

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.