Search code examples
arduinosleepesp32

ESP32 EXT1 wakeup


I have an ESP32 dev kit connected to a 3 x 4 matrix keypad.

3 columns are connected to the PINS: 12, 13, 27

4 rows are connected to the PINS: 14, 15, 32, 33

GOAL: I would love to be able to put the MCU in deep sleep mode and be able to wake it up with a keypress, meaning ANY keypress, not one specifically.

I've put together this piece of code

void setup()
{
  Serial.begin(115200);
  // Wait for serial.
  while (!Serial)
    delay(100);
  Serial.println("I am ON");
  // power all columns.
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(27, OUTPUT);
  // enable all rows as input (HIGH).
  pinMode(14, INPUT_PULLDOWN);
  pinMode(15, INPUT_PULLDOWN); // tried also pin 35, no luck.
  pinMode(32, INPUT_PULLDOWN);
  pinMode(33, INPUT_PULLDOWN);
  esp_sleep_enable_ext1_wakeup(0b001100000000000000001100000000000000, ESP_EXT1_WAKEUP_ANY_HIGH);
  // hold the pin HIGH during sleep.
  gpio_deep_sleep_hold_en();

  // do the actual output on the pins.
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
  digitalWrite(27, HIGH);

  // delay to make sure.
  delay(500);
  esp_deep_sleep_start();
}

The problem seems that the whole setup is erratic. Some keypresses work more consistently than others. But more consistently still means that sometime the more consistent key, won't be able to wake up the device.

I've tried the snippet with 2 different boards (Huzzah32 and a generic dev kit) with the same results.

The keypad setup works otherwise great, meaning I am able to read the keypresses reliably.


Solution

  • Ah, I found it: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html#external-wakeup-ext1 The pull-up/down resistors are not normally enabled during deep sleep, which seems to explain the inconsistencies.