Most microcontrollers have a peripheral on them called an Analog to Digital Converter (ADC). This is a very useful feature that converts a voltage (analog) on a pin to a number (digital) that can be manipulated in a program. This is especially helpful because there are many sensors that report their values by setting a voltage to a certain level. By reading the datasheet for the sensor we can determine what the voltage value actually means in ‘real-world’ units. There’s still a missing link though. The ADC will convert the voltage to a number that we can use in our program, but in order to decipher what that number means we have to know what voltage that number is representing. Basically, we have to convert the number back to a voltage.
In order to figure out how to make this calculation we first need to know two numbers that are dependent on the system configuration: the voltage that the system is running on (usually 3.3V or 5V) and the maximum range that the ADC number can report. The maximum range for the ADC is dictated by the ADC size, and is usually defined as a number of bits (8-bit, 10-bit, 12-bit, etc). The Arduino, for example, has a 10-bit ADC. The maximum range for a 10-bit ADC is 1024 (2 to the power of 10). The actual numbers in the range are 0-1023, but the important part for this conversation is that there are 1024 discrete numbers.
Once we know these two numbers it’s pretty easy to calculate how the numbers relate. The only thing to keep in mind is that the voltages are usually represented in millivolts (mV). Here’s the equation that describes how the ADC value is created:
or
*Assuming a 10-bit ADC on a 3.3V system
These equations are OK, but they could use a little help. When the ‘analogRead(pin)’ command is used in Arduino it gets the ADC value. We want to use the ADC value to find the voltage on the pin we’re reading. With a little bit of algebra we can change the equation to look like this:
or
*Assuming a 10-bit ADC on a 3.3V system
Much Better!
For example let's say that we attach a temperature sensor to an ADC pin on a 5V system that has a 10-bit ADC. If the ADC reading is 377, what is the voltage the temperature sensor is giving us?
Plug those numbers into a calculator and we find that the corresponding voltage is 1840mV, or 1.84V.
If the voltage on an ADC pin is 2.2V, what would the reported ADC value be? Assume a 10-bit ADC on a 3.3V system.