I've followed advice on the web on how to use "bandgap" voltage reference to measure supply voltage, however, the measurements are not as accurate as the ADC can be on an Arduino Mega.
On the one hand, I've measured the 1.1V ref to actually be 1.087V using a DMM to measure the voltage across a capacitor from AREF to GND with the appropriate code.
On the other hand, measuring the BG internally with the ADC using the attached code returns an ADC average reading of 226.5 which is 1.107V with a 5V supply (measured using a DMM from the 5V pin). This is a 20mV error. I've being getting about 5mV accuracy when using the ADC to measure an external voltage source.
The Arduino Mega is powered via USB.
An external supply of 1.087V was simultaneously (alternately) measured with the analog pin yielded a different reading. What is the difference between the two passes?
#define N 128 #define ND 128.0D int analogReadBG(){ uint8_t low, high; ADMUX = 0; bitClear(ADCSRB,MUX5); // Set ADC input as BG (1.1V) bitSet(ADMUX,MUX4); bitSet(ADMUX,MUX3); bitSet(ADMUX,MUX2); bitSet(ADMUX,MUX1); bitSet(ADMUX,REFS0); // Set 5V as ADC reference voltage for (int i=0;i<7;i++){ // Perform several converstions for reading to settle bitSet(ADCSRA, ADSC); while (bit_is_set(ADCSRA, ADSC)); } low = ADCL; high = ADCH; return (high << 8) | low; } void setup() { Serial.begin(115200); } long movingSum; double movingAverage; void loop() { movingSum = 0; for (int i=0;i<N;i++){ movingSum += analogReadBG(); } movingAverage = movingSum/ND; Serial.println(movingAverage); delay(1000); }