TiLDA MK3/adc: Difference between revisions
Jump to navigation
Jump to search
(Created page with "See here [https://micropython.org/doc/module/pyb/ADC] for the main micropython documentation. All ADC readings are referenced to the supply rail, which is about 3.3V. To incr...") |
No edit summary |
||
Line 6: | Line 6: | ||
adcin = pyb.ADC(channel_to_read).read() | adcin = pyb.ADC(channel_to_read).read() | ||
ref_reading = pyb.ADC(0).read() | ref_reading = pyb.ADC(0).read() | ||
supply_voltage = 4095/ref_reading*1.21 # or change 1.21 with the calibrated value - | supply_voltage = 4095/ref_reading*1.21 # or change 1.21 with the calibrated value - see below | ||
adc_voltage = adcin / 4095 * supply_voltage | adc_voltage = adcin / 4095 * supply_voltage | ||
</pre> | </pre> | ||
Line 14: | Line 14: | ||
import stm | import stm | ||
factory_reading = stm.mem16[0x1FFF75AA] | factory_reading = stm.mem16[0x1FFF75AA] | ||
reference_voltage = factory_reading/4095*3 | reference_voltage = factory_reading/4095*3 # will be approximately 1.21V | ||
</pre> | |||
For convenience, the library `onboard' provides the following functions for reading the battery voltage, unregulated voltage, or light level | |||
<pre> | |||
import onboard | |||
onboard.get_battery_voltage() | |||
onboard.get_unreg_voltage() | |||
onboard.get_light() | |||
</pre> | </pre> |
Revision as of 12:20, 3 August 2016
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()