#include <Wire.h>
#include <BH1750.h>
#include <DHT.h>
// Light Sensors for Solar Tracking
BH1750 lightNorth;
BH1750 lightSouth;
// Motor Driver Pins
#define IN1 5
#define IN2 6
#define ENA 9
// Movement Settings
const int maxSpeed = 255;
const int minSpeed = 20;
// Sunlight Thresholds
const int cloudDrop = 100;
const int noSunThreshold = 20;
int luxNorth = 0, luxSouth = 0;
int previousLux = 0;
unsigned long cloudDetectedTime = 0;
// Temperature & Power Measurement
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define VOLTAGE_SENSOR A0
#define CURRENT_SENSOR A1
float voltage = 0, current = 0, power = 0;
float temperature_air = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
// Initialize Sensors
lightNorth.begin(BH1750::CONTINUOUS_HIGH_RES_MODE);
lightSouth.begin(BH1750::CONTINUOUS_HIGH_RES_MODE);
dht.begin();
// Motor Control Pins
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);
Serial.println("Solar Tracker + Power vs. Temperature Monitoring Initialized...");
}
void loop() {
// Read light intensity for tracking
luxNorth = lightNorth.readLightLevel();
luxSouth = lightSouth.readLightLevel();
int luxDifference = luxNorth - luxSouth;
Serial.print("Lux North: "); Serial.print(luxNorth);
Serial.print("\t Lux South: "); Serial.print(luxSouth);
Serial.print("\t Difference: "); Serial.println(luxDifference);
// Move Solar Tracker
if (luxNorth > luxSouth) {
int speed = constrain(map(abs(luxDifference), 0, 500, minSpeed, maxSpeed), minSpeed, 255);
Serial.print("Moving South at Speed: "); Serial.println(speed);
movePiston("South", speed);
}
else if (abs(previousLux - luxSouth) > cloudDrop) {
Serial.println("🌥 Cloud Detected → Pausing...");
cloudDetectedTime = millis();
stopPiston();
}
else if (millis() - cloudDetectedTime < 5000) {
Serial.println("Waiting for cloud to pass...");
}
else if (abs(luxNorth - luxSouth) < 10) {
Serial.println("☀ Sunlight Balanced → Returning to North");
movePiston("North", maxSpeed);
}
else {
Serial.println("✔ Stable Sunlight → Stopping");
stopPiston();
}
previousLux = luxSouth;
// Measure Power & Temperature
temperature_air = dht.readTemperature();
voltage = readVoltage();
current = readCurrent();
power = voltage * current;
// Print Data for Arduino Serial Plotter
Serial.print("T_Air:"); Serial.print(temperature_air);
Serial.print("\tPower:"); Serial.println(power);
delay(2000);
}
// Move Piston Function
void movePiston(String direction, int speed) {
if (direction == "South") {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
} else if (direction == "North") {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
analogWrite(ENA, speed);
}
// Stop Piston Function
void stopPiston() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0);
}
// Read Voltage
float readVoltage() {
int sensorValue = analogRead(VOLTAGE_SENSOR);
return (sensorValue / 1023.0) * 5.0 * 11; // Adjust for voltage divider
}
// Read Current
float readCurrent() {
int sensorValue = analogRead(CURRENT_SENSOR);
float voltage = (sensorValue / 1023.0) * 5.0;
return (voltage - 2.5) / 0.185; // ACS712 (5A version)
}