#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// DEFINE SENSOR PIN
const int lm35Pin = 32;
const int ldrPin = 34;
const int voltageSensorPin = 35;
const int soilMoisturePin = 39; // Example analog pin for soil moisture sensor
// Define relay
const int relayPin = 27; // Example digital pin for relay
// Voltage divider resistors
const float R1 = 30000.0;
const float R2 = 7500.0;
// Soil moisture threshold
const int soilMoistureThreshold = 2000;
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize the serial communication
Serial.begin(115200);
Serial.println("SOLAR POWER MONITORING SYSTEM");
Serial.println("======================================");
Serial.println();
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Starting up...");
// Delay to allow the LCD to initialize properly
delay(2000); // 2 seconds delay
// Configure the analog pins as input (optional as it's the default)
pinMode(lm35Pin, INPUT);
pinMode(ldrPin, INPUT);
pinMode(voltageSensorPin, INPUT);
pinMode(soilMoisturePin, INPUT);
// Configure the relay pin as output and ensure it is off initially
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure the relay is initially off (active low)
}
void loop() {
// Read and process LM35 data, LDR and VOLTAGE SENSOR
float temperatureC = readTemperature();
float lux = readLDR();
float vIN = readVoltage();
// Read and process Soil Moisture Sensor data
int soilMoistureValue = analogRead(soilMoisturePin);
Serial.print("Soil Moisture Value: ");
Serial.println(soilMoistureValue);
// Control the relay based on the soil moisture value
if (soilMoistureValue < soilMoistureThreshold) {
Serial.println("Soil is dry, turning on the pump...");
digitalWrite(relayPin, HIGH); // Turn on the relay (active low)
} else {
Serial.println("Soil is wet, turning off the pump...");
digitalWrite(relayPin, LOW); // Turn off the relay
}
// Print a separator for readability
Serial.println("---------------------------------------------");
Serial.println();
// Print the values to the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tmp:");
lcd.print(temperatureC, 1); // Print temperature with 1 decimal place
lcd.setCursor(9, 0);
lcd.print("V:");
lcd.print(vIN, 1); // Print voltage with 1 decimal place
lcd.setCursor(0, 1);
lcd.print("Lux:");
lcd.print(lux, 0); // Print light level without decimal places
lcd.setCursor(9, 1);
lcd.print("M:");
lcd.print(soilMoistureValue); // Print soil moisture value
// Delay before the next reading
delay(2000);
}
float readTemperature() {
// Read the analog value from the LM35
int lm35Value = analogRead(lm35Pin);
// Convert the analog value to voltage (for ESP32 with 12-bit ADC)
float voltage = lm35Value * (3.3 / 4095.0);
// Convert the voltage to temperature in Celsius
float temperatureC = voltage * 100.0;
// Print the temperature in Celsius
Serial.println("Temperature Sensor (LM35):");
Serial.print(" Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
Serial.println();
return temperatureC;
}
float readLDR() {
// Read the analog value from the LDR
int ldrValue = analogRead(ldrPin);
// Convert the analog value to voltage (for ESP32 with 12-bit ADC)
float voltage = ldrValue * (3.3 / 4095.0);
// Calculate the resistance of the LDR (assuming a 10kΩ resistor in series with the LDR)
float RLDR = (10000.0 * (3.3 - voltage)) / voltage;
// Convert the resistance to a light level (Lux)
float Lux = RLDR / 500;
// Print the LDR value and the corresponding light level in Lux
Serial.println("Light Sensor (LDR):");
Serial.print(" LDR Value: ");
Serial.print(ldrValue);
Serial.print(" / Voltage: ");
Serial.print(voltage, 4); // Print voltage with 4 decimal places
Serial.print(" V / Light Level: ");
Serial.print(Lux);
Serial.println(" Lux");
Serial.println();
return Lux;
}
float readVoltage() {
// Read the analog value from the voltage sensor
int sensorValue = analogRead(voltageSensorPin);
// Convert the analog value to voltage (for ESP32 with 12-bit ADC)
float vOUT = sensorValue * (3.3 / 4095.0);
// Calculate the input voltage using the voltage divider formula
float vIN = vOUT / (R2 / (R1 + R2));
// Print the voltage sensor value and the corresponding input voltage
Serial.println("Voltage Sensor:");
Serial.print(" Sensor Value: ");
Serial.print(sensorValue);
Serial.print(" / Output Voltage: ");
Serial.print(vOUT, 4); // Print output voltage with 4 decimal places
Serial.print(" V / Input Voltage: ");
Serial.print(vIN);
Serial.println(" V");
Serial.println();
return vIN;
}