solar tracker system using arduino and ldr code and circuit diagram

Arduino Code

#include <Servo.h>

Servo sunflower;

const int ldrLeft = A0;
const int ldrRight = A1;
const int servoPin = 9;

int angle = 90;                  // Centre position
const int threshold = 50;       // Ignore small light differences
const int stepSize = 2;         // Movement per step

void setup() {
  sunflower.attach(servoPin);
  sunflower.write(angle);
  Serial.begin(9600);
  delay(500);
}

void loop() {
  int leftValue = analogRead(ldrLeft);
  int rightValue = analogRead(ldrRight);

  Serial.print(“Left: “);
  Serial.print(leftValue);
  Serial.print(”  Right: “);
  Serial.println(rightValue);

  int difference = leftValue – rightValue;

  if (difference > threshold) {
    // Left sensor receives more light
    angle -= stepSize;
  }
  else if (difference < -threshold) {
    // Right sensor receives more light
    angle += stepSize;
  }

  angle = constrain(angle, 10, 170);
  sunflower.write(angle);

  delay(50);
}

Circuit Diagram

Leave a Comment