Embedded / IoT
Beginner
ESP-IDF / Arduino
저전력 배터리 운용을 위한 ESP32 Deep Sleep 및 타이머/외부 GPIO 웨이크업ESP32 Deep Sleep with Timer and External GPIO Wakeup for Battery Operation
평상시 150mA 이상 소모하는 전류를 10μA 수준으로 낮추고, RTC 타이머 또는 외부 GPIO 인터럽트로 깨어나는 배터리 기반 센서 노드 펌웨어 설계입니다.Designing low-power ESP32 firmware that reduces idle current from 150mA+ down to ~10μA and wakes via the RTC timer or external GPIO interrupts.
#Embedded #ESP32 #DeepSleep #LowPower #PowerManagement
핵심 개념ESP32가 Deep Sleep 모드에 진입하면 메인 CPU 코어, 일반 SRAM, Wi-Fi/Bluetooth 무선 모듈의 전원이 차단되고 ULP(초저전력 보조 프로세서)와 RTC 컨트롤러 및 8KB RTC 고속/저속 메모리만 유지됩니다. 깨어날 때마다 프로그램은
setup()부터 다시 부팅되며, 수면 전후로 유지할 변수는 RTC_DATA_ATTR 속성을 붙여 RTC 메모리에 배치합니다.Key ConceptWhen the ESP32 enters Deep Sleep, power to the main CPU cores, standard SRAM, and Wi-Fi/Bluetooth radios is cut, leaving only the ULP coprocessor, RTC controller, and 8KB of RTC memory powered. Because waking from Deep Sleep triggers a reset back into
setup(), variables that must persist across sleep cycles are annotated with RTC_DATA_ATTR.1. Deep Sleep 타이머 및 GPIO 웨이크업 C++ 코드1. Deep Sleep Timer and GPIO Wakeup Implementation in C++
#include <esp_sleep.h>
#define TIME_TO_SLEEP_SEC 60 // 60초 수면 주기
#define uS_TO_S_FACTOR 1000000ULL
RTC_DATA_ATTR int bootCount = 0; // RTC 메모리에 배치되어 Deep Sleep 후에도 값이 유지됨
void setup() {
Serial.begin(115200);
bootCount++;
Serial.printf("[Boot Count] %d 회 부팅되었습니다.\n", bootCount);
// 깨어난 원인(Wakeup Cause) 판별
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
if (cause == ESP_SLEEP_WAKEUP_EXT0) {
Serial.println("외부 GPIO 버튼 인터럽트로 깨어났습니다.");
}
// 1. 센서 측정 및 무선 데이터 전송 수행
// ReadAndSendSensorData();
// 2. 타이머 웨이크업 소스 설정 (60초, 64비트 ULL 곱셈 필수)
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_SEC * uS_TO_S_FACTOR);
// 3. 외부 버튼(RTC GPIO 33번이 LOW가 될 때) 웨이크업 추가
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0);
Serial.println("Deep Sleep에 진입합니다...");
Serial.flush(); // 시리얼 송신 버퍼가 모두 비워질 때까지 대기
esp_deep_sleep_start();
}
void loop() {}
#include <esp_sleep.h>
#define TIME_TO_SLEEP_SEC 60 // 60-second sleep interval
#define uS_TO_S_FACTOR 1000000ULL
RTC_DATA_ATTR int bootCount = 0; // Stored in RTC memory; persists across Deep Sleep resets
void setup() {
Serial.begin(115200);
bootCount++;
Serial.printf("[Boot Count] Booted %d times.\n", bootCount);
// Inspect wakeup cause
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
if (cause == ESP_SLEEP_WAKEUP_EXT0) {
Serial.println("Woken up by external GPIO button interrupt.");
}
// 1. Sample sensors and transmit telemetry
// ReadAndSendSensorData();
// 2. Configure timer wakeup source (60s; use 64-bit ULL multiplier)
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_SEC * uS_TO_S_FACTOR);
// 3. Configure external button wakeup (when RTC GPIO 33 goes LOW)
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0);
Serial.println("Entering Deep Sleep...");
Serial.flush(); // Wait for UART transmit buffer to drain before powering down
esp_deep_sleep_start();
}
void loop() {}
2. 실무 적용 및 주의사항2. Practical Considerations and Caveats
- RTC GPIO 핀 번호 확인: 모든 GPIO가 Deep Sleep 중에 동작하는 것은 아닙니다.
ext0및ext1외부 웨이크업 핀은 전원 차단 중에도 살아 있는 RTC GPIO(GPIO 0, 2, 4, 12~15, 25~27, 32~39) 핀만 지정할 수 있습니다. - 타이머 인자 64비트 정수(
ULL) 오버플로우 방지:esp_sleep_enable_timer_wakeup()은 마이크로초($\mu\text{s}$) 단위의uint64_t값을 받습니다. 32비트 부호 있는 정수로 계산하면 약 35분(2,147,483,647 us) 이상 수면 시간을 설정할 때 오버플로우가 발생하므로 반드시1000000ULL접미사를 붙여야 합니다. - 보드 내 주변 소자 대기 전류 확인: ESP32 칩 자체가 10μA만 소모하더라도, 일반 개발 보드(NodeMCU, DevKitC)에 장착된 USB-UART 변환 칩(CP2102, CH340)과 LDO 레귤레이터(AMS1117), 전원 LED가 상시 5~10mA를 소모하므로 실제 배터리 제품 제작 시에는 저전력 LDO(HT7333 등)가 적용된 베어 모듈 회로를 구성해야 합니다.
- Use RTC-Capable GPIOs Only: Standard digital GPIOs are powered off during Deep Sleep. External
ext0andext1wakeups only work on RTC GPIOs (GPIO 0, 2, 4, 12–15, 25–27, 32–39). - Prevent 32-Bit Overflow on Microsecond Sleep Duration:
esp_sleep_enable_timer_wakeup()accepts a 64-bituint64_tduration in microseconds. Always multiply with an unsigned 64-bit literal (1000000ULL) so sleep durations beyond ~35 minutes (2,147,483,647 us) do not overflow. - Account for Development Board Quiescent Current: Even when the ESP32 silicon draws 10μA, onboard USB-UART bridges (CP2102/CH340), power LEDs, and AMS1117 regulators on standard DevKit boards draw 5–10mA continuously; battery-powered deployments require bare modules paired with low-quiescent LDOs (such as HT7333).
센서 측정 및 패킷 전송이 끝난 직후 즉시 esp_deep_sleep_start()로 전환하는 펌웨어 구조를 적용하면, 리튬 배터리 하나로 수개월 이상 동작하는 무선 원격 측정 노드를 구현할 수 있습니다.
Structuring firmware to sample sensors, transmit packets, and immediately call esp_deep_sleep_start() allows battery-powered ESP32 telemetry nodes to operate for months on a single charge.