// Solar Tracker Using LDR & Servo Motor
// https://how2electronics.com/arduino-based-solar-tracker-using-ldr-servo-motor/

/*
Introduction:
In this project, we are going to show you how to make an Arduino Based Solar Tracker Using LDR & Servo Motor. The Solar Panel Tracker is 
designed to follow the sun movement so that maximum light intensity hits on the solar panel, thus increasing the power efficiency.

We have designed a single-axis solar tracking system. In this system, the whole solar panel moves from east to west in a day to point in the 
direction of the sun. The use of a solar tracker circuit in the field of energy production will increase its efficiency. This system can also 
be successfully implemented in other solar energy-based projects like water heaters and steam turbines.

Arduino Based Solar Tracker Using LDR & Servo Motor
Components Required:
Following are the list of components required to design a Solar Tracker

Arduino Uno Board
Servo Motor SG90
Tact Switch (Button)
Resistors 10K – 3 Nos
LDR – 2 Nos
Breadboard
Connecting Wires
5 to 12 Volt power Supply


Working of the Project:
Two LDR’s (Light Dependent Resistor) LDR1 & LDR2 are connected to Analog pins of the Arduino. A solar plate is attached in parallel to the axis 
of the servo motor and both the sensors are kept on the solar plate as shown in the figure above.

The design & the arrangement is done in such a manner that the movement of the sun is from LDR1 to LDR2, as shown in the image below.
There are three cases that are to be followed:-

Case 1: Sun is in the left side
Light on LDR1 is high because the shadow of barrier falls on LDR2 so solar plate moves clockwise.

Case 2: Sun is in right Side
Light on LDR2 is high because the shadow of barrier falls on LDR1 so solar plate movie anticlockwise.

Case 3: Sun is in the Center
Light on both LDR’s is equal so, plate will not rotate in any direction.

Source Code/Program
For designing Arduino Based Solar Tracker Using LDR & Servo Motor you need to program Atmega 328 Arduino microcontroller. Below is the program 
that will interface servo motor & LDR with Arduino for Solar Tracking. Copy this code and upload it to your Arduino Board.

*/

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <EEPROM.h>
#include <Servo.h>
 
Servo myservo;
 
int sensor1=A1;
int sensor2=A0 ;
int calswitch=2;
 
int val1;
int val2;
 
int pos=0;
int error;
int state;
 
void setup() {

lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Solar Tracker !!");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Get Max POWER !!");

pinMode(sensor1,INPUT);
pinMode(sensor2,INPUT);
pinMode(calswitch,INPUT);
 
myservo.attach(10);
}
 
void loop() {
if(digitalRead(calswitch)==0) {
myservo.detach();
 
val1=analogRead(sensor1);
val2=analogRead(sensor2);
 
if (val1>val2) {error=val1-val2; state=0; }
else {error=val2-val1; state=1; }
 
EEPROM.write(0,error);
EEPROM.write(1,state);
 
delay(1000);
}
 
else{
myservo.attach(10);
 
val1=analogRead(sensor1);
val2=analogRead(sensor2);
 
state=EEPROM.read(1);
error=EEPROM.read(0);
 
if(state==0) { val1=val1-error;}
else { val2=val2-error;}
 
if (val1-val2>4) {myservo.write(pos); pos=pos-1; delay(10);}
else if (val2-val1>4) {myservo.write(pos); pos=pos+1; delay(10);}
else {myservo.write(pos);}
 
if (pos>90) {pos=90;}
else if (pos<0) {pos=0;}
 
}
}