diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index 9ff9ec7ee3..0b04b5c1ac 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -93,6 +93,16 @@ public: */ unsigned short read_u16(); + /** Read the input voltage in volts. The output depends on the target board's + * ADC reference voltage (typically equal to supply voltage). The ADC reference voltage + * sets the maximum voltage the ADC can quantify (ie: Vin == Vref when ADC output == ADC_MAX_VALUE) + * + * The target's ADC reference voltage can be configured by overriding "drivers.adc_vref" + * + * @returns A floating-point value representing the current input voltage, measured in volts. + */ + float read_volts(); + /** An operator shorthand for read() * * The float() operator can be used as a shorthand for read() to simplify common code sequences diff --git a/drivers/mbed_lib.json b/drivers/mbed_lib.json index 614baa955e..8caeebad2c 100644 --- a/drivers/mbed_lib.json +++ b/drivers/mbed_lib.json @@ -41,6 +41,9 @@ "qspi_csn": { "help": "QSPI chip select pin", "value": "QSPI_FLASH1_CSN" - } + }, + "adc_vref": { + "help": "Reference voltage for ADC (float)", + "value": 3.3f } } diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp index 63d910c227..084930f4ec 100644 --- a/drivers/source/AnalogIn.cpp +++ b/drivers/source/AnalogIn.cpp @@ -54,6 +54,11 @@ unsigned short AnalogIn::read_u16() return ret; } +float AnalogIn::read_volts() { + float ret = this->read(); + return (ret*MBED_CONF_DRIVERS_ADC_VREF); +} + } // namespace mbed #endif