Skip to content

Commit 568b3f5

Browse files
committed
Use framework getBatteryVoltage() for reliable battery reading
Replace manual single-sample ADC reading with the framework's getBatteryVoltage() which averages 50 analogReadmV() samples and handles VBAT_ADC_CTL correctly. Fixes inaccurate/noisy battery voltage values.
1 parent 7b8b660 commit 568b3f5

1 file changed

Lines changed: 4 additions & 24 deletions

File tree

src/mesh/Telemetry.h

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,33 +90,13 @@ class TelemetryManager {
9090
}
9191

9292
/**
93-
* Read battery voltage.
94-
* Framework 1.6.0 bugs: analogReadmV()/getBatteryVoltage() broken
95-
* (ch3 calibration not initialized).
96-
* Workaround: analogRead() + ch0 calibration + VBAT_ADC_CTL.
93+
* Read battery voltage using framework getBatteryVoltage().
94+
* Uses analogReadmV() with 50-sample averaging + VBAT_ADC_CTL.
9795
*/
9896
void readBattery() {
9997
#ifdef CUBECELL
100-
extern volatile int16 ADC_SAR_Seq_offset[];
101-
extern volatile int32 ADC_SAR_Seq_countsPer10Volt[];
102-
103-
// Enable VBAT measurement circuit
104-
pinMode(VBAT_ADC_CTL, OUTPUT);
105-
digitalWrite(VBAT_ADC_CTL, LOW);
106-
delay(100);
107-
108-
uint16_t counts = analogRead(ADC);
109-
110-
pinMode(VBAT_ADC_CTL, INPUT);
111-
112-
// Convert using ch0 calibration, apply x2 voltage divider
113-
int32_t gain = ADC_SAR_Seq_countsPer10Volt[0];
114-
if (gain != 0) {
115-
int32_t adj = (int32_t)counts - ADC_SAR_Seq_offset[0];
116-
data.batteryMv = (uint16_t)((adj * 20000L) / gain);
117-
} else {
118-
data.batteryMv = 0;
119-
}
98+
extern uint16_t getBatteryVoltage(void);
99+
data.batteryMv = getBatteryVoltage();
120100
// Apply ADC multiplier if set (tenths: 10=1.0x, 0=auto/skip)
121101
extern uint8_t configAdcMultiplier;
122102
if (configAdcMultiplier > 0 && configAdcMultiplier != 10) {

0 commit comments

Comments
 (0)