Difference between revisions of "TiLDA MK3/adc"

From EMF Badge
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...")
 
(fix broken link to micropython adc docs)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
See here [https://micropython.org/doc/module/pyb/ADC] for the main micropython documentation.
+
See here [https://docs.micropython.org/en/latest/pyboard/library/pyb.ADC.html] 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.
 
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.
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 - se below
+
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>

Latest revision as of 00:50, 2 March 2017

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()