#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <CD74HC4067.h>
#include <Servo.h>
CD74HC4067 my_mux(5, 4, 3, 2); // (S0, S1, S2, S3)
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo waterPump;
int relay = 8; // Define the pin number for the relay
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
waterPump.attach(9); // Attach servo to pin 9
pinMode(8, INPUT); // Mengatur pin 8 sebagai output untuk relay
}
void loop() {
lcd.clear();
for (int i = 0; i < 8; i++) { // Loop through each sensor
my_mux.channel(i); // Select the sensor channel
int sensorNumber = i + 1; // Change sensor number to start from 1
int soilMoistureValue = analogRead(A0 + i); // Read soil moisture level from the selected sensor
double y = map(soilMoistureValue, 0, 1023, 0, 100); // Map the soil moisture value to a range of 0 to 100
// Display the result on LCD with sensor number starting from 1
lcd.setCursor(0, 0);
lcd.print("Sensor " + String(sensorNumber) + ":");
lcd.setCursor(0, 1);
lcd.print("Moisture: " + String(y, 0) + "%");
// Control the water pump (servo) based on soil moisture value using relay
if (y < 50) {
digitalWrite(8, HIGH); // Turn on the relay to activate the servo if moisture is below 50%
} else {
digitalWrite(8, LOW); // Turn off the relay to deactivate the servo if moisture is above 50%
}
delay(2000); // Delay for display stability
}
}
Loading
cd74hc4067
cd74hc4067