Measure the board's own supply
The board can measure its own 5 V rail with no wire at all: read the chip's fixed 1.1 V bandgap against the rail, and the rail is 1.1 × 1024 divided by the count. It is steady to a few hundredths of a volt and, until you calibrate it once, up to about 10% off.
Turn the trick round
analogRead measures a pin against the 5 V rail. If the rail is the thing
you do not know, measure something you do know against it instead. The chip
has such a thing inside: the bandgap, a reference of 1.1 V that does not
depend on the supply.
The ADC can read the bandgap as if it were a pin. Measured against the rail, it gives count = 1.1 × 1024 ÷ rail, so the rail is 1.1 × 1024 ÷ count.
The smaller the rail, the bigger the share 1.1 V takes of it, so the count goes up as the supply sags: 225 at 5.00 V, 237 at 4.75 V. Turning it back gives the rail to about 22 mV, one count's worth.
The other control is the catch. The datasheet gives the bandgap as 1.0 to 1.2 V. A sketch that assumes 1.1 on a chip whose bandgap is really 1.0 reports every supply about 10% high, and one at 1.2 V about 8% low. The error is the same every time on that chip, so the changes are right even before the number is.
Two register writes Arduino does not offer
There is no Arduino function for this, so the sketch sets the converter's multiplexer register, ADMUX, itself:
// REFS1:0 = 01, AVCC reference. MUX3:0 = 1110, the 1.1 V bandgap.
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);The two REFS bits choose AVCC, the 5 V rail, as the reference (table 23-3
in the datasheet). The four MUX bits, 1110, choose the input called
"1.1V (VBG)" in table 23-4. Then it starts a conversion by setting ADSC and
waits for the chip to clear it, which is exactly what analogRead does
inside.
The datasheet warns that the bandgap needs time to settle as an input and a reading taken too soon may be wrong. The sketch waits 2 ms and converts twice, keeping the second.
What to compare
Run it from each USB source you have. A laptop port, a powered hub, a phone charger and a long thin cable each give a slightly different number, and a board that misbehaves on one of them often shows a low rail here first. A USB port is allowed to give 4.75 to 5.25 V, and the rail is less than the port by what the board's USB path costs: a little, across the 0.5 A fuse and the switching transistor. Your board's own book shows that path.
Calibrate once, if you own a meter
Measure between the 5V and GND holes with a multimeter while the sketch runs. Then replace 1.1 with 1.1 × meter ÷ printed. If the meter says 4.92 V and the sketch said 5.05 V, the constant becomes 1.072, and that number belongs to this chip, not to the next board.
Without a meter, keep 1.1 and trust the differences: a drop of 0.2 V when a motor starts is a real drop of 0.2 V, give or take a tenth of it.
The code
No wiring. It points the ADC at the bandgap, measures it against AVCC, and prints the rail once a second. Try it on a laptop port, a powered hub and a phone charger.
// Read_Own_Vcc.ino — the board measures its own 5 V rail
// 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: none. The ADC reads the chip's 1.1 V bandgap against
// AVCC, which on this board is the 5 V rail.
// Typical is 1.1. To calibrate: measure 5V to GND with a meter,
// then set BANDGAP = 1.1 * meter / printed.
const float BANDGAP = 1.1;
int readBandgap() {
// REFS1:0 = 01, AVCC reference. MUX3:0 = 1110, the 1.1 V bandgap.
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // let the input settle
ADCSRA |= _BV(ADSC); // start a conversion
while (bit_is_set(ADCSRA, ADSC)) {}
ADCSRA |= _BV(ADSC); // and a second: keep this one
while (bit_is_set(ADCSRA, ADSC)) {}
return ADC;
}
void setup() {
Serial.begin(9600);
}
void loop() {
int n = readBandgap();
float vcc = BANDGAP * 1024.0 / n;
Serial.print("bandgap reads ");
Serial.print(n);
Serial.print(" -> 5 V rail = ");
Serial.print(vcc, 3);
Serial.println(" V");
delay(1000);
}Around 225 means about 5.0 V; a higher count means a lower rail. A Nano on USB reads nearer 240, about 4.7 V, because its USB power comes through a diode. The sketch writes ADMUX itself, so the first analogRead after it changes the reference back and that one reading may be off: discard it.
When it does not work
Your chip's bandgap is not exactly 1.1 V; the datasheet allows 1.0 to 1.2. Every reading from this chip is off by the same percentage. Measure 5V to GND with a multimeter once and set BANDGAP to 1.1 × meter ÷ printed.
The bandgap needs time to settle as an ADC input, and the datasheet warns that a reading taken too soon may be wrong. The sketch waits 2 ms and throws one conversion away; if your first line is still off, ignore it.
readBandgap() leaves ADMUX on the bandgap channel and the AVCC reference. analogRead rewrites both, but the first reading after a change of channel or reference may be inaccurate. Read once and discard it.
The bandgap reads about 225 at 5 V, and one count there is about 22 mV of supply. That is the resolution of the trick. It is plenty to see a weak port, a long cable or a sagging battery, not to calibrate a meter.
Turn a count into the units you want, and read voltages bigger than 5 V.
map() and voltage dividers →Edit this page — content/books/atmega328p/measure-your-own-supply.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.