Arduino Code For Solar Tracking

Library and Variables

#include <Servo.h>

This includes the Servo library, which provides the functionality to control Servo motors.

Servo servohori;
int servoh=0;
int servohLimitHigh=160;
int servohLimitLow=20;

Here, we're defining a Servo object (servohori) and three integer variables. servoh will store the current horizontal position of the servo. servohLimitHigh and servohLimitLow define the upper and lower limits of the servo's movement.

The same definitions are made for the vertical servo:

Servo servoverti;
int servov=0;
int servovLimitHigh=160;
int servovLimitLow=20;

Next, we define the pins to which our Light Dependent Resistors (LDRs) are connected:

int ldrtopl=2;
int ldrtopr=1;
int ldrbotl=3;
int ldrbotr=0;

Setup

void setup()
{
  servohori.attach(10);
  servohori.write(0);
  servoverti.attach(9);
  servoverti.write(0);
  delay(500);
}

In the setup() function, we attach our Servo objects to the appropriate pins on our Arduino (servohori to pin 10 and servoverti to pin 9). We then set the initial position of each servo to 0 degrees. A short delay of 500 milliseconds is added to allow the servos to reach their initial positions.

Loop

void loop()
{
  servoh=servohori.read();
  servov=servoverti.read();
  
  int topl=analogRead(ldrtopl);
  int topr=analogRead(ldrtopr);
  int botl=analogRead(ldrbotl);
  int botr=analogRead(ldrbotr);

In the loop() function, the first two lines read the current position of the servos. Next, the light intensity at each LDR is read using analogRead().

  int avgtop=(topl+topr)/2;
  int avgbot=(botl+botr)/2;
  int avgleft=(topl+botl)/2;
  int avgright=(topr+botr)/2;

These lines calculate the average light intensity for the top, bottom, left, and right pairs of LDRs.

The next block of code is responsible for controlling the vertical servo (servoverti). If the light intensity is higher at the top, it tries to increment the servo position. If it's higher at the bottom, it tries to decrement the position. If the light intensity is equal at the top and bottom, the servo position remains the same.

  if(avgtop < avgbot)
  {
    if(servov < servovLimitHigh)
    {
      servoverti.write(servov + 1);
      servov++;
    }
    delay(10);
  }
  else if(avgbot < avgtop)
  {
    if(servov > servovLimitLow)
    {
      servoverti.write(servov - 1);
      servov--;
    }
    delay(10);
  }
  else
  {
    servoverti.write(servov);
  }

Similarly, the next block of code controls the horizontal servo (servohori). If

the light intensity is higher on the left, it tries to increment the servo position. If it's higher on the right, it tries to decrement the position. If the light intensity is equal on the left and right, the servo position remains the same.

  if(avgleft > avgright)
  {
    if(servoh < servohLimitHigh)
    {
      servohori.write(servoh + 1);
      servoh++;
    }
    delay(10);
  }
  else if(avgright > avgleft)
  {
    if(servoh > servohLimitLow)
    {
      servohori.write(servoh - 1);
      servoh--;
    }
    delay(10);
  }
  else
  {
    servohori.write(servoh);
  }
  delay(50);
}

The delay(50) at the end of the loop provides a small pause before the loop starts again, allowing the servos time to move to their new positions before they're potentially updated again.

Remember, in the conditional blocks that control servo movement, we first check if the new position would be within limits. If it is, then we change the position. This protects your servos from attempting to move beyond their set limits.

Hi!
If you looking for help, you forgot to ask a question.
If this a program review, it would be better to start with short description. Also the code have to be inserted to single code block to make it downloading more convient.

1 Like

This thread has no question. It would be better suited in a tutorial or exposition section.

Along with the code a single block, a schematic would be helpful.

1 Like

Sorry for not adding questions, but this is mine:

What is the interpretation of this:

void loop()
{
  servoh=servohori.read();
  servov=servoverti.read();
  //capturing analog values of each LDR
  int topl=analogRead(ldrtopl);
  int topr=analogRead(ldrtopr);
  int botl=analogRead(ldrbotl);
  int botr=analogRead(ldrbotr);
  //calculating average
  int avgtop=(topl+topr)/2;
  int avgbot=(botl+botr)/2;
  int avgleft=(topl+botl)/2;
  int avgright=(topr+botr)/2;

  if(avgtop<avgbot)
  {
    servoverti.write(servov +1);
    if(servov>servovLimitHigh)
    {
      servov=servovLimitHigh;
    }
    delay(10);
  }

   else if(avgbot<avgtop)
  {
    servoverti.write(servov -1);
    if(servov<servovLimitLow)
    {
      servov=servovLimitLow;
    }
    delay(10);
  }

  else
  {
    servoverti.write(servov);
  }

   if(avgleft>avgright)
  {
    servohori.write(servoh +1);
    if(servoh>servohLimitHigh)
    {
      servoh=servohLimitHigh;
    }
    delay(10);
  }

  else if(avgright>avgleft)
  {
    servohori.write(servoh -1);
    if(servoh<servohLimitLow)
    {
      servoh=servohLimitLow;
    }
    delay(10);
  }

  else
  {
    servohori.write(servoh);
  }
  delay(50);
}

You might ask the author of the sketch.

See post #1. Or be more specific with the request. Is there a bit, in particular, that you need help with?

It looks like it's doing standard sun tracking using four LDRs. It uses their readings to move the servos a little at a time to find maximum sunlight.

Hi, @accessduino

Haven't you already explained it in your breakdown in post #1?

You have just done some editing in the direction making decision of the code.

Does your code work?

Tom... :smiley: :+1: :coffee: :australia:

I did not put this here to confront the OP. I put it here for a guy who arrived here by using the right search term, to point that guy in the right direction.

this is not solar tracking. this is bright light tracking. if you have a cloudy day, and the clouds dissipate, will the tracker be pointed at the sun? this serves no purpose and solves no problem.

the position of the sun is a function of location and time. it does not require tracking, it requires pointing, that which is called solar tracking is a form of clock. you point the object that needs to see the sun at where the sun is predicted to be, and if the weather permits, you are looking directly at the sun.

if you want to "track" the sun, find a sun tracking or ephemeris library, and find an example program that uses that ephemeris library.

1 Like

Never understood why "solar tracker" builders don't make a one axis tracker with the rotation axis pointed at the celestial pole and rotated 1 degree every 4 minutes. The celestial pole is due North or South (depending on hemisphere) and degrees of local latitude above a level (horizontal) line. The declination (vertical axis) could be adjusted manually every week or two as the seasons change. Or that could be automated fairly simply as it only needs to swing about 47 degrees over a years time.

HI,
[soapbox]
A "tracker" on a PV system, tracks the part of the sky that has the highest radiation that produces the most energy from say a PV system.
If there are clouds in the sky, the area that has the most radiation is found, this may not be where the sun is positioned.
For example, a cloud front approaches and obscures the sun, not all the sky is cloudy and there may be and sometimes is more radiation from the clear part.
If this system is being condemned, why do so many solar arrays use it.


I have been a looked at these two arrays, both use solar radiation detectors, "trackers".
[soapbox]

Tom.. :smiley: :+1: :coffee: :australia:
PS. I drive pass them everyday to work.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.