#define LDR_UP A1
#define LDR_DOWN A2
#define VOLTAGE_SENSOR A3 // Simulated by potentiometer
#define CURRENT_SENSOR A4 // Simulated by potentiometer
const int forward = 8;
const int backward = 9;
const int speedPin = 10;
// ** Variables for Voc and Isc **
float Voc = 0; // Open Circuit Voltage
float Isc = 0; // Short Circuit Current
bool measured = false; // Flag to check if Voc/Isc is measured
void setup() {
Serial.begin(9600);
pinMode(forward, OUTPUT);
pinMode(backward, OUTPUT);
pinMode(speedPin, OUTPUT);
}
void loop() {
// **Step 1: Measure Voc & Isc Only Once**
if (!measured) {
measureVocIsc();
measured = true; // Ensure it's only measured once
}
// **Step 2: Read Sensor Data**
int lightUp = analogRead(LDR_UP);
int lightDown = analogRead(LDR_DOWN);
int luxDifference = lightUp - lightDown;
float voltage = analogRead(VOLTAGE_SENSOR) * (25.0 / 1023.0);
float current_raw = analogRead(CURRENT_SENSOR) * (5.0 / 1023.0);
// **Step 3: Limit the PV Curve Based on Voc & Isc**
float current = Isc * (1 - exp(-voltage / Voc)); // PV Curve Limiting
float power = voltage * current; // Corrected PV Power
// **Step 4: Solar Tracker Movement**
int motorSpeed = map(abs(luxDifference), 0, 200, 150, 255);
if (lightUp < 10 && lightDown < 10) {
Serial.println("Returning to Start Position");
digitalWrite(forward, LOW);
digitalWrite(backward, HIGH);
analogWrite(speedPin, 100);
}
else if (luxDifference > 30) {
Serial.println("Moving Forward");
digitalWrite(forward, HIGH);
digitalWrite(backward, LOW);
analogWrite(speedPin, motorSpeed);
}
else if (luxDifference < 5) {
Serial.println("Waiting (Cloud detected)");
digitalWrite(forward, LOW);
digitalWrite(backward, LOW);
delay(5000);
}
else {
Serial.println("Stopping");
digitalWrite(forward, LOW);
digitalWrite(backward, LOW);
}
// **Step 5: Display PV Data**
Serial.print("Voc: "); Serial.print(Voc); Serial.print("\t");
Serial.print("Isc: "); Serial.print(Isc); Serial.print("\t");
Serial.print("Voltage: "); Serial.print(voltage); Serial.print("\t");
Serial.print("Current: "); Serial.print(current); Serial.print("\t");
Serial.print("Power: "); Serial.print(power); Serial.print("\t");
Serial.print("Light Up: "); Serial.print(lightUp); Serial.print("\t");
Serial.print("Light Down: "); Serial.print(lightDown); Serial.print("\t");
Serial.print("Motor Speed: "); Serial.println(motorSpeed);
delay(1000);
}
// **Function to Measure Voc and Isc**
void measureVocIsc() {
// Measure Voc (Max Voltage when Current = 0)
float sumVoc = 0;
for (int i = 0; i < 10; i++) { // Take multiple readings for accuracy
sumVoc += analogRead(VOLTAGE_SENSOR) * (25.0 / 1023.0);
delay(100);
}
Voc = sumVoc / 10; // Average Voc
// Measure Isc (Max Current when Voltage = 0)
float sumIsc = 0;
for (int i = 0; i < 10; i++) {
sumIsc += analogRead(CURRENT_SENSOR) * (5.0 / 1023.0);
delay(100);
}
Isc = sumIsc / 10; // Average Isc
Serial.print("Measured Voc: "); Serial.println(Voc);
Serial.print("Measured Isc: "); Serial.println(Isc);
}