This circuit is designed to monitor rain using a rain sensor and control a servo motor to retract or extend a stand based on the rain detection. The status and rain sensor readings are displayed on a 16x2 I2C LCD. The circuit is controlled by an Arduino UNO microcontroller.
RAIN SENSOR
16x2 I2C LCD
Arduino UNO
Servo
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Pin definitions
const int rainSensorDigitalPin = 2;
const int rainSensorAnalogPin = A0;
const int servoPin = 9;
const int lcdAddress = 0x27;
const int lcdColumns = 16;
const int lcdRows = 2;
// Create objects
LiquidCrystal_I2C lcd(lcdAddress, lcdColumns, lcdRows);
Servo servo;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize LCD
lcd.begin(lcdColumns, lcdRows); // Corrected line
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Clothes Drying");
lcd.setCursor(0, 1);
lcd.print("Stand Ready");
// Initialize servo
servo.attach(servoPin);
servo.write(0); // Initial position
// Initialize rain sensor pin
pinMode(rainSensorDigitalPin, INPUT);
}
void loop() {
// Read rain sensor digital value
int rainDetected = digitalRead(rainSensorDigitalPin);
int rainAnalogValue = analogRead(rainSensorAnalogPin);
// Display rain sensor value on LCD
lcd.setCursor(0, 1);
lcd.print("Rain: ");
lcd.print(rainAnalogValue);
if (rainDetected == LOW) { // Rain detected
lcd.setCursor(0, 0);
lcd.print("Rain Detected!");
servo.write(90); // Retract stand
} else { // No rain
lcd.setCursor(0, 0);
lcd.print("No Rain ");
servo.write(0); // Extend stand
}
delay(1000); // Wait for a second
}
This code initializes the LCD, servo, and rain sensor. It continuously reads the rain sensor values and updates the LCD display. If rain is detected, the servo retracts the stand; otherwise, it extends the stand.