TiLDA MK3/adc

From EMF Badge
Revision as of 00:50, 2 March 2017 by Claude (talk | contribs) (fix broken link to micropython adc docs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

See here [1] for the main micropython documentation.

All ADC readings are referenced to the supply rail, which is about 3.3V. To increase the ADC accuracy, the internal voltage reference can be read, and used to offset variations in the supply rail.

adcin = pyb.ADC(channel_to_read).read()
ref_reading = pyb.ADC(0).read()
supply_voltage = 4095/ref_reading*1.21  # or change 1.21 with the calibrated value - see below
adc_voltage =  adcin / 4095 * supply_voltage

Note, the factory reads the internal reference with a supply voltage of 3.0V, and stores this reading. This can then be used to get the actual voltage reference. For example

import stm
factory_reading = stm.mem16[0x1FFF75AA]
reference_voltage = factory_reading/4095*3   # will be approximately 1.21V

For convenience, the library `onboard' provides the following functions for reading the battery voltage, unregulated voltage, or light level

import onboard
onboard.get_battery_voltage()
onboard.get_unreg_voltage()
onboard.get_light()