const int ldrPin = 35;
const int temperaturePin = 32;
const int pumpPin = 26; // This is for LED which is used instead of a water pump
const int soilMoisturePin = 33; // This is a simulated soil moisture (potentiometer) since wokwi doesnt have soil moisture
const int rainPin = 14; // This is a simulated rain sensor (push-button switch) since wokwi doesnt have rain sensor
// all this basically tell u where the wires are connected. ex: the ldr is connected to pin 35
void setup() {
Serial.begin(115200);
pinMode(pumpPin, OUTPUT);
pinMode(rainPin, INPUT_PULLUP);
}
void loop() {
int lightIntensity = analogRead(ldrPin); // Reading LDR sensor
float temperature = getTemperature(temperaturePin); // Reading NTC Temperature Sensor
int soilMoisture = analogRead(soilMoisturePin); // reading simulated soil moisture sensor (potentiometer)
bool isRaining = !digitalRead(rainPin); // reading the simulated rain sensor (push button)
// Based on these conditions the water pump (LED) will fuction
if (lightIntensity < 200 && temperature > 25 && !isRaining && soilMoisture < 500) {
digitalWrite(pumpPin, HIGH); // Turn on the water pump (simulated LED)
} else {
digitalWrite(pumpPin, LOW); // Turn off the water pump (simulated LED)
}
//printing or displaying sensor data to the serial monitor
Serial.print("Light Intensity: ");
Serial.print(lightIntensity);
Serial.print("\tTemperature: ");
Serial.print(temperature);
Serial.print("°C\tRain: ");
Serial.print(isRaining ? "Yes" : "No");
Serial.print("\tSoil Moisture: ");
Serial.println(soilMoisture);
delay(1000);
}
float getTemperature(int pin) {
int rawValue = analogRead(pin);
float voltage = (rawValue / 1023.0) * 3.3;
float r = (3300.0 / voltage) - 1000.0;
float steinhart;
steinhart = log(r / 10000.0);
steinhart /= 3950.0;
steinhart += 1.0 / (298.15 + 273.15);
steinhart = 1.0 / steinhart - 273.15;
return steinhart;
// this entire process called steinhart is used to convert analog readings to
//into a temperature value in degrees Celsius. I did this sice in wokwi they dont have ADC (Analog to digital converter)
}