Skip to content

muhammadwali0/Solar-Tracker-with-Python-Dashboard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Solar Tracker with Python Dashboard

An Arduino Nano–based dual-LDR solar tracker that sweeps a servo motor across a 120° arc to lock onto the brightest light source, paired with a real-time Python dashboard that streams and visualizes live telemetry from the device.


Table of Contents


Overview

This project combines embedded hardware control with real-time data visualization. An Arduino Nano reads two LDR (light-dependent resistor) sensors and drives a servo motor to continuously orient toward the strongest light source. A companion Python script connects over serial, parses the telemetry stream, and renders a live scrolling graph with sensor state, servo angle, movement direction, and tracking stability.


How It Works

Two LDRs are mounted on opposite sides of the servo arm. On each loop iteration, the Arduino reads both sensors and applies the following logic:

Left LDR Right LDR Action
BRIGHT DARK Step servo left (−5°)
DARK BRIGHT Step servo right (+5°)
BRIGHT BRIGHT Nudge toward center (90°)
DARK DARK Hold current position

The servo is constrained between 30° and 150°, providing a 120° tracking arc. Each step is 5°, and the control loop runs with an 80 ms delay.

After every iteration, the Arduino transmits a comma-separated telemetry line over Serial at 9600 baud:

<left>,<right>,<angle>

The Python dashboard reads this stream, parses each frame, and updates a rolling 100-sample live graph at approximately 20 Hz.


Hardware

Bill of Materials

Component Specification
Microcontroller Arduino Nano
Light Sensors 2× LDR module (digital, active-low)
Actuator Servo motor (standard, 5V)
Misc Breadboard, jumper wires

Wiring

Arduino Pin Connected To
D2 Left LDR output
D3 Right LDR output
D9 Servo signal
5V LDR and servo VCC
GND LDR and servo GND

Note: The LDR modules used here output LOW when light is detected (active-low). The firmware inverts these readings internally so that 1 consistently means bright and 0 means dark throughout the codebase and telemetry stream.


Software

Arduino Firmware

File: solar_tracker.ino

Written in C++ using the Arduino Servo library (built-in — no additional installation required). On each loop cycle the firmware:

  1. Reads both LDR pins via digitalRead.
  2. Inverts the active-low signals for logical consistency.
  3. Applies the four-case tracking logic to compute the new servo angle.
  4. Clamps the angle to the [30°, 150°] range using constrain.
  5. Writes the updated angle to the servo.
  6. Transmits left,right,angle over Serial at 9600 baud.

Python Dashboard

File: solar_dashboard.py

Library Purpose
pyserial Serial communication with the Arduino
matplotlib Live plot rendering via plt.pause() animation
collections.deque Efficient fixed-length rolling window for data

The dashboard maintains a rolling window of the last 100 angle samples and refreshes the plot every ~50 ms. The figure title bar displays a live summary:

Left: BRIGHT | Right: DARK | Angle: 75° | Dir: LEFT | Stability: 84.3%

Stability is calculated as the percentage of samples where the servo angle did not change — a proxy for how well the tracker has locked onto a stable light source.


Getting Started

1. Flash the Arduino

Open solar_tracker.ino in the Arduino IDE and upload it to your Nano. The only dependency is the built-in Servo.h library — no Library Manager installation is needed.

2. Install Python Dependencies

pip install -r requirements.txt

3. Configure the Serial Port

Edit the PORT constant at the top of solar_dashboard.py to match the port your Arduino is connected to:

# Linux
PORT = "/dev/ttyACM0"

# macOS
PORT = "/dev/cu.usbmodem14101"  # port name varies — check Arduino IDE

# Windows
PORT = "COM3"

You can find the correct port in the Arduino IDE under Tools → Port.

4. Run the Dashboard

Ensure the Arduino is connected, then run:

python solar_dashboard.py

A dark-themed Matplotlib window will appear and begin plotting live servo angle data. Press Ctrl+C in the terminal to stop.


Project Structure

Solar-Tracker-with-Python-Dashboard/
├── solar_tracker.ino       # Arduino firmware (C++)
├── solar_dashboard.py      # Real-time Python dashboard
├── requirements.txt        # Python dependencies
├── LICENSE
├── .gitignore
└── README.md

License

This project is licensed under the terms of the LICENSE file included in this repository.

About

Arduino-based solar light tracker with dual LDR sensing and a real-time Python/Matplotlib dashboard.

Topics

Resources

License

Stars

Watchers

Forks

Contributors