From b8a4c27daae7ddb6e25714e96e4c9217e7c49a47 Mon Sep 17 00:00:00 2001 From: acidburns Date: Mon, 2 Feb 2026 23:28:54 +0100 Subject: [PATCH] Average battery ADC samples - Read battery 5 times and average for a steadier voltage estimate --- README.md | 2 +- src/power_manager.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d394147..f507527 100644 --- a/README.md +++ b/README.md @@ -261,7 +261,7 @@ inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = { 0xF19C }; - 4.2 V = 100% - Uses deep sleep between cycles (`SENDER_WAKE_INTERVAL_SEC`). - Sender CPU is throttled to 80 MHz and LoRa RX is only enabled in short windows (ACK wait or time-sync). -- Battery sampling uses a single ADC read and updates at most once per `BATTERY_SAMPLE_INTERVAL_MS` (default 60s). +- Battery sampling averages 5 ADC reads and updates at most once per `BATTERY_SAMPLE_INTERVAL_MS` (default 60s). ## Web UI - AP SSID: `DD3-Bridge-` (prefix configurable) diff --git a/src/power_manager.cpp b/src/power_manager.cpp index 4b2e44a..2b599fd 100644 --- a/src/power_manager.cpp +++ b/src/power_manager.cpp @@ -34,8 +34,12 @@ void power_configure_unused_pins_sender() { } void read_battery(MeterData &data) { - uint32_t raw = analogRead(PIN_BAT_ADC); - float v = (static_cast(raw) / 4095.0f) * ADC_REF_V * BATTERY_DIVIDER * BATTERY_CAL; + uint32_t sum = 0; + for (uint8_t i = 0; i < 5; ++i) { + sum += analogRead(PIN_BAT_ADC); + } + float avg = static_cast(sum) / 5.0f; + float v = (avg / 4095.0f) * ADC_REF_V * BATTERY_DIVIDER * BATTERY_CAL; data.battery_voltage_v = v; data.battery_percent = battery_percent_from_voltage(v);