Skip to content

Commit 09bb6bd

Browse files
committed
Added API call to return ADC reading in volts, scaled by configurable system ADC reference voltage.
1 parent c8ab263 commit 09bb6bd

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

drivers/AnalogIn.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ class AnalogIn {
9393
*/
9494
unsigned short read_u16();
9595

96+
/** Read the input voltage in volts. The output depends on the target board's
97+
* ADC reference voltage (typically equal to supply voltage). The ADC reference voltage
98+
* sets the maximum voltage the ADC can quantify (ie: Vin == Vref when ADC output == ADC_MAX_VALUE)
99+
*
100+
* The target's ADC reference voltage can be configured by overriding "drivers.adc_vref"
101+
*
102+
* @returns A floating-point value representing the current input voltage, measured in volts.
103+
*/
104+
float read_volts();
105+
96106
/** An operator shorthand for read()
97107
*
98108
* The float() operator can be used as a shorthand for read() to simplify common code sequences

drivers/mbed_lib.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
"qspi_csn": {
4242
"help": "QSPI chip select pin",
4343
"value": "QSPI_FLASH1_CSN"
44-
}
44+
},
45+
"adc_vref": {
46+
"help": "Reference voltage for ADC (float)",
47+
"value": 3.3f
4548
}
4649
}

drivers/source/AnalogIn.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ unsigned short AnalogIn::read_u16()
5454
return ret;
5555
}
5656

57+
float AnalogIn::read_volts() {
58+
float ret = this->read();
59+
return (ret*MBED_CONF_DRIVERS_ADC_VREF);
60+
}
61+
5762
} // namespace mbed
5863

5964
#endif

0 commit comments

Comments
 (0)