View Single Post
Old 12-02-2013, 06:03 AM   #263 (permalink)
Quezacotl
Scandinavian creature
 
Join Date: Jun 2012
Location: Finland
Posts: 146

Golf ball - '94 Volkswagen Golf III
90 day: 28.46 mpg (US)
Thanks: 4
Thanked 27 Times in 22 Posts
//Voltage reading (circuit = 12V---22k---VoltagePin---10k---GND )

#define voltagePin A1 // put this on definitions and change the pin to what you use

then this into sub-programs (where all other similar things are):

unsigned long batteryvoltage(void){
float vout = 0.0;
float vin = 0.0;
float R1 = 21550.0; // 22k
float R2 = 9095.0; // and 10k are just fine
int value = 0;
value = analogRead(voltagePin);
vout = (value * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));
return vin * 1000;
}

and add the voltage things to the menus the exact same way than everything else. The program name is "batteryvoltage" as you see.

This code is not mine, but i've used it successfully. It has 2% accuracy if you apply it unchanged, and 12V is after safety diode. Of course you can tweak it to your needs, whick is recommended.
__________________

Brrrmm
Now selling preassembled MPGuino's!
http://www.mthtek.net/mpguino/
  Reply With Quote
The Following 2 Users Say Thank You to Quezacotl For This Useful Post:
josemapiro (12-02-2013), t vago (12-02-2013)