I am making a solar charge controller using Arduino. So for starting, I thought I should calibrate the voltage so that I am able to read the exact voltage of battery and solar charger. For testing purposes, I do not have any solar panel, so I used a 12V power supply and set its output voltage to 13.56. I used the following calculations for getting exact voltage:
float solarData = 0;
float batData = 0;
float batScale = 0;
float solarScale = 0;
float batPinVolt = 0;
float solPinVolt = 0;
float batVolt = 0;
float solVolt = 0;
Vcc = readVcc()/1000.00; //getting the exact voltage of arduino
Serial.print(Vcc);
solarScale = analogRead(A0); //reading solar analog data
batScale = analogRead(A1); //reading battery analog data
solPinVolt = (solarScale/1024.0) * Vcc;
solVolt = solPinVolt * (9.80 + 2.168)/2.168; //I used 10k & 2.2k so entered their exact value
batPinVolt = (batScale/1024.0) * Vcc;
batVolt = batPinVolt * (9.80 + 2.168)/2.168;
Initially, I faced few problem regarding the exact voltage of the Arduino which I used as a reference voltage to calculate voltage. But then I found a solution here. So below is the code which calculate the voltage of Arduino
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 = 1125300L / result; // Back-calculate AVcc in mV
return result;
}
Now when I uploaded the code, I got the exact values of the voltages.
Now I wrote a logic where if the battery voltage is less than 12V then start charging until it is above 13V. Following is the code:
if((solVolt > batVolt)&& ( batVolt <= 12.00 ))
{
digitalWrite(chargePin,HIGH); //enable charging
digitalWrite(loadPin,LOW); //load off
}
else if((solVolt > batVolt)&&(batVolt > 12.00)&& (batVolt <= 13.00 ))
{
digitalWrite(chargePin,HIGH); //enable chargin
digitalWrite(loadPin,LOW); //load off
}
// // shut down when battery is fully charged or when sunlight is not enough
else if ((batVolt > 13.00) or (solVolt < batVolt))
{
digitalWrite(chargePin,LOW); //disable charging
digitalWrite(loadPin,HIGH); //load on
}
But when I uploaded the code, the Arduino started showing wrong values of the voltages. Power supply was set to 13.5, but it showed 13.15 and battery voltage was 11.8, but it showed 12.7.
Now due to this what exactly happened was it started charging so the value of battery voltage 12.7 (which is wrong) started increasing. When it was above 13V, Arduino turned off the chargePin
. So when the chargePin
is low, it shows the exact battery voltage which is low so it again started charging it and this loop keeps on going. This is all because of the wrong values of voltages. Following is the schematic:
I am following the article I found on Instructables. (cannot post more links due to reputation). I want to know why there is change in voltage. Can anyone please help me on what I am doing wrong.