Esp32

ESP32 Touch Wake Up from Deep Sleep

ESP32 is an IoT based microcontroller board that comes with prebuilt features such as Bluetooth and WiFi drivers. However, ESP32 has multiple pins that connect to onboard hall effect and capacitive touch sensors. Today we will be discussing the ESP32 capacitive touch pins and how they can be used to wake ESP32 from deep sleep.

What is ESP32 Deep Sleep

ESP32 sleep mode is a power-saving mode that the ESP32 can enter when not in use, saving all data in RAM. At this time, power to any unnecessary peripherals is cut off while the RAM receives enough power to be able to retain its data.

ESP32 Touch Sensor Pinout

The ESP32 board comes with 10 GPIO pins that support capacitive touch sensors. These GPIO pins can detect changes in electrical charge that may be caused by human skin. So, these pins can detect variations caused by human fingers and generate output accordingly.

These pins can easily be integrated with touch pads and can replace mechanical buttons in the ESP32 project. These touch pins can also wake up ESP32 from deep sleep.

Following are the touch sensor pins highlighted in green color:

A picture containing text, electronics Description automatically generated

Here touch sensor pin 0 corresponds to GPIO pin 4 and touch sensor 2 is at GPIO pin 2. One pin which is touch pin 1 is missing in this particular version of ESP32 (30 pin) board. Touch sensor 1 is at GPIO pin 0 which is available in the 36-pin version of ESP32 board.

Enable Touch Wake Up on ESP32

Enabling touch pin wakeup for ESP32 from deep sleep is simple. We just need to use following function inside ESP32 code:

esp_sleep_enable_touchpad_wakeup()

ESP32 Touch Wake up Deep Sleep Example

Now we will take an example to test the ESP32 capacitive touch sensor for awakening our board. Make sure to install the ESP32 board in Arduino IDE. To see a guide on ESP32 installation with Arduino IDE click here.

Now open Arduino IDE Go to: File>Examples>ESP32>DeepSleep>TouchWakeUp

Graphical user interface, text, application Description automatically generated

Following code will appear in new window:

#define Threshold 40 /* sensitivity set */
RTC_DATA_ATTR int bootCount = 0;
touch_pad_t touchPin;
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 due to signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup due to signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Timer caused wakeup");   break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Touchpad caused wakeup"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("ULP program caused wakeup"); break;
    default : Serial.printf("Wake Up was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}
void print_wakeup_touchpad(){
touchPin = esp_sleep_get_touchpad_wakeup_status();
  switch(touchPin)
  {
    case 0  : Serial.println("Touch on GPIO 4"); break;
    case 1  : Serial.println("Touch on GPIO 0"); break;
    case 2  : Serial.println("Touch on GPIO 2"); break;
    case 3  : Serial.println("Touch on GPIO 15"); break;
    case 4  : Serial.println("Touch on GPIO 13"); break;
    case 5  : Serial.println("Touch on GPIO 12"); break;
    case 6  : Serial.println("Touch on GPIO 14"); break;
    case 7  : Serial.println("Touch on GPIO 27"); break;
    case 8  : Serial.println("Touch on GPIO 33"); break;
    case 9  : Serial.println("Touch on GPIO 32"); break;
    default : Serial.println("Wakeup not by touchpad"); break;
  }
}
void callback(){
  //placeholder callback function
}
void setup(){
  Serial.begin(115200);
  delay(1000);
  //Increase boot number
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));
  //Print reason of wakeup and pin number
  print_wakeup_reason();
  print_wakeup_touchpad();
  //set interrupt at touch pin t0
  touchAttachInterrupt(T0, callback, Threshold);
  //Touchpad configured
  esp_sleep_enable_touchpad_wakeup();
  //sleep mode active
  Serial.println("Going to sleep now");
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}
void loop(){

}

This code reads the touch pin T0. Here T0 corresponds to GPIO 4 or D4. First thing we have to start with is setting a threshold value after which ESP32 will wake up from sleep. Here in the above example threshold is defined as 40. Threshold value can be changed depending upon conditions.

Once the read value at T0 pin becomes lower than the set threshold value the ESP32 will wake up from sleep by calling callback() function.

The callback() function will execute only when ESP32 is awake. If one just touches and releases the pin it will not be executed. If we want to use some other pin, then we have to use interrupts for that pin.

Next using the esp_sleep_enable_touchpad_wakeup() function we set the touch pin as a wakeup source for the ESP32 board.

Hardware

To test the code, take a breadboard and place an ESP32 board over there, connect a jumper wire to GPIO 4 and touch it using your finger.

Schematic

Attach a jumper wire at D4 and touch the header of the jumper wire using your finger.

Output

Output displays the reading before and after touching the GPIO pin 4 of ESP32. Here we can see the ESP32 wakes after touching the pin and prints the message of pin number causing it.

Conclusion

This article will guide you on use of ESP32 touch pins to wake up your board from deep sleep. ESP32 has 10 touch pins which can be called and set as a source for awakening ESP32 boards. When touch is detected at GPIO touch pin ESP32 will wake up and run the callback () function after that it returns to sleep.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.