#include <Wire.h>
#include <Adafruit_SSD1306.h>
// OLED Display Configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Sensor Pins
#define VOLTAGE_SENSOR_PIN 36 // Potentiometer 1 (Voltage Sensor)
#define CURRENT_SENSOR_PIN 39 // Potentiometer 2 (Current Sensor)
// Constants for calculations
const float VOLTAGE_MAX = 24.0; // Maximum Solar Panel Voltage (Adjust as needed)
const float CURRENT_MAX = 10.0; // Maximum Solar Panel Current (Adjust as needed)
void setup() {
Serial.begin(115200);
// Initialize OLED Display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 initialization failed!");
while (true);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
}
void loop() {
// Read sensor values
float voltage = (analogRead(VOLTAGE_SENSOR_PIN) / 4095.0) * VOLTAGE_MAX;
float current = (analogRead(CURRENT_SENSOR_PIN) / 4095.0) * CURRENT_MAX;
float power = voltage * current; // Power in Watts
static float energy = 0; // Energy in Watt-Hours (Wh)
energy += power * (2.0 / 3600.0); // Update energy (every 2 sec)
// Display on Serial Monitor
Serial.print("Voltage: "); Serial.print(voltage); Serial.print("V, ");
Serial.print("Current: "); Serial.print(current); Serial.print("A, ");
Serial.print("Power: "); Serial.print(power); Serial.print("W, ");
Serial.print("Energy: "); Serial.print(energy); Serial.println("Wh");
// Display on OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("Solar Monitoring");
display.setCursor(0, 15);
display.print("Voltage: "); display.print(voltage); display.print(" V");
display.setCursor(0, 30);
display.print("Current: "); display.print(current); display.print(" A");
display.setCursor(0, 45);
display.print("Power: "); display.print(power); display.print(" W");
display.setCursor(0, 60);
display.print("Energy: "); display.print(energy); display.print(" Wh");
display.display();
delay(2000); // Update every 2 seconds
}