From e8a564b1cf1d1c91c7aba7f6ba39dc16c9d4b9ae Mon Sep 17 00:00:00 2001 From: Arthur Lutz Date: Wed, 17 Jun 2026 12:33:49 +0200 Subject: [PATCH] feat(xiao_s3_wio): add battery read with voltage divider --- variants/xiao_s3_wio/XiaoS3WIOBoard.h | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/variants/xiao_s3_wio/XiaoS3WIOBoard.h b/variants/xiao_s3_wio/XiaoS3WIOBoard.h index 7ae06a359b..98283c2c91 100644 --- a/variants/xiao_s3_wio/XiaoS3WIOBoard.h +++ b/variants/xiao_s3_wio/XiaoS3WIOBoard.h @@ -3,6 +3,25 @@ #include #include +/* + * This board has no built-in way to read battery voltage. + * Nevertheless it's very easy to make it work, you only require two 1% resistors. + * If your using the WIO SX1262 Addon for xaio, make sure you dont connect D1! + * + * BAT+ -----+ + * | + * VSYS --+ -/\/\/\/\- --+ + * 200k | + * +-- D1 + * | + * GND --+ -/\/\/\/\- --+ + * | 100k + * BAT- -----+ + */ +#define PIN_VBAT_READ 2 // D1 +#define BATTERY_SAMPLES 8 +#define ADC_MULTIPLIER (3.0f * 3.3f * 1000) + class XiaoS3WIOBoard : public ESP32Board { public: XiaoS3WIOBoard() { } @@ -10,4 +29,21 @@ class XiaoS3WIOBoard : public ESP32Board { const char* getManufacturerName() const override { return "Xiao S3 WIO"; } + + uint16_t getBattMilliVolts() override { +#if defined(PIN_VBAT_READ) && defined(ADC_MULTIPLIER) + analogReadResolution(12); + + uint32_t raw = 0; + for (int i = 0; i < BATTERY_SAMPLES; i++) { + raw += analogRead(PIN_VBAT_READ); + } + raw = raw / BATTERY_SAMPLES; + + return (ADC_MULTIPLIER * raw) / 4096; +#else + return 0; +#endif + } + };