May 15, 2021 Arduino
In this chapter, we'll look at some advanced input and output functions.
Configure the reference voltage (that is, the value used as the top of the input range) for analog inputs. The option is:
DEFAULT - Default analog reference value for 5 volts (5V Arduino board) or 3.3 volts (3.3V Arduino board).
INTERNAL - Built-in reference equals 1.1 volts on ATmega168 or ATmega328 and 2.56 volts on ATmega8 (not applicable to Arduino Mega)
INTERNAL1V1 - Built-in 1.1V reference (Arduino Mega only)
INTERNAL2V56 - Built-in 2.56V reference (Arduino Mega only)
EXTERNAL - The voltage applied to the AREF pin (0 to 5V only) is used as a reference
analogReference (type);
type - Can use any of the following types (DEFAULT, INTERNAL, INTERNAL1V1, INTERNAL2V56, EXTERNAL)
Do not use any value less than 0V or greater than 5V for the external reference voltage of the AREF pin. /b10> If an external reference is used on the AREF pin, the analog reference must be set to EXTERNAL before the analogRead() function is called. /b11> Otherwise, the valid reference voltage (internally generated) and AREF pins will be shorted, which may damage the microcontroller on the Arduino board.
Alternatively, you can connect the external reference voltage to the AREF pin with a 5K resistor, allowing switching between the external and internal reference voltages.
Note that the resistor changes the voltage used as a reference because there is an internal 32K resistor on the AREF pin. /b10> Both are used as voltage splits. /b11> For example, 2.5V applied through a resistor will generate a voltage of 2.5 x 32/ (32 x 5) at the AREF pin.
Example
int analogPin = 3;// potentiometer wiper (middle terminal) connected to analog pin 3 int val = 0; // variable to store the read value void setup() { Serial.begin(9600); // setup serial analogReference(EXTERNAL); // the voltage applied to the AREF pin (0 to 5V only) // is used as the reference. } void loop() { val = analogRead(analogPin); // read the input pin Serial.println(val); // debug value }