Mapping Arduino Analog-to-Digital Converter (ADC) Output to Voltage

The Arduino Uno is a digital device, but it has an analog-to-digital converter (ADC) to allow us to probe the analog signals that permeating our analog world. The ADC is a 10-bit device that can map an analog signal consisting of a voltage ranging from 0 to 5 volts to a decimal value between 0 and 1023 (210-1). The sketch below prototypes how to take the decimal value supplied by the ADC and map it back to voltage. Conceptually, the idea of mapping a number from one range to another involves selecting the number on the destination range that has the same location relative to the end points as the original number had to the end points on the original range. If that sentence makes no sense to you, let me give a simple example. If I gave you the number 7 on the range 0 to 10 and told you to map the 7 to a number on the range 0 to 1000, you should pick 700 because 700 has the same position relative to 1000 as 7 has relative to 10. In other words, 7 is to 10 as 700 is to 1000. With a little thought, you can convince yourself that if both the original and destination ranges start at zero, then the mapping is accomplished as follows.

newNumber = orignialNumber*maxValueOnNewRange/maxValueOnOriginalRange

The following is the prototype Arduino sketch promised above.

int analogValue; // a place to hold the decimal value produced by 
// the ADC
float analogVolts; // a place to hold the analogValue mapped back 
// to voltage
byte analogPin = 5; // the analog pin connected to the analog 
// signal we wish to read (pin A5)

void setup() {
// nothing

void loop() {
analogValue = analogRead(analogPin);
analogVolts = (float)analogValue*5/1023; // must convert int to float 
// to perform floating point math

Leave a Reply

Your email address will not be published.