Reading battery supply voltage without a voltage divider?

EKMallon

  • Full Member
  • ***
    • Posts: 27
    • View Profile
    • The Cave Pearl Project
Just stumbled across this little trick, and it seems to work reasonably well on the Tiny duino (although it seems to report about 0.6 volts lower than an external voltmeter directly on the supply).  I will try to compare this to a voltage divider later, but for now I just thought someone else might find this handy for low voltage warnings, etc. I have not tried the temperature trick yet.

from:  http://forum.arduino.cc/index.php/topic,15629.0.html

"There is a little trick using that internal 1.1v reference Grumpy Mike talks about. I'm not sure exactly how they measure the Vcc (or probably AVcc) without any external voltage divider. http://code.google.com/p/tinkerit/wiki/SecretVoltmeter

This only works for the atmega 168 or 328.

Copy, paste into Arduino and see what it returns. This works on an Arduino 168 or 328.
Code: [Select]
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(9600);
}

void loop() {
  Serial.println( readVcc(), DEC );
  delay(1000);
}

There's also a similar method of measuring the temperature: http://code.google.com/p/tinkerit/wiki/SecretThermometer"
« Last Edit: December 12, 2013, 04:59:05 AM by calvinthedestroyer »


wafel

  • Full Member
  • ***
    • Posts: 12
    • View Profile
Wow that's just great buddy,
you saved my project with these readings!

How accurate are the Voltage readings ?


EKMallon

  • Full Member
  • ***
    • Posts: 27
    • View Profile
    • The Cave Pearl Project
I have not yet had time to put this one under real scrutiny, but I am hoping to use it to detect low voltage on long term datalogger project.  At the moment, I have only tested it down to about 3.8 volts, and seen a bit of noise in the readings - the flop around by 200 mV ish, but as my project is pretty rough I dont yet know if this is due to the routine, or its simply my cheep AA battery holders giving me grief in a humid environment.

Apparently this code has been floating around for quite a while, so you might be able to find out how reliable the internal 1.1 v ref via google. If you do, please post the extra info here so others can use it...



Josephur

  • Full Member
  • ***
    • Posts: 14
    • View Profile
    • Stack-Tech
This is a great piece of code that I did not know existed as well, good post!


 

SMF spam blocked by CleanTalk