Go Down

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

hotshotharry

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 !!!

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

hotshotharry

#1
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() {
 Serial.begin(9600);
}

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

dhenry


Go Up