I'm an advanced beginner and for my home solar system, I'm planning to automatically disconnect and reconnect again the load (DC-AC inverter) from the solar battery, as soon as the battery voltage droped (for a certain time period, e.g. 2 min) under a certain voltage, respectively has risen again (for a certain time period, e.g. 2 min) over a certain value.
In easier words: The load should be switched off, when the battery voltage has droped persistently (e.g. for a minimum of 2 min) under 12 V and the load should be switched on again, when the battery voltage has persistently risen again (due to solar charging) above 12.8 V.
Regarding the hardware I'm fine and I think of a relais, a volt meter and of course an Arduino/ESP32.
What I'm looking for, are any ideas for a nice Arduino code. Does anyone have already realized a similar project or can help me with good ideas?
A good MPPT controller will automatically disconnect the load from the battery when the Vbatt get to xVbatt. xVbatt is user selectable.
I found the most useful thing to monitor and plot is vBatt. I noticed that my vBatt's were taking longer to charge over a few weeks. A mop and water took care of that issue.
Sounds like you are using the Arduino IDE. I'd suggest checking the the voltage (scaled down with a potential divider using two resistors to be sure to be below 3.3V into the ESP32) every second, using delay(1000); between checking values and if this is done on a loop with 120 cycles you can check if your voltage has dropped below 12V for all the measurements. Something like:
for (n = 0;n<120;n++){
volts = analogRead(Vpin);
volts = volts * scale_factor //where scale_ factor converts your reading 0 to 3.3 V to the actual value
if (volts > 12){break;}
if (n > 118){digital_write(relay_pin,LOW);}
Relay between the +ve rail and the collector of a transistor turned on by the relay_pin via a resistor into the base of the transistor
Thanks for the idea. However I cannot use the voltage controlled load output of the solar charger, for it supplies not enough power. The inverter has a voltage control too, unfortunatly it allows only switch on-off settings in 1 volt steps, which is not precise enough.
Just an update for those, who might face the same problem with an off-grid solar system.
I'm using the sketch below to switch off the inverter when it has reached a certain discharge voltage, in my case 24.7 V. As soon as the inverter is switched off, the battery voltage will rise again (because of the switched off inverter) by approx. 0.3 to 0.6 volts (depending on the power, the inverter has provided before it's shut down). After a defined time (120000 ms in my case) the inverter is switched on again, IF the battery voltage is 25.3 Volt or more (because of the switched off inverter). Then there are two possibilities:
Inverter NOW has to supply a lower power as before the shut down --> the battery voltage is not dropping to 24.7 V again (because of the lower power demand of the inverter) and the inverter stays on, as long as the battery voltage again drops to 24.7 volts (due to an again higher power demand or the ongoing discharging of the battery)
Inverter has to supply the same power as before the shut down --> the battery voltage immediately drops again to 24.7 V or less and the inverter is switched off again for the next 120000 ms.
Sketch:
int autoinverterSwitch = 4; //Declare autoinvSwitch an int, and set to pin 4
long Timer = 0; //variable 1 - start value for time loop
long Timeout = 120000; //variable 2 - duration of time loop in ms
void setup()
{
pinMode(autoinverterSwitch, OUTPUT),//Switch pin is an output pin
digitalWrite(autoinverterSwitch, HIGH);//Set Switch pin to HIGH as default
}
void loop()
{
//automatic ON_OFF switch at threshold voltages
if (voltageCloud <= 24.70)
{
digitalWrite(autoinverterSwitch, LOW); //Set autoinverterSwitch to HIGH (OFF)
}
if (millis() > Timeout + Timer ) //time loop logic
{
Timer = millis(); //time loop logic
if (voltageCloud >= 25.30)
{
digitalWrite(autoinverterSwitch, HIGH); //Set autoinverterSwitch (again) to LOW (ON)
}
}
Remark: The autoinverterSwitch PIN is controling a relais with an NO (normally on) circuit.