FluxBench.AIShared project · read-only

Deep Sleep Battery Logger

ESP32 DevKit (WROOM-32)·Arduino C++·Updated Jul 27, 2026·by FluxBench TeamIncludes Lab + Schematic

A battery-powered data logger pattern: the ESP32 wakes every 5 seconds, reads the battery voltage on GPIO34, prints a reading with a rising/falling trend, then deep-sleeps again — and RTC_DATA_ATTR variables keep the boot count and last reading alive across sleeps. In the simulator the bench potentiometer stands in for the battery pack (wiper to GPIO34): drag it below about 45% and the low-battery warning fires. Watch millis() restart at 0 on every wake and the onboard LED pulse once per boot. Only the timer wake source is modeled in simulation; on real hardware this pattern runs for months on a battery.

sketch.ino
// Deep Sleep Battery Logger — wakes every 5 s, logs the battery voltage, sleeps again.
// The potentiometer on the bench stands in for the battery pack: drag it to change the voltage.

#define BATT_PIN 34        // ADC1 input — usable even while WiFi is on
#define LED_PIN 2          // onboard LED: quick pulse on every wake
#define SLEEP_SECONDS 5
#define LOW_BATT_VOLTS 3.0

RTC_DATA_ATTR int bootCount = 0;      // survives deep sleep (RTC slow memory)
RTC_DATA_ATTR float lastVolts = 0.0;  // previous reading, for a simple trend

void setup() {
  Serial.begin(115200);
  delay(100);
  bootCount++;

  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);   // wake pulse

  if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER) {
    Serial.printf("Wake #%d (timer)\n", bootCount);
  } else {
    Serial.println("Power-on boot - starting battery log");
  }

  int raw = analogRead(BATT_PIN);
  float volts = raw * (3.3 / 4095.0) * 2.0;  // 2:1 divider from the pack

  Serial.printf("Boot #%d  battery %.2f V", bootCount, volts);
  if (bootCount > 1) {
    if (volts < lastVolts - 0.05)      Serial.print("  (falling)");
    else if (volts > lastVolts + 0.05) Serial.print("  (rising)");
    else                               Serial.print("  (steady)");
  }
  Serial.println();
  if (volts < LOW_BATT_VOLTS) Serial.println("!! LOW BATTERY - charge soon");
  lastVolts = volts;

  delay(150);                    // keep the LED visible for a moment
  digitalWrite(LED_PIN, LOW);

  Serial.printf("Sleeping %d s (this boot ran %lu ms)\n", SLEEP_SECONDS, millis());
  Serial.flush();
  esp_sleep_enable_timer_wakeup(SLEEP_SECONDS * 1000000ULL);
  esp_deep_sleep_start();
}

void loop() {
  // never reached — the sketch deep-sleeps at the end of setup()
}

Built with FluxBench.AI — the AI workbench for embedded projects.