-
Notifications
You must be signed in to change notification settings - Fork 2
/
adc_battery.py
24 lines (18 loc) · 1.1 KB
/
adc_battery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# see also https://gitlab.com/rcolistete/micropython-samples/-/blob/master/Pycom/Using_Expansion_Board_en.md#10-battery-voltage-reading-via-voltage-divider-and-vbatt-pin
import machine
# import statistics
NUM_ADC_READINGS = const(50)
adc = machine.ADC()
adc.vref(1126) # put here ESP32 VRef measured in mV with a voltmeter
# adcP16 = adc.channel(pin='P16', attn=machine.ADC.ATTN_6DB) # For Expansion Board v2.x
adcP16 = adc.channel(pin='P16', attn=machine.ADC.ATTN_11DB) # For Expansion Board v3.x
# samples_voltage = [0]*NUM_ADC_READINGS
# for i in range(NUM_ADC_READINGS):
# samples_voltage[i] = (171*adcP16.voltage())//56 # Expansion Board v2.x has voltage divider (115K + 56K) converting to integer voltage, [0, 6702] mV
samples_voltage[i] = (2*adcP16.voltage())//1 # Expansion Board v3.x has voltage divider (1M + 1M) converting to integer voltage, [0, 6600] mV
batt_mV = round(statistics.mean(samples_voltage))
batt_mV_error = round(statistics.stdev(samples_voltage))
import machine
adc = machine.ADC()
adcP16 = adc.channel(pin='P16', attn=machine.ADC.ATTN_2_5DB)
print(adcP16.voltage(), adcP16.value())