Skip to content

Commit 429f821

Browse files
authored
tweak getBattMilliVolts to report battery more accurately
1 parent c0a51af commit 429f821

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

variants/thinknode_m2/ThinknodeM2Board.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@ void ThinknodeM2Board::begin() {
1919
enterDeepSleep(0);
2020
}
2121

22-
uint16_t ThinknodeM2Board::getBattMilliVolts() {
22+
uint16_t ThinknodeM2Board::getBattMilliVolts() {
2323
analogReadResolution(12);
24-
delay(10);
25-
float volts = (analogRead(PIN_VBAT_READ) * ADC_MULTIPLIER * AREF_VOLTAGE) / 4096;
26-
analogReadResolution(10);
27-
return volts * 1000;
28-
}
24+
analogSetPinAttenuation(PIN_VBAT_READ, ADC_11db);
25+
26+
uint32_t mv = 0;
27+
for (int i = 0; i < 8; ++i) {
28+
mv += analogReadMilliVolts(PIN_VBAT_READ);
29+
delayMicroseconds(200);
30+
}
31+
mv /= 8;
32+
33+
analogReadResolution(10);
34+
return static_cast<uint16_t>(mv * ADC_MULTIPLIER );
35+
}
2936

3037
const char* ThinknodeM2Board::getManufacturerName() const {
3138
return "Elecrow ThinkNode M2";
32-
}
39+
}

0 commit comments

Comments
 (0)