Sun Tracker

Sun Position Tracker – Improved Scan Method

The purpose of the improvement is to make the project more adjustable as per the surrounding. Learning from previous code in the First post, I am upgrading the code to be more dynamic. You might have noticed in the previous code, that I hard-coded the maximum value of the LDR for light source sensing.

But, in real – that threshold value depends on your setup, the quality of LDR (or photosensor), and the surrounding of the equipment that you have in hand.

Sun tracker – Scan_And_Max method

Still, our setup is one axis, but this time we have a bit improved code. Where the sensor scans for the entire 180 deg angle. Let’s see the results in different conditions of the surrounding environment.

Indoor Testing – Sun tracker module

Outdoor Test

Components used

  1. Arduino
  2. LDR (Photosensor, or Photo Resistor)
  3. Servo motor
  4. Packing carton
  5. Few wires

Connections – Schematic

The Code

As Arduino supports Cpp kind of code structure, so it was easy for me to write it fast. But, the logic in the code now, starts from an upper limit and lower limit & makes a temp log of the sensor readings. It compares and keeps the max value of the light sensor, while also saving the angle in a variable. This angle is the angle of the highest light sensor value.

const int ldrPin = A3;
const int ServoPin = 2;

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position
int max_value = 0;
int max_pos = 0;
int ldrStatus = 0;

void setup() {

myservo.attach(ServoPin);  // attaches the servo on pin 9 to the servo object
pinMode(ldrPin, INPUT);

//-------Scanning to get the maximum reading in Z Axis...
  for (int i=0;i<180;i++) {
    ldrStatus = analogRead(ldrPin);
    if (max_value<ldrStatus) {
        max_value = ldrStatus;
        max_pos = i;
  Serial.print("MAX Value Detected = ");Serial.println(max_value);

void loop() {

Unnecessary components are removed and this simple 1 Input, 1 Output setup is tested.


The Same code and the same setup is tested under different surroundings. which clearly shows that the efficiency & accuracy of the working directly depends on the surroundings. Also, one more thing to notice is that – we are scanning through entire range of motion. We can remove this time taking process and implement a better algorithm in code for better and fast detection of light source… i.e. SUN.

That I will do in the next experiment and share my experience with you guys 🙂

Leave a Reply