Go Down

Topic: Clever low voltage monitor (no external components) (Read 618 times) previous topic - next topic


Jan 28, 2013, 11:15 am Last Edit: Jan 28, 2013, 11:27 am by hotshotharry Reason: 1
I found this clever little snippet of code that will return the supply voltage for the arduino using no external components! Would be great for those lipo projects !!!



Jan 28, 2013, 11:17 am Last Edit: Jan 28, 2013, 11:27 am by hotshotharry Reason: 1
Just in case the link vanishes I have copied the code here

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() {

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


Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

shortcuts: alt+s submit/post or alt+p preview