The reference: DEFAULT, INTERNAL, AREF
Every analogRead count is a share of a reference voltage. On this board the default is the 5 V rail, which moves with your USB port; INTERNAL is a 1.1 V bandgap that does not; EXTERNAL is the AREF pin, and a voltage on AREF with the wrong setting shorts two references together inside the chip.
The top of the ruler
analogRead does not measure volts. It measures what share of the
reference the input is, in 1024 steps. Change the reference and the same
input gives a different count, so the sketch has to know which one it used
before it can turn the count back into volts.
analogReference() picks one of three:
| Setting | The reference is | Range | One step |
|---|---|---|---|
DEFAULT | AVCC, which on this board is the 5 V rail | 0 to 5 V | 4.88 mV |
INTERNAL | the chip's bandgap, 1.0 to 1.2 V, 1.1 typical | 0 to 1.1 V | 1.07 mV |
EXTERNAL | whatever is on the AREF pin, 1.0 V up to the supply | 0 to AREF | AREF ÷ 1024 |
The call itself only stores the choice. The Arduino core writes it to the
chip at the next analogRead, so the switch happens there.
One volt in, three answers. DEFAULT gives about 204 on a 5.00 V rail, INTERNAL about 930, and the DEFAULT answer itself moves by 20 counts or so across the range of voltages a USB port is allowed to give. Nothing on A0 changed: the top of the ruler did.
DEFAULT moves with your USB port
On USB, the 5 V rail is the port's 5 V after a resettable fuse and a switching transistor, which cost a little. USB allows a port to give 4.75 to 5.25 V, so a laptop, a hub and a phone charger can each give a slightly different rail, and every DEFAULT reading shifts with it. For a sensor that gives a share of the same 5 V, such as a knob between 5V and GND, that is harmless: both ends move together. For a voltage that comes from somewhere else, it is an error, and the next article shows how to measure it.
AREF stays empty in this book
The AREF hole on the top strip is wired to the chip's reference pin and to a 100 nF capacitor to ground, C14, and nothing else. The capacitor steadies whichever reference is chosen; the chip itself puts that reference on AREF.
That is why a voltage on AREF is dangerous. With DEFAULT or INTERNAL
selected, the chip is driving AREF from inside, and the datasheet (section
23.5.2) warns that the other references "will be shorted to the external
voltage". Arduino's rule is the same: a sketch that uses AREF must call
analogReference(EXTERNAL) before its first analogRead, every time. Then
the next sketch you upload, which reads with DEFAULT, connects the 5 V rail
to whatever is on AREF.
The only other voltage on this board is 3V3, and wiring it to AREF works only as long as every sketch remembers. This book leaves AREF empty.
Throw the first reading away
The datasheet says the first conversion after switching reference "may be inaccurate", and advises discarding it. The capacitor on AREF also has to move to the new voltage. The sketch above reads once, waits 20 ms, and reads again; it prints both so you can see whether your chip's first one is off.
The code
Wire A0 to 3V3, upload, and watch the same 3.3 V read twice a second against DEFAULT and against INTERNAL. Each line also shows the first reading after the switch, which the sketch throws away.
// Reference_Compare.ino — read A0 against DEFAULT and INTERNAL
// Tools > Board: Arduino Uno, or Arduino Nano with
// Processor ATmega328P (Old Bootloader if the upload says
// "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: one jumper wire from A0 to 3V3. A0 is an input.
// Nothing on AREF: this board has only a 100 nF capacitor there.
void setup() {
Serial.begin(9600);
}
// Switch reference, keep the first reading to show it, wait for
// the capacitor on AREF to settle, then read again.
void readWith(uint8_t ref, const char *name, float vref) {
analogReference(ref); // takes effect at the next read
int first = analogRead(A0); // datasheet: may be inaccurate
delay(20);
int n = analogRead(A0);
Serial.print(name);
Serial.print(": first ");
Serial.print(first);
Serial.print(", settled ");
Serial.print(n);
Serial.print(" = ");
Serial.print(n * vref / 1024.0, 3);
Serial.println(n >= 1023 ? " V or more" : " V");
}
void loop() {
readWith(DEFAULT, "DEFAULT (5 V rail)", 5.0);
readWith(INTERNAL, "INTERNAL (1.1 V) ", 1.1);
Serial.println();
delay(1000);
}DEFAULT should give about 675 on a 5.00 V rail, and more when the rail is lower: a little on the UNO, about 720 on a Nano on USB, whose rail sits behind a diode. INTERNAL gives 1023: its ruler stops at 1.1 V, so 3.3 V is off the end. Plug the board into a different USB port or a phone charger and the DEFAULT number moves; INTERNAL is for small voltages that need to stay put.
When it does not work
DEFAULT measures against the 5 V rail, and the rail is whatever the USB port gives, less what the board's USB path costs: a little across the UNO's fuse and switch, a few tenths of a volt across the Nano's diode. A port may sit anywhere from 4.75 to 5.25 V and still be within the USB specification. Use INTERNAL for small voltages, or measure the rail and correct for it.
The datasheet says the first conversion after changing the reference may be inaccurate, and advises throwing it away. The 100 nF capacitor on AREF also has to charge or discharge to the new reference. Read once, wait a few milliseconds, then read for real.
The internal reference is 1.1 V, so anything at or above about 1.1 V is the top of the scale. INTERNAL is for small signals, like a sensor that gives a few hundred millivolts. For 0 to 5 V, go back to DEFAULT.
The bandgap is 1.0 to 1.2 V by the datasheet, so the same input can read up to about 10% off on any one chip. It is steady on any one chip, so a single calibration against a meter fixes it for that board.
Read the 1.1 V bandgap against the 5 V rail, and the rail falls out of the sum.
Measure the board's own supply →Edit this page — content/books/atmega328p/the-reference.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.