Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Start streaming on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!
robotistan
Published

Solar Tracking System Using Arduino

The system will follow the light source and catch the energy with its solar panel.

IntermediateFull instructions provided2 hours2,993
Solar Tracking System Using Arduino

Things used in this project

Hardware components

Robotistan's Solar Tracker Kit
×1

Story

Read more

Schematics

Solar Tracker Manual Book

You can find all instructions and schematics in this document.

Code

Solar Tracking System Arduino Codes

C/C++
#include <Servo.h>
//defining Servoss

#define TOLERANCE       10
#define STEP_DELAY      7
Servo servohori;
int servoh = 0;
int servohLimitHigh = 160;
int servohLimitLow = 20;

Servo servoverti;
int servov = 0;
int servovLimitHigh = 160;
int servovLimitLow = 20;
//Assigning LDRs
int ldrtopl = A0; //top left LDR green
int ldrtopr = A3; //top right LDR yellow
int ldrbotl = A1; // bottom left LDR blue
int ldrbotr = A2; // bottom right LDR orange

void setup ()
{
  servohori.attach(10);
  servohori.write(45);
  servoverti.attach(9);
  servoverti.write(45);
  Serial.begin(9600);
  delay(500);
}

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; //average of top LDRs
  int avgbot = (botl + botr) / 2; //average of bottom LDRs
  int avgleft = (topl + botl) / 2; //average of left LDRs
  int avgright = (topr + botr) / 2; //average of right LDRs
  Serial.println(avgtop);

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

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

  else
  {
    servoverti.write(servov);
  }

  if (avgleft - avgright > TOLERANCE)
  {
    servohori.write(servoh + 1);

    if (servoh > servohLimitHigh)
    {
      servoh = servohLimitHigh;
    }
    delay(STEP_DELAY);
  }

  else if (avgright - avgleft > TOLERANCE)
  {
    servohori.write(servoh - 1);

    if (servoh < servohLimitLow)
    {
      servoh = servohLimitLow;
    }
    delay(STEP_DELAY);
  }

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

Credits

robotistan

robotistan

5 projects • 0 followers
Global maker store that inspire you to make something, teach you to something and meet your material requirements. Make It!

Comments