/*
 Reads the internal voltage on an Arduino with an Atmega328
 or later (Duemilanove, Uno, etc)

 Based on an example by Peter Knight, at

http://code.google.com/p/tinkerit/wiki/Voltmeter

 Created 6 Feb 2012

 */



void setup() {
  Serial.begin(9600);
}

void loop() {
  float voltage = readVcc() / 1000.0;
  Serial.println( voltage );
  delay(1000);
}

 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;
}