View Single Post
Old 03-31-2011, 05:54 PM   #18 (permalink)
meelis11
EcoModding Apprentice
 
meelis11's Avatar
 
Join Date: Feb 2009
Location: Estonia
Posts: 199

Green frog - '97 Audi A4 Avant 1.9TDI 81kW
Diesel
90 day: 43.1 mpg (US)
Thanks: 19
Thanked 40 Times in 28 Posts
Here is my working function:
Voltage divider ratio could be bigger but I had these resistors and it is working.
* correct R1 and R2 value to resistor values you use. For calibration, you can fine-tune these numbers
* you also must define voltagePin with correct pin number


Code:
#define voltagePin   2

unsigned long batteryVoltage(void){
   float vout = 0.0;
   float vin = 0.0;
   float R1 = 21500.0;    // !! resistance of R1 !!
   float R2 = 9500.0;     // !! resistance of R2 !!
   int value = 0;

   value = analogRead(voltagePin);
   vout = (value * 5.0) / 1024.0;
   vin = vout / (R2/(R1+R2));

   return vin * 1000;
}
Schematic is like this.
12V...R1...VoltagePin...R2...GND
so it means R1 is between 12V and VoltagePin, R2 is between VoltagePin and GND

Last edited by meelis11; 03-31-2011 at 06:02 PM..
  Reply With Quote