10 bit AtoD returning integers from 0 to 1023.

Read Analog Input

There is no need to set the pin up as an analog input, just start reading it:

  int MyAnalogValue = analogRead(A3);    //Read 10-bit (0 to 1023) analog input value (causes stall for approx 100uS)

Setting The Analog Reference Voltage

Use the analogReference() function;

DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)
INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)
INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)
EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference. 

Using Analog Pins as GPIO

The analog pins also have all the functionality of general purpose input/output (GPIO) pins (the same as digital pins 0 – 13).

Use the aliases A0 (for analog input 0), A1, etc. E.g this would set analog pin 0 to an output, and to set it HIGH:

pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);

Special Arduinos

Arduino Due, Zero and MKR Family

Use analogReadResolution() to set the resolution you want to use.

Example function with averaging of sampled values

//****************************************
//****************************************
//********** READ ANALOG INPUTS **********
//****************************************
//****************************************
void read_analog_inputs (void)
{
  static uint8_t AnalogInputSampleCount = 0;
  static uint16_t AnalogInputA2Buffer = 0;
  
  
  //Called every 10mS

  AnalogInputA2Buffer += analogRead(A2);    //Read 10-bit (0 to 1023) analog input value (causes stall for approx 100uS)
  AnalogInputSampleCount++;

  if (AnalogInputSampleCount >= 8)
  {
    //----- SAMPLE PERIOD COMPLETE -----
  
    //----- INPUT A2 -----
    //1V = 1A
    //0-1023 @ 5V VCC = 204.6 = 1A
    //0.2046 = 1mA
    CurrentSense_mA = (AnalogInputA2Buffer >> 3) / 0.2046;    // >> 3 for /8

    //Reset for next buffer sample 
    AnalogInputA2Buffer = 0;
    AnalogInputSampleCount = 0;
  }


#ifdef __DEBUG
  static uint8_t DebugOutputCount = 200;

  DebugOutputCount--;
  if (DebugOutputCount == 0)
  {
    DebugOutputCount = 200;

    Serial.print("Current sense: ");
    Serial.print(CurrentSense_mA, DEC);
    Serial.println("mA");
  }
#endif

}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *