Last active
September 10, 2019 17:04
-
-
Save marschr/eedc36d457dc8cea2e8423f90508c4df to your computer and use it in GitHub Desktop.
Arduino internal VRef (Atmega 328/328P based board only)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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