#include <Stepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define stepper motor parameters
const int stepsPerRevolution = 200; // Adjust based on your stepper motors
// azimuthStepper pins
const int stepPin1 = 5;
const int dirPin1 = 6;
// elevationStepper pins
const int stepPin2 = 3;
const int dirPin2 = 4;
// LDR pins
const int ldrTopLeftPin = A0;
const int ldrTopRightPin = A1;
const int ldrBottomLeftPin = A2;
const int ldrBottomRightPin = A3;
// Voltage and current sensor pins
const int voltagePin = A4;
const int currentPin = A5;
// Threshold for LDR difference to trigger movement
const int threshold = 25; // Adjust for sensitivity
// Create stepper objects
Stepper azimuthStepper(stepsPerRevolution, stepPin1, dirPin1);
Stepper elevationStepper(stepsPerRevolution, stepPin2, dirPin2);
// LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, 2 rows
void setup() {
// Set initial stepper speeds
azimuthStepper.setSpeed(200);
elevationStepper.setSpeed(200);
// Start serial communication for debugging
Serial.begin(9600);
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("System Ready");
delay(2000);
lcd.clear();
}
void displayVoltageAndCurrent() {
float voltage = analogRead(voltagePin) * (0.5 / 1023.0) * 35; // Adjust based on your sensor
float current = analogRead(currentPin) * (1.0 / 1023.0) * 0.639; // Adjust based on your sensor
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.print(voltage, 2);
lcd.print("V ");
lcd.setCursor(0, 1);
lcd.print("Current: ");
lcd.print(current, 3);
lcd.print("A ");
}
void loop() {
// Read LDR values
int ldrTopLeft = analogRead(ldrTopLeftPin);
int ldrTopRight = analogRead(ldrTopRightPin);
int ldrBottomLeft = analogRead(ldrBottomLeftPin);
int ldrBottomRight = analogRead(ldrBottomRightPin);
// Calculate differences
int horizontalDifference = ldrTopLeft + ldrBottomLeft - ldrTopRight - ldrBottomRight;
int verticalDifference = ldrTopLeft + ldrTopRight - ldrBottomLeft - ldrBottomRight;
// LDR-based tracking
if (ldrTopLeft < 500 && ldrTopRight < 500 && ldrBottomLeft < 500 && ldrBottomRight < 500) {
if (horizontalDifference > threshold) {
azimuthStepper.step(1);
} else if (horizontalDifference < -threshold) {
azimuthStepper.step(-1);
}
if (verticalDifference > threshold) {
elevationStepper.step(1);
} else if (verticalDifference < -threshold) {
elevationStepper.step(-1);
}
}
// Display voltage and current
displayVoltageAndCurrent();
delay(1000); // Delay between tracking updates
}
Horizontal
Vertical
Solar Tracker
Aim Horizontal pointer at sun
(north is up)