Because the output of a solar panel is dependent on the weather, you can’t always count on it to keep the battery fully charged. So it might be a good idea to have the Arduino monitor the battery’s voltage. This would enable the system to turn itself off if the battery gets too low.
You can easily use the AnalogRead function to measure a voltage. Unfortunately, it can only measure voltages up to 5V. So you need to make a voltage divider to reduce the voltage to something that can be measured. To make one, you will need two resistors. One of them should be twice the value of the other. Connect one lead of the larger resistor to the 12V+ line from the battery. Connect the other lead of the larger resistor to one of analog pins in input mode. Also connect one of the leads of the smaller resistor to this same analog pin. Then connect the other lead of the smaller resistor to the negative terminal of the battery or the GND pin of the Arduino. Then you can use the AnalogRead function to measure a voltage as an integer between 0 and 1023. To convert this into volts you can use the formula: V=AnalogRead*0.0049*RLarger/RSmaller. Here is an example of how you could use this setup to monitor the battery’s voltage and turn off the fountain when the battery gets low. The chosen resistors are 10K and 4.7K.
int RelayPin = 13; // relay driver connected to digital pin 13
int analogPin = 3; // Center of voltage divided connected to analog pin 3
int val = 0; // variable to store the value read
void setup()
{
pinMode(RelayPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
val = analogRead(analogPin); // read the input pin
if (val 785) //if the battery’s voltage is greater than 12
//with the chosen resistors of 10K and 4.7K, the voltage divider turns 12V into 3.8364V
//this is read by the analogRead function as 785
{
digitalWrite(RelayPin, HIGH); // turns the fountain on if the battery’s voltage is greater than 12V
}
delay(60000); // waits for one minute
}