Home

How to Read from an Analog Input

The ATmega328 microcontroller on both the Arduino Uno and Nano boards contains a 10-bit ADC (Analog to Digital Converter). Reading an analog input returns an unsigned integer in the range 0 to 1023 steps, delivering 3 significant digits. The reference voltage and input span are both 0.0 to 5.0 volts nominal. The nominal step size is therefore 5.0 ÷ 1024 = 0.00488 volts or 4.88 millivolts per step. The absolute accuracy will depend on the accuracy of the +5V supply, which will have a tolerance of a few percent.

The Arduino Uno board has 5 analog inputs A0 to A5 that are shared with digital pins D14 to D19. Pins A4 and A5 are also shared with the on-board I2C bus controller and are often not usable as analog inputs. The Arduino Nano board has two additional analog inputs A6 and A7 that are not shared with anything else.

For some reason, the Arduino AVR core package defines identifiers for analog inputs A0 to A7 but not digital pins D0 to D19.

#define ADC_SCALE_FACTOR 0.00488; // 4.88 millivolts per step

void setup()
{
  Serial.begin(115200);
  Serial.println("Arduino ADC Test");
}

void loop()
{
  unsigned sample = analogRead(A0);
  double volts = sample*ADC_SCALE_FACTOR;
  Serial.println(volts);
  delay(1000);
}

Home


Questions or comments to phil@munts.net