Detecting Position of Sun – Basic concept with Arduino

The project focuses on the detection of the source of light in a particular environment. That environment can be an indoor environment or outdoor. And, I am barely starting with the project from scratch. So, need to prepare everything from the beginning.

The concept is to use only 1 or 2 photo sensors (aka LDRs) to accurately detect the position of Light Source (Sun). There are methods available to detect position of Sun with 4 LDR sensors, But I wanted to avoid that – as I think, we can get enough accuracy with single Photo sensor, that we do not need to use 4 sensors to do the same Job.

Components used

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

Connections – Schematic

Detecting Position of Sun - Basic concept with Arduino

The Code

Following Arduino Code (custom Cpp) is what makes it possible to scan for the light source in the surroundings. But, this is limited to only 1 Axis or 1 plane – detection of Sun position.

const int ledPin = 5;
   //Fake Sun
const int ldrPin = A3;
   //Photo Sensor
const int ServoPin = 2;
  //Servo Motor

#include <Servo.h>

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

int pos = 0;    // variable to store the servo position
int Threshold = 40;  // change this value, depending on Your sensor Quality

void setup() {
myservo.attach(ServoPin);  // attaches the servo on pin 9 to the servo object
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
digitalWrite(ledPin, HIGH);

void loop() {
  // Looping through the complete 180 deg angle of servo
  // for detecting light Source in 1 axis...  
  if (pos > 180) {
    pos = 0;
  int ldrStatus = analogRead(ldrPin);
  if (ldrStatus < Threshold) {

In the Setup, I am using 5mm White LED as a very small personal Sun. So, We can see the theory in action.

The Theory

The Single LDR setup for Sun position tracker is based on the fact that – Light Travels in a straight line. So, When light directly falls on the sensor inside the dark box – then, it is assumed by the micro-controller that Sun is in that direction.

Unprofessional Diagram

The Above picture is not a masterpiece, but enough to make sure that you grasp the concept and how we can combine physics with our code and electronics to achieve the same thing with fewer components.

One thought on “Detecting Position of Sun – Basic concept with Arduino

Leave a Reply