Mbed OS Reference
Loading...
Searching...
No Matches
AnalogIn Class Reference

An analog input, used for reading the voltage on a pin. More...

#include <AnalogIn.h>

Public Member Functions

 AnalogIn (const PinMap &pinmap, float vref=MBED_CONF_TARGET_DEFAULT_ADC_VREF)
 Create an AnalogIn, connected to the specified pin. More...
 
 AnalogIn (PinName pin, float vref=MBED_CONF_TARGET_DEFAULT_ADC_VREF)
 Create an AnalogIn, connected to the specified pin. More...
 
float read ()
 Read the input voltage, represented as a float in the range [0.0, 1.0]. More...
 
unsigned short read_u16 ()
 Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]. More...
 
float read_voltage ()
 Read the input voltage in volts. More...
 
void set_reference_voltage (float vref)
 Sets this AnalogIn instance's reference voltage. More...
 
float get_reference_voltage () const
 Gets this AnalogIn instance's reference voltage. More...
 
 operator float ()
 An operator shorthand for read() More...
 

Detailed Description

An analog input, used for reading the voltage on a pin.

Note
Synchronization level: Thread safe

Example:

// Print messages when the AnalogIn is greater than 50%
#include "mbed.h"
AnalogIn temperature(p20);
int main() {
while(1) {
if(temperature > 0.5) {
printf("Too hot! (%f)", temperature.read());
}
}
}
An analog input, used for reading the voltage on a pin.
Definition: AnalogIn.h:68

Definition at line 68 of file AnalogIn.h.

Constructor & Destructor Documentation

◆ AnalogIn() [1/2]

AnalogIn ( const PinMap pinmap,
float  vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF 
)

Create an AnalogIn, connected to the specified pin.

Parameters
pinmapreference to structure which holds static pinmap.
vref(optional) Reference voltage of this AnalogIn instance (defaults to target.default-adc-vref).
Note
An input voltage at or above the given vref value will produce a 1.0 result when read is called

◆ AnalogIn() [2/2]

AnalogIn ( PinName  pin,
float  vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF 
)

Create an AnalogIn, connected to the specified pin.

Parameters
pinAnalogIn pin to connect to
vref(optional) Reference voltage of this AnalogIn instance (defaults to target.default-adc-vref).
Note
An input voltage at or above the given vref value will produce a 1.0 result when read is called

Member Function Documentation

◆ read()

float read ( )

Read the input voltage, represented as a float in the range [0.0, 1.0].

Returns
A floating-point value representing the current input voltage, measured as a percentage

◆ read_u16()

unsigned short read_u16 ( )

Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF].

Returns
16-bit unsigned short representing the current input voltage, normalized to a 16-bit value

◆ read_voltage()

float read_voltage ( )

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: ADC output == ADC_MAX_VALUE when Vin == Vref)

The target's default ADC reference voltage is determined by the configuration option target.default-adc_vref. The reference voltage for a particular input can be manually specified by either the constructor or AnalogIn::set_reference_voltage.

Returns
A floating-point value representing the current input voltage, measured in volts.

◆ set_reference_voltage()

void set_reference_voltage ( float  vref)

Sets this AnalogIn instance's reference voltage.

The AnalogIn's reference voltage is used to scale the output when calling AnalogIn::read_volts

Parameters
[in]vrefNew ADC reference voltage for this AnalogIn instance.

◆ get_reference_voltage()

float get_reference_voltage ( ) const

Gets this AnalogIn instance's reference voltage.

Returns
A floating-point value representing this AnalogIn's reference voltage, measured in volts.

◆ operator float()

operator float ( )

An operator shorthand for read()

The float() operator can be used as a shorthand for read() to simplify common code sequences

Example:

float x = volume.read();
float x = volume;
if(volume.read() > 0.25) { ... }
if(volume > 0.25) { ... }

Definition at line 146 of file AnalogIn.h.