Skip to content

Instantly share code, notes, and snippets.

@marschr
Last active September 10, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marschr/eedc36d457dc8cea2e8423f90508c4df to your computer and use it in GitHub Desktop.
Save marschr/eedc36d457dc8cea2e8423f90508c4df to your computer and use it in GitHub Desktop.
Arduino internal VRef (Atmega 328/328P based board only)
//Code from: https://code.google.com/archive/p/tinkerit/wikis/SecretVoltmeter.wiki
#include <Arduino.h>
//Reads internal Arduino VRef
long readVcc() { long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2);
// Wait for Vref to settle
ADCSRA |= _BV(ADSC);
// Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result;
// Back-calculate AVcc in mV
return result;
}
void setup() {
Serial.begin(57600);
}
void loop() {
Serial.println(readVcc(), DEC );
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment