Friday, 9 May 2025

🌞 DIY Arduino Solar Tracker with Motor Control – Complete Guide

 

🌞 DIY Arduino Solar Tracker with Motor Control – Complete Guide



🔧 Overview:

This project uses Light Dependent Resistors (LDRs) to track the sun's position and adjust the orientation of a solar panel using two servo motors. Additionally, based on the sunlight intensity, it controls a motor using two buttons – ideal for automating a solar-powered device like a water pump.


🧰 Components Required:

Component
Quantity
Description
Arduino Uno
1
Microcontroller board
Servo Motor (SG90/MG90)
2
For horizontal and vertical rotation
LDR (Light Sensor)
5
For detecting sunlight intensity and direction
10kΩ Resistors
5
Pull-down resistors for each LDR
Push Buttons
2
For controlling motor direction
DC Motor
1
Can be used for pumping or rotation purposes
NPN Transistor (optional)
1
For driving higher power motors
Diode (1N4007)
1
For back EMF protection
Power Supply (9V/12V)
1
To power the motor
      Breadboard + Jumper Wires                      As needed    For circuit connections

🔌 Pin Configuration:

Arduino PinConnected Component
A1LDR Top Left
A2LDR Top Right
A3LDR Bottom Left
A4LDR Bottom Right
A5LDR Middle Top (Sunlight Detection)
D5Horizontal Servo Signal
D6Vertical Servo Signal
D7Motor Control A
D8Motor Control B
D9Button 1 (Forward)
D10Button 2 (Reverse)
GNDAll GND connections
5VPower supply for LDRs and buttons

⚙️ How It Works:

  • LDRs detect light intensity. The 4 corner LDRs determine the direction of sunlight, and the middle LDR checks whether there is enough light.

  • The Arduino calculates the average light values and adjusts the horizontal and vertical servo angles accordingly.

  • If sunlight is detected and button 1 is pressed, the motor rotates in one direction.

  • If no sunlight is present and button 2 is pressed, the motor rotates in the opposite direction.

  • If neither condition is met, the motor stops.


🖼️ Circuit Diagram:

📟 Arduino Code:

#include <Servo.h>


Servo horizontal; // horizontal servo

Servo vertical; // vertical servo
// LDR analog pins
const int button1 = 9;
int pos = 0;
void setup() {
  pinMode(motorA, OUTPUT);
  delay(2500); // Give servos time to initialize
void loop() {
    int lt = analogRead(ldrlt);
    int avt = (lt + rt) / 2;
    if (abs(dvert) > tol) {
    if (abs(dhoriz) > tol) {
    delay(dtime);
    for (pos = oldvalue; pos <= 180; pos++) {
    buttonStateB = digitalRead(button2);


int servoh = 180;

int servohLimitHigh = 175;

int servohLimitLow = 5;



int servov = 0;

int servovLimitHigh = 60;

int servovLimitLow = 0;



int ldrlt = A1; // Top Left

int ldrrt = A2; // Top Right

int ldrld = A3; // Bottom Left

int ldrrd = A4; // Bottom Right

int ldrmt = A5; // Middle Top LDR



const int button2 = 10;

const int motorA = 7;

const int motorB = 8;

int buttonStateA = 0;

int buttonStateB = 0;



int pos2 = 0;

int oldvalue, oldvalue2;



  horizontal.attach(5);

  vertical.attach(6);

  horizontal.write(180);

  vertical.write(0);



  pinMode(motorB, OUTPUT);

  pinMode(button1, INPUT);

  pinMode(button2, INPUT);



}



  int ldrStatus = analogRead(ldrmt);

  

  if (ldrStatus > 30) {

    buttonStateA = digitalRead(button1);

    if (buttonStateA == LOW) {

      digitalWrite(motorA, HIGH);

      digitalWrite(motorB, LOW); // Motor rotates in one direction

    } else {

      digitalWrite(motorA, LOW);

      digitalWrite(motorB, LOW); // Stop motor

    }



    int rt = analogRead(ldrrt);

    int ld = analogRead(ldrld);

    int rd = analogRead(ldrrd);

    int dtime = 10;

    int tol = 90;



    int avd = (ld + rd) / 2;

    int avl = (lt + ld) / 2;

    int avr = (rt + rd) / 2;

    int dvert = avt - avd;

    int dhoriz = avl - avr;



      if (avt > avd) {

        servov++;

        if (servov > servovLimitHigh) servov = servovLimitHigh;

      } else {

        servov--;

        if (servov < servovLimitLow) servov = servovLimitLow;

      }

      vertical.write(servov);

    }



      if (avl > avr) {

        servoh--;

        if (servoh < servohLimitLow) servoh = servohLimitLow;

      } else {

        servoh++;

        if (servoh > servohLimitHigh) servoh = servohLimitHigh;

      }

      horizontal.write(servoh);

    }



  }

  else {

    oldvalue = horizontal.read();

    oldvalue2 = vertical.read();



      horizontal.write(pos);

      delay(15);

    }

    for (pos2 = oldvalue2; pos2 >= 0; pos2--) {

      vertical.write(pos2);

      delay(15);

    }



    if (buttonStateB == LOW) {

      digitalWrite(motorA, LOW);

      digitalWrite(motorB, HIGH); // Reverse motor

    } else {

      digitalWrite(motorA, LOW);

      digitalWrite(motorB, LOW);

    }

  }

}

🔔 DON’T FORGET TO SUBSCRIBE TO D TREND FOR MORE AWESOME PROJECTS AND TECH CONTENT! 🔔


No comments:

Post a Comment

🎯 Build Your Own RFID Attendance System Using Arduino + ESP32 + Google Sheets

  🎯 Build Your Own RFID Attendance System Using Arduino + ESP32 + Google Sheets 📅 By Dinesh | Tech DIY Projects | May 2025     🚀 Intro...