Skip to content

Commit b9154be

Browse files
battery_voltage: read ADC 10 times
1 parent 51716d3 commit b9154be

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

internal_filesystem/lib/mpos/battery_voltage.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ def read_battery_voltage():
2121
import random
2222
return random.randint(370,420) / 100
2323
# Read raw ADC value
24-
raw_value = adc.read()
24+
total = 0
25+
# Read multiple times to try to reduce variability.
26+
# Reading 10 times takes around 3ms so it's fine...
27+
for _ in range(10):
28+
total = total + adc.read()
29+
raw_value = total / 10
2530
# Convert to voltage, accounting for divider and reference
2631
voltage = (raw_value / ADC_MAX) * VREF * VOLTAGE_DIVIDER
2732
# Clamp to 0–4.2V range for LiPo battery

0 commit comments

Comments
 (0)