Arduino Projects

Dual Axis Solar Tracker Arduino Project Using LDR & Servo Motors

Hello and Welcome to The IoT Projects. In this article, you will learn to make a Dual Axis Solar Tracker Arduino Project Using LDR and Servo Motors in Step by Step manner. In this project, we are going to use some Light Sensitive Sensors like (LDR) to track the sunlight and direct the solar panels towards the areas that Increase their efficiency. I have divided the article into 7 segments:

Components required for this project

The following list is the list of all the components that are required for this project:

S.NComponents NameDescriptionQuantityGet Products from Amazon
1Arduino UNOARDUINO UNO R31https://amzn.to/2L0iZf2
2Servo MotorMicro Servo - SG902https://amzn.to/2NRXXAy
LDRLDR ( Photoresistor) Sensor4https://amzn.to/3kiHz8r
3Resistors10k-ohm resistors4https://amzn.to/2NTPtsF
4BreadboardMini Breadboard1https://amzn.to/2NP2UKL
53D body parts3D body parts Design1https://www.thingiverse.com/thing:2467743
6Potentiometer10k Potentiometer2https://amzn.to/2MgRMWz

Our Popular Arduino Based Projects:

Interfacing Temperature and Humidity Sensor with Arduino

DIY Mobile Phone using GSM Module & Arduino with Nextion Display

Simple Weather station using Arduino & BME280 Barometric Pressure Sensor

RFID Based Attendance System Using NodeMCU with PHP Web App

Capacitive Soil Moisture Sensor with OLED Display & Arduino

Working Principle of LDR Sensor

Here, LDR Works as a light detector. It is also known as a photoresistor. Actually, It is a light Sensitive device. As shown in the graph, the resistance decreases as light falls on it. In this project, we are using 4 LDRs to detect Sunlight. And when they send a signal to the Arduino, It will guide two Servo Motors to better place the solar panel to maximize its efficiency.

Interface LDR Photo Resistor to Arduino and Control LEDs
Working Process of LDR

Project Simulation: Dual Axis Solar Tracker Arduino Project Using LDR & Servo Motors

Here, we will show you a complete overview of this project. Later on, we will discuss its wiring system. But, now we will stimulate this project.

When we power on the Arduino all the sensors and servo motors are in the action. As you can see in the above image. Usually, when we increase the light intensity on the LDR Sensor, the signal is sent to the Arduino. And Hence it guides the two servo motors to better place the solar panel. To increase its efficiency. Actually, you can see here as we increase or decrease the light intensity Servo Motors are into their actions.

Using these two potentiometers you can control the speed of Servo Motors as well. We will talk about it later in the programming section.

Tinkercad Dual Axis Solar Tracker Arduino Simulation file

Interfacing Dual Axis Solar Tracker Arduino Project Using LDR & Servo Motors

Interfacing Dual Axis Solar Tracker Arduino Project Using LDR & Servo Motors
Interfacing Dual Axis Solar Tracker Arduino Project Using LDR & Servo Motors
  • Connect the 5 volt pin from the Arduino to the Lower horizontal row of the breadboard.
  • Similarly, connect the GND pin from the Arduino to a second lower horizontal row of the breadboard.
  • Extend the 5 volt and GND Rows to the upper horizontal rows of the breadboard respectively.
  • Now connect the power pins of both Vertical and Horizontal servo motors to the 5 volts.
  • Similarly, Connect the GND pin of the Both Horizontal and Vertical Servo motor to the Ground.
  • Now, connect the Signal pin of the Vertical Servo Motor to the Digital Pin No. 10 of the Arduino.
  • Again, connect the Signal pin of the horizontal Servo Motor to the Digital Pin No. 9 of the Arduino.
  • Connect one terminal of both potentiometers to the Ground and the Other end terminals of both potentiometers to the VCC 5 volt.
  • Now, connect all LDRs from one terminal to the 5-volt and other terminals to the Ground Through 10k-ohm resistors.
  • Let’s connect the wiper pin of the potentiometer-1 one to the Analog Pin A4 and A5 to another wiper pin of Potentiometer-2.
  • Connect bottom left LDR voltage Divider point to A1 pin of Arduino.
  • Again, Connect Top Left LDR voltage Divider Point to A0 pin of the Arduino.
  • Similarly, Top Right LDR Voltage Divider Point to the A2 pin.
  • Finally, Connect the Bottom Right Voltage Divider Point of LDR to the A3 pin of the Arduino.

Programming Arduino for Dual Axis Solar Tracker Project

#Include is used to include a servo header library file.

#include <Servo.h> 

Configuration for Horizontal servo.
The Servo Horizontal is set to 180 degrees.
Servo Horizontal Limit When Signal is High is set to 175 degrees.
Again, Servo Horizontal Limit when the Signal value is low is set to 5 degrees.

Servo horizontal; // horizontal servo
int servoh = 180; 
int servohLimitHigh = 175;
int servohLimitLow = 5;
// 65 degrees MAX

This is the Servo Configuration for Vertical
The Servo Vertical is set to 45 degrees.
Servo Vertical Limit When Signal is High is set to 60 degrees.
Also, Servo Vertical Limit when the Signal value is low is set to 1 degree.

Servo vertical; // vertical servo
int servov = 45; 
int servovLimitHigh = 60;
int servovLimitLow = 1;

LDR Pin Connections
ldrlt is for Top Left
ldrrt is for Top Right
ldrld is for Down Left
ldrrd is for Down Right

// LDR pin connections
// name = analogpin;
int ldrlt = A0; //LDR top left - BOTTOM LEFT <--- BDG
int ldrrt = A3; //LDR top rigt - BOTTOM RIGHT
int ldrld = A1; //LDR down left - TOP LEFT
int ldrrd = A3; //ldr down rigt - TOP RIGHT

On void setup, we have attached the vertical and Horizontal servo signal pin. and the servo rotation for the horizontal is set to 180 degrees. and similarly, the vertical servo is set to 45 degrees. we have also set a delay of 2.5 seconds.

void setup(){
horizontal.attach(9);
vertical.attach(10);
horizontal.write(180);
vertical.write(45);
delay(2500);
}

In the Void loop() Function, we read the value for the analog pin of the Arduino connected to LDRs. and then calculate the average of vertical and horizontal.

void loop() {
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down right
int dtime = 10; int tol = 90; // dtime=diffirence time, tol=toleransi
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the diffirence of up and down
int dhoriz = avl - avr;// check the diffirence og left and rigt

IF and IF-ELSE statements are defined to loop the program and calculate the average values of the respective LDR’s. Finally, this code helps to change the degree of the servo motor. So that it can be more effective.

if (-1*tol > dvert || dvert > tol) 
 {
 if (avt > avd)
 {
 servov = ++servov;
 if (servov > servovLimitHigh)
 {servov = servovLimitHigh;}
 }
 else if (avt < avd)
 {servov= --servov;
 if (servov < servovLimitLow)
 { servov = servovLimitLow;}
 }
 vertical.write(servov);
 }
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
 {
 if (avl > avr)
 {
 servoh = --servoh;
 if (servoh < servohLimitLow)
 {
 servoh = servohLimitLow;
 }
 }
 else if (avl < avr)
 {
 servoh = ++servoh;
 if (servoh > servohLimitHigh)
 {
 servoh = servohLimitHigh;
 }
 }
 else if (avl = avr)
 {
 delay(5000);
 }
 horizontal.write(servoh);
 }

At last, we have added the delay of (dtime).

delay(dtime);
 
}

Now, compile the program and upload it to your Arduino board.

Our IoT Based Projects Resources:

ESP8266 based Coronavirus Tracker for your country

NodeMCU ESP8266 Monitoring DHT11/DHT22 Temperature and Humidity with Local Web Server

Home Automation with ESP8266 Web Server & Relay Module Control Appliances from Local Network

Interfacing DHT11 Humidity and Temperature Sensor with Arduino & LCD

IoT Based Flood Monitoring System Using NodeMCU & Thingspeak

Final Program code/sketch

#include <Servo.h> 


Servo horizontal; // horizontal servo
int servoh = 180; 
int servohLimitHigh = 175;
int servohLimitLow = 5;
// 65 degrees MAX

Servo vertical; // vertical servo
int servov = 45; 
int servovLimitHigh = 60;
int servovLimitLow = 1;

// LDR pin connections
// name = analogpin;
int ldrlt = A0; //LDR top left - BOTTOM LEFT <--- BDG
int ldrrt = A3; //LDR top rigt - BOTTOM RIGHT
int ldrld = A1; //LDR down left - TOP LEFT
int ldrrd = A3; //ldr down rigt - TOP RIGHT

void setup(){
horizontal.attach(9);
vertical.attach(10);
horizontal.write(180);
vertical.write(45);
delay(2500);
}
void loop() {
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down right
int dtime = 10; int tol = 90; // dtime=diffirence time, tol=toleransi
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the diffirence of up and down
int dhoriz = avl - avr;// check the diffirence og left and rigt

if (-1*tol > dvert || dvert > tol) 
 {
 if (avt > avd)
 {
 servov = ++servov;
 if (servov > servovLimitHigh)
 {servov = servovLimitHigh;}
 }
 else if (avt < avd)
 {servov= --servov;
 if (servov < servovLimitLow)
 { servov = servovLimitLow;}
 }
 vertical.write(servov);
 }
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
 {
 if (avl > avr)
 {
 servoh = --servoh;
 if (servoh < servohLimitLow)
 {
 servoh = servohLimitLow;
 }
 }
 else if (avl < avr)
 {
 servoh = ++servoh;
 if (servoh > servohLimitHigh)
 {
 servoh = servohLimitHigh;
 }
 }
 else if (avl = avr)
 {
 delay(5000);
 }
 horizontal.write(servoh);
 }
 
 delay(dtime);
 
}

Connecting all the parts together

Connecting all the printed parts together

Video Tutorials

Conclusion

Finally, we have completed Interfacing Dual Axis Solar Tracker Arduino Project Using LDR & Servo Motors. Now, you can use this Project to track the solar panel and increase its efficiency by 40%. We hope you found this project useful! Drop a comment below if you have any doubts or queries. We’ll do our best to answer your questions.

Related Articles

45 Comments

  1. Congrats by the didatic

    Is the pin ldrrt correct? (A3)

    int ldrlt = A0; //LDR top left – BOTTOM LEFT

  2. Hello bro

    int ldrlt = A0; //LDR top left – BOTTOM LEFT <— BDG
    int ldrrt = A3; //LDR top rigt – BOTTOM RIGHT
    int ldrld = A1; //LDR down left – TOP LEFT
    int ldrrd = A3; //ldr down rigt – TOP RIGHT

    it can be wrong?
    there have A3 for BOTTOM RIGHT and TOP RIGHT

    1. int ldrlt = A0; //LDR top left – BOTTOM LEFT
      int ldrrt = A2; //LDR top rigt – BOTTOM RIGHT
      int ldrld = A1; //LDR down left – TOP LEFT
      int ldrrd = A3; //ldr down rigt – TOP RIGHT

      Change ldrrt to A2 it was a spelling mistake

  3. Hi Alsan, I’m trying to build the project myself and would like to implement the potentiometers as well to control the servo speed. Can you please send me the code?

  4. Pin mode was not declared in setup
    Here is the compiled code

    #include
    Servo horizontal; // horizontal servo
    int servoh = 180;
    int servohLimitHigh = 175;
    int servohLimitLow = 5;
    // 65 degrees MAX
    const int ldrlt = A0;
    const int ldrrt = A1;
    const int ldrld = A2;
    const int ldrrd = A3;

    Servo vertical; // vertical servo
    int servov = 45;
    int servovLimitHigh = 60;
    int servovLimitLow = 1;

    void setup(){
    horizontal.attach(9);
    vertical.attach(10);
    horizontal.write(180);
    vertical.write(45);
    delay(2500);
    pinMode(A0,INPUT);
    pinMode(A1,INPUT);
    pinMode(A2,INPUT);
    pinMode(A3,INPUT);

    }
    void loop() {
    int lt = analogRead(ldrlt); // top left
    int rt = analogRead(ldrrt); // top right
    int ld = analogRead(ldrld); // down left
    int rd = analogRead(ldrrd); // down right
    int dtime = 10; int tol = 90; // dtime=diffirence time, tol=toleransi
    int avt = (lt + rt) / 2; // average value top
    int avd = (ld + rd) / 2; // average value down
    int avl = (lt + ld) / 2; // average value left
    int avr = (rt + rd) / 2; // average value right
    int dvert = avt – avd; // check the diffirence of up and down
    int dhoriz = avl – avr;// check the diffirence og left and rigt

    if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle
    {
    if (avt > avd)
    {
    servov = ++servov;
    if (servov > servovLimitHigh)
    {servov = servovLimitHigh;}
    }
    else if (avt < avd)
    {servov= –servov;
    if (servov dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
    {
    if (avl > avr)
    {
    servoh = –servoh;
    if (servoh < servohLimitLow)
    {
    servoh = servohLimitLow;
    }
    }
    else if (avl servohLimitHigh)
    {
    servoh = servohLimitHigh;
    }
    }
    else if (avl = avr)
    {
    delay(5000);
    }
    horizontal.write(servoh);
    }

    delay(dtime);

    }

    1. The sensitivity is too inaccurate the 4 LDR, for example if light hits the top right and bottom right no correction to the light source the stepper motor version – code is better because all 4 values are compared here, but it works, more suitable as a gimbal or as source code for robot projects Greetings from Vienna

      1. Hi Richard, if i use a stepper motor in this project rather than servo, do i need a different code or the same servo code can rotate the stepper motor?
        could you explain more how the stepper motor here is better than servo motor?

  5. is it possible to use a continuous servo in place of the horizontal servo and if so what code would i need to make that work?

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button