From 5665ccff52319a80a0ede6db5f85a37929d06af0 Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Thu, 2 Aug 2018 00:11:57 +0200 Subject: [PATCH] Add function to measure Vbat with some simple filter (mean of n samples, 10 by default) --- circuitpython/main.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/circuitpython/main.py b/circuitpython/main.py index a0660f5..de15027 100644 --- a/circuitpython/main.py +++ b/circuitpython/main.py @@ -68,15 +68,10 @@ class Data: #Data from Feather board self.data['SYS'] = {'time': {'val': datetime_format.format(*clock.datetime[0:6]), 'unit': '' }, - 'v_bat': {'val': vbat.value*0.000100708, + 'v_bat': {'val': measure_vbat(), 'unit': 'V' }, 'CPU_temp': {'val': microcontroller.cpu.temperature, 'unit': '°C' }} - # Note about v_bat calculation : - # 0.000100708 = 2*3.3/65536 with - # 2 : voltage is divided by 2 - # 3.3 : Vref = 3.3V - # 65536 : 16bit ADC #Data from BME280 self.data['BME280'] = {'temp': { 'val': round(bme280.temperature, 1), 'unit': '°C' }, @@ -197,6 +192,20 @@ def set_clock_from_GPS(): clock.datetime = gps_datetime #Trust GPS if there is a bias print("New date/time : " + datetime_format.format(*clock.datetime[0:6])) +def measure_vbat(samples=10, timestep=0.01): + """Measure Vbattery as the mean of n samples with timestep second between + each measurement""" + # Note about v_bat calculation : + # 0.000100708 = 2*3.3/65536 with + # 2 : voltage is divided by 2 + # 3.3 : Vref = 3.3V + # 65536 : 16bit ADC + v = 0 + for i in range(samples): + v = v + vbat.value + time.sleep(timestep) + return v/samples*0.000100708 + ######### # Setup # #########