Solar tracker project issue

Hi guys,

First time posting so hoping I explain it well enough as I am fairly new to Arduino myself. I am an educator trying to make some projects about alternative energy and the first project I am looking at is making a device that allows a solar panel to track with the sun throughout the day to maximise its output. I got this idea from the link below:

I followed all the steps of it and 3D printed a single axel that the servo sits on and rotates the solar panel based on the two variable light sensors. It tracks fine within the limits that I set but the idea is when it is dark is is supposed to reset back to the “east” limit but it instead it is rotating way past and smashing the stand where the solar panel is suppose to sit. I essentially need some help to see where I have gone wrong with my code and how I can get it to reset back to the east limit. As I said I am very new to Arduino so I am hoping someone with a more trained eye can assist me to see where I have gone wrong. Much appreciated for any assistance code is pasted below

#include <Servo.h>

Servo tracker; // create servo object to control a servo
int eastLDRPin = 0; //Assign analogue pins
int westLDRPin = 1;
int eastLDR = 0; //Create variables for the east and west sensor values
int westLDR = 0;
int error = 0;
int calibration = 15; //Calibration offset to set error to zero when both sensors receive an equal amount of light
int trackerPos = 85; //Create a variable to store the servo position

void setup()
{
tracker.attach(11); // attaches the servo on pin 11 to the servo object
// Turn the serial port on.
Serial.begin(115200);
}
void loop()
{
eastLDR = calibration + analogRead(eastLDRPin); //Read the value of each of the east and west sensors
westLDR = analogRead(westLDRPin);
if(eastLDR<350 && westLDR<350) //Check if both sensors detect very little light, night time
{
while(trackerPos=85) //Move the tracker all the way back to face east for sunrise
{
trackerPos++;
tracker.write(trackerPos);

  delay(100);
}

}
error = eastLDR - westLDR; //Determine the difference between the two sensors.
if(error>15) //If the error is positive and greater than 15 then move the tracker in the east direction
{
if(trackerPos<=159) //Check that the tracker is not at the end of its limit in the east direction
{
trackerPos++;
tracker.write(trackerPos); //Move the tracker to the east
}
}
else if(error<-15) //If the error is negative and less than -15 then move the tracker in the west direction
{
if(trackerPos>85) //Check that the tracker is not at the end of its limit in the west direction
{
trackerPos–;
tracker.write(trackerPos); //Move the tracker to the west
}
}
delay(100);
// Print the contents of the variable to the serial monitor.
Serial.println(trackerPos);
}

Hi Michael,

Cool project! Please use the “preformatted text” option when posting code so its easier to read. (its the </> button in the forum entry tool.

I think I can see some errors in the code, but to help you out effectively I just need a bit more info.

What is the servo position (1-180) in the desired eastmost and westmost position?
What is the reading of the LDR at the brightness when the sun has set and the panel needs to move back to the start?
You can find the LDR readings by adding:
Serial.println(eastLDR)
inside your loop.

1 Like

Hi Stephen,

Thanks for the quick reply. I wasn’t expecting one so quick. Here are your answers to your questions

What is the servo position (1-180) in the desired eastmost and westmost position? East =155 and West= 85

What is the reading of the LDR at the brightness when the sun has set and the panel needs to move back to the start? <150

Attached is the latest code below. I selected the preformatted text option before pasting it so I hope it is easier to read this time.

#include <Servo.h> 
 
Servo tracker;  // create servo object to control a servo 
int eastLDRPin = 0;  //Assign analogue pins
int westLDRPin = 1;
int eastLDR = 0;   //Create variables for the east and west sensor values
int westLDR = 0;
int error = 0;
int calibration = 15;  //Calibration offset to set error to zero when both sensors receive an equal amount of light
int trackerPos = 85;    //Create a variable to store the servo position
 
void setup() 
{ 
  tracker.attach(11);  // attaches the servo on pin 11 to the servo object
 // Turn the serial port on.
Serial.begin(115200);
}
void loop() 
{
  eastLDR = calibration + analogRead(eastLDRPin);    //Read the value of each of the east and west sensors
  westLDR = analogRead(westLDRPin);
  if(eastLDR<150 && westLDR<150)  //Check if both sensors detect very little light, night time
  {
    while(trackerPos=155)  //Move the tracker all the way back to face east for sunrise
    {
      trackerPos++;
      tracker.write(trackerPos);
 
      delay(100);
    }
  }
  error = eastLDR - westLDR;          //Determine the difference between the two sensors.
  if(error>15)        //If the error is positive and greater than 15 then move the tracker in the east direction
  {
    if(trackerPos<=155)  //Check that the tracker is not at the end of its limit in the east direction
    {
      trackerPos++;
      tracker.write(trackerPos);  //Move the tracker to the east
    }
  }
  else if(error<-15)  //If the error is negative and less than -15 then move the tracker in the west direction
  {
    if(trackerPos>85)  //Check that the tracker is not at the end of its limit in the west direction
    {
      trackerPos--;
      tracker.write(trackerPos);  //Move the tracker to the west
    }
     Serial.println(eastLDR);
  }
  delay(100);
       // Print the contents of the variable to the serial monitor.
    
}

Hi Stephen, I wasn’t sure when I posted an hour ago whether you could get notified so I have replied to your comment as well to see if you get notified. Thanks!

Hi Michael,

I suspect that this is the issue right here:

 if(eastLDR<150 && westLDR<150)  //Check if both sensors detect very little light, night time
  {
    while(trackerPos=155)  //Move the tracker all the way back to face east for sunrise
    {
      trackerPos++;
      tracker.write(trackerPos);
 
      delay(100);
    }
  }

It seems that this while loop is incorrect, and will currently only move the panel back to the East if the panel is already in the Eastmost position. Try this:

 if(eastLDR<150 && westLDR<150)  //Check if both sensors detect very little light, night time
  {
    while(trackerPos != 155)  //Move the tracker all the way back to face east for sunrise
    {
      trackerPos++;
      tracker.write(trackerPos);
 
      delay(100);
    }
  }

or you could probably simplify it to just this:

 if(eastLDR<150 && westLDR<150)  //Check if both sensors detect very little light, night time
  {
    trackerPos = 155;
    tracker.write(trackerPos);
 
    delay(100);
  }

Let me know if that works for you!

1 Like