Arduino Programming Language: AnalogReference

Name

analogReference()

Description

The analogReference() method sets the mode for assigning the voltage used as reference for the analogRead() command, this value will be the maximum voltage used as reference.
The options are:

  • DEFAULT: the default analog reference are 5V or 3.3V;
  • INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 and Atmega32u4;
  • INTERNAL1V1: built-in 1.1V reference;
  • INTERNAL2V56: built-in 2.56V reference;
  • EXTERNAL: use as reference the voltage applied to AREF pin in the range 0-5V only.


The function “analogReference(ref)” sets voltage ref for A/D converter on M0 and M0PRO and requires one parameter (ref).
Possible value for ref are:

  • AR_DEFAULT: In this case Vref is VDDana, because VDDana is 3.3 V than Vref is 3.3 V.
  • AR_INTERNAL: In this case Vref=1 V
  • AR_EXTERNAL: Vref is variable according to the voltage applied on VREF pin available on the board. Pay attention to not exceed VDDana-0,6 V (3,3-0,6=2,7 V) on the VREF pin because ATSAMD21G18A doesn't tolerate voltages above the value written before, as reported on datasheet.

 
The function “analogReference(ref)” sets voltage ref for A/D converter on Arduino Primo and requires one parameter (ref). 
Possible value for ref are:

  • DEFAULT: In this case Vref is 3.3 V.
  • INTERNAL: In this case Vref is 3 V.
  • INTERNAL3V6: In this case Vref is 3 V.

Syntax

analogReference(mode)

Parameters

mode can be any of:

  • DEFAULT
  • INTERNAL
  • INTERNAL1V1
  • INTERNAL2V56
  • EXTERNAL

for M0/M0Pro:

  • AR_DEFAULT
  • AR_INTERNAL
  • AR_EXTERNAL

for Arduino Primo:

  • DEFAULT
  • INTERNAL
  • INTERNAL3V6

Returns

none 

Usage

Applications

Note

After changing the analog reference, the first few readings from analogRead() could be inaccurate.

Warning

Do NOT apply to AREF any voltage out of the 0-5V range 
If you're using an external reference on the AREF pin, you must set the analog reference to EXTERNAL before calling analogRead().

If the AREF pin is connected to an external source do not use the other reference voltage options in the application, as they will be shorted to the external voltage and cause permanent damage to the microcontroller in the board.

Examples

int inpin = 0;
int val = 0;

void setup() {
// set the analog reference as built-in 2.56Volts
analogReference(INTERNAL2V56);
Serial.begin(9600);
}

void loop() {
val = analogRead(inpin); // read the value of analog pin 0
Serial.println(val); // write the value to the serial port
}


Based on the reference originally written by Arduino Community,  wiring.org.co and  arduino.cc