Circuit Arduino Solar Tracker
I have a standard servo that can rotate approximately 180 degrees (90° in each direction) and is controlled using the included Arduino’s Servo Library. The code is simple too and I’ll try to explain it after this video where I made a short presentation of the project in action. Unfortunately I had no solar panel at that moment.
#include
Servo myservo;
int pos = 90; // initial position
int sens1 = A0; // LRD 1 pin
int sens2 = A1; //LDR 2 pin
int tolerance = 2;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(sens1, INPUT);
pinMode(sens2, INPUT);
myservo.write(pos);
delay(2000); // a 2 seconds delay while we position the solar panel
}
void loop()
{
int val1 = analogRead(sens1); // read the value of sensor 1
int val2 = analogRead(sens2); // read the value of sensor 2
if((abs(val1 - val2) <= tolerance) || (abs(val2 - val1) <= tolerance)) { //do nothing if the difference between values is within the tolerance limit } else { if(val1 > val2)
{
pos = --pos;
}
if(val1 < val2) { pos = ++pos; } } if(pos > 180) { pos = 180; } // reset to 180 if it goes higher
if(pos < 0) { pos = 0; } // reset to 0 if it goes lower myservo.write(pos); // write the position to servo delay(50); }
In the setup() function we set the pins were the LDR are connected as INPUTs and position the servo motor at 90° then wait for a 2 seconds before the code execution inside the loop(). In the loop() we read the values received from our 2 sensors and adjust the solar panel based on these values.