External wakeup with Arduino on ESP32-C3

Updated on 30 October 2023
dev board ESP32-C3-DevKitM-1
chip ESP32-C3-MINI-1-N4
features external wakeup trigger sleep esp32c3

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Pre-requisites

Try these simpler or similar examples:

Buy the components

Code

Download code external-wakeup-arduino-esp32c3.ino
#define LED 3  // or use LED_BUILTIN for on-board LED
#define INTERRUPT_PIN 4
RTC_DATA_ATTR int bootCount = 0;

void setup() {
  Serial.begin(115200);
  delay(1000);

  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  print_wakeup_reason();

  Serial.println("Going to blink the LED 10 times");
  blink(10);  // Allow time for wakeup
  Serial.println("Going to sleep now");

  esp_deep_sleep_enable_gpio_wakeup(1 << INTERRUPT_PIN,
    ESP_GPIO_WAKEUP_GPIO_HIGH);
  esp_deep_sleep_start();

  Serial.println("This will never be printed");
}

void loop() { }

void blink(int times) {
  for (int i = 0; i < times; i++) {
    digitalWrite(LED, HIGH);
    delay(1000);

    digitalWrite(LED, LOW);
    delay(1000);
    Serial.print("Blink ");
    Serial.println(i + 1);
  }
}

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;
  }
}

Makefile

BOARD?=esp32:esp32:esp32c3
PORT?=/dev/cu.usbserial-*
BUILD=build
## Pass in V=--verbose to output more information than default

.PHONY: help default lint compile upload clean

default: clean lint compile upload

help: ## Show help message
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

lint: ## Lint code using cpplint
	cpplint --extensions=ino --filter=-legal/copyright,-runtime/int *.ino

compile: ## Compile code and create the firmware binary
	arduino-cli compile $(V) --fqbn $(BOARD) --output-dir $(BUILD) ./

upload: ## Upload the firmware to the board
	arduino-cli upload $(V) --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean: ## Remove all built files
	rm -rf build

Prototype

A photo of the actual setup.

External wakeup with Arduino on ESP32-C3 prototype

Schematic

Wire up the hardware accordingly

External wakeup with Arduino on ESP32-C3 schematic

Serial console

Serial output from the firmware.

External wakeup with Arduino on ESP32-C3 serial console

Description

Wire up the hardware according to the schematic. Compile and upload the firmware.

Press the push button to wake up and see the LED Blinking 10 times before going back to sleep again.

Troubleshooting: Sleep function

Use function esp_deep_sleep_enable_gpio_wakeup instead of esp_sleep_enable_ext0_wakeup or esp_sleep_enable_ext1_wakeup. [5] [6]

References