1. Home
2. Knowledge Base
3. Tutorials
4. Arduino

While digitally reading if a pin is high or low is great, it’s not very helpful with an analog sensor. In this case, we need to know the voltage of the pin. There is a device built into most microcontrollers called an analog-to-digital converter (ADC) that makes this possible. The ADC compares the voltage on the pin with a reference voltage and outputs a value corresponding to the voltage.

## Resolution

Inside the ADC, the full voltage range of the ADC is divided into small increments and the input voltage is compared to each incremental voltage to determine its value on the full range. The number of increments depends on the resolution of the ADC. The resolution of an ADC is measured in bits. For example, a 10-bit ADC has 210 – 1 (1023) increments of measurement.

## Voltage Reference

Some fancy ADCs actually let you provide a Vref- which allows you to narrow your measurement range, even more. This further increases the accuracy of the ADC reading.

## Arduino IDE

To use the function with the default settings, simply call the analogRead() function with the analog pin number as the argument and it will return the value of the voltage of the pin as a numerical value from 0-1023; 1023 being equal to Vcc.

### Converting the Reading to a Voltage

The reading is then converted into a voltage by multiplying the output by the reference voltage and dividing that by the full ADC output range of 1023.

### Changing the Reference Voltage

Arduino boards actually have several options for the reference voltage. The input voltage to the MCU is the default reference used. This is typically 5V or 3.3V, depending on your Arduino board. There are also fixed internal reference voltages, depending on the Arduino board that you are using. The Uno board has an internal 1.1V reference, while the Mega board has a 1.1V and a 2.65V reference. Finally, the ADC can also be set to use an external reference voltage on the AREF pin. The voltage supplied to the AREF pin can be anywhere from 0V up to the supply voltage of the MCU. (Never exceed 5v!)

The ADC reference is changed with the command analogReference() with the argument DEFAULT, EXTERNAL, INTERNAL, INTERAL1V1 (Mega Only), or INTERNAL2V56 (Mega Only).

When you change the voltage reference on an Arduino board, it seems to take a while for it to actually switch. The times I have switched the reference I “burn out” the old reference by reading the ADC a lot of times (50 – 150). This seems to work.

Here are some useful equations for working with ADCs, where NADC is the value returned from the ADC.

$latex N_{ADC} = \frac{V \; – \; V_{ref-}}{V_{ref+} \; – \; V_{ref-}} \cdot (2^{Bits of Resolution} – 1)&s=2$

$latex V = (V_{ref+} \; – \; V_{ref-}) \cdot \frac{N_{ADC}}{2^{Bits of Resolution} \; – \; 1} \; + \; V_{ref-}&s=2$

$latex Resolution = V_{1 bit} = \frac{V_{ref+} \; – \; V_{ref-}}{2^{Bits of Resolution} \; – \; 1}&s=2$

Example Calculations

$latex {Bits \; of \; Resolution} = 10 &s=1$

$latex V = 3.3V, \quad V_{ref+} = 5V, \quad V_{ref-} = 0V&s=1$

$latex N_{ADC} = \frac{V \; – \; V_{ref-}}{V_{ref+} \; – \; V_{ref-}} \cdot (2^{Bits of Resolution} – 1) = \frac{3.3V \\ – \\ 0V}{5.0V \\ – \\ 0V} \cdot 1023 = 675&s=1$

$latex V = (V_{ref+} – V_{ref-}) \cdot \frac{N_{ADC}}{2^{Bits of Resolution} \; – \; 1} + V_{ref-} = (5.0V – 0V) \cdot \frac{675}{1023} + 0V = 3.3V&s=1$

$latex Resolution = V_{1 bit} = \frac{V_{ref+} \; – \; V_{ref-}}{2^{Bits of Resolution} \; – \; 1} = \frac{5.0V – 0V}{1023} = 4.89{mV}&s=1$