DaveL17
DaveL17

Reputation: 2013

ESP32C6 Deep Sleep Recovery via External IO

I'm finding it difficult to find much reliable information on how to implement deep sleep in combination with wake on external IO with ESP32C6 (ESP32-C6-DevKitC-1, rev. 1.12). After days of false starts, I've decided to go back to basics and try to implement it in the simplest way I can think of that meets my criteria. I think where I might be stumbling is related to this note in the docs:

Internal pullups and pulldowns don't work when RTC peripherals are shut down. In this case, external resistors need to be added. Alternatively, RTC peripherals (and pullups/pulldowns) may be kept enabled using esp_sleep_pd_config function.

source

I don't understand how to implement esp_sleep_pd_config() and what the best options are available for it my use case. If the answer is to use an external pullup, I can do that, but I want whatever is the best option to keep power consumption to a minimum (and at least have some comprehension of the options laid before me).

Goal:

A low power switch monitor where the switch can be open or closed for extended periods (not momentary). The board should wake on low -> high and high -> low. Upon state change, connect to WiFi, and send an insecure POST message to a local server. The following sketch only applies to the sleep/wake component.

Sketch

Here is my simple sketch just to get wake on external IO to work; changing the pin state does not get a response (I'm using Arduino IDE not ESP-IDF).

#include <Arduino.h>
#include <esp_sleep.h>

uint64_t gpio_pin_mask = (1ULL << 4);
RTC_DATA_ATTR unsigned btn_state;

void setup() {
  Serial.begin(115200);
  pinMode(4, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(4) == 0) {
    Serial.println("btn state 0");
    btn_state = 0;
    esp_sleep_enable_ext1_wakeup_io(gpio_pin_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
    esp_deep_sleep_start();
  } else {
    Serial.println("btn state 1");
    btn_state = 1;
    esp_sleep_enable_ext1_wakeup_io(gpio_pin_mask, ESP_EXT1_WAKEUP_ANY_LOW);
    esp_deep_sleep_start();
  }
  delay(1000);
}

ESP32-C6-DevKitC-1, rev. 1.12
Arduino IDE 2.3.4 (CLI 1.1.1)

Upvotes: 0

Views: 110

Answers (1)

DaveL17
DaveL17

Reputation: 2013

The above logic was good, something was wrong with my prototype. Here is some drier code that I can confirm works on ESP32C6 that also implements a suggestion by @romkey. This is running with a 10K external resistor and not esp_sleep_pd_config().

#include <Arduino.h>
#include <esp_sleep.h>

uint64_t gpio_pin_mask = (1ULL << 4);
RTC_DATA_ATTR unsigned btn_state;

void setup() {
  Serial.begin(115200);
  pinMode(4, INPUT_PULLUP);
}

void loop() {
  btn_state = digitalRead(4);
  Serial.println("btn state " + String(btn_state));
  if (btn_state == 0) {
    esp_sleep_enable_ext1_wakeup_io(gpio_pin_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
  } else {
    esp_sleep_enable_ext1_wakeup_io(gpio_pin_mask, ESP_EXT1_WAKEUP_ANY_LOW);
  }
  Serial.println("Entering sleep...");
  delay(1500);
  esp_deep_sleep_start();
}

Upvotes: 1

Related Questions