I am trying to do voltage measurement using Arduino. I am using a voltage divider circuit to reduce the voltage to less than 5V. I measured the voltage on the 5V pin of Arduino and found out that its 4.279 V (I am powering through USB). So, I am doing following calculations for voltage measurement:
int data1 = analogRead(A0);
float data2 = data1 * (4.279/1023.0);
float volts = data2 * (10 + 2.2)/2.2; //R1=10k , R2=2.2k
Serial.println(volts);
if(volts > 12.00)
{
digitalWrite(6,HIGH);
}
if(volts < 12.00)
{
digitalWrite(6,LOW);
}
So I am getting exact values of voltage. Now I have put a condition like if volt is greater than 12V, turn on an output, else turn off the output. For output, I have a transistor and mosfet connected. As soon as the voltage increases above 12V, output get on but the problem is that when the output is on, Arduino displays wrong values. Like if the voltage is 12.3, it shows 13.6.
I looked it into more and found out that the voltage at 5V pin of Arduino (4.279
) gets reduced to 3.865
and due to which calculation goes wrong and it displays wrong values of voltage.
Is there anything which I am doing wrong? Is there any function or keyword in Arduino libraries which can tell us the voltage or is there any way through which we can measure it? I tried putting the 5V pin to analog pins to read it and convert it into volts but again for this we need a reference voltage. I hope I have explained the question clearly.
Schematic
A0
is the analog pin and PIN6
is digital output of arduino