Dependancies
Pre-requisites
Buy the components
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;
}
}
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
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.
Use function esp_deep_sleep_enable_gpio_wakeup
instead of esp_sleep_enable_ext0_wakeup
or esp_sleep_enable_ext1_wakeup
. [5] [6]