This circuit is designed to automatically close a roof when rain is detected and open it when the rain stops. The system uses an Arduino Uno R3 microcontroller to read input from a rain sensor and control a DC motor via an L298N motor driver. Additionally, a buzzer is used to provide audible feedback during the roof's operation.
L298N DC Motor Driver
12V Adapter
Arduino Uno R3
Rain Sensor
DC Motor (2 units)
Buzzer
// Define pins
const int rainSensorPin = 2; // Digital pin connected to rain sensor
const int motorPin1 = 9; // Motor driver input pin 1
const int motorPin2 = 10; // Motor driver input pin 2
const int buzzerPin = 8; // Buzzer pin
// Define states
bool isRoofClosed = false;
void setup() {
// Initialize pins
pinMode(rainSensorPin, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Start with the roof open
openRoof();
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int rainDetected = digitalRead(rainSensorPin);
if (rainDetected == LOW && !isRoofClosed) {
// Rain detected, close the roof
closeRoof();
} else if (rainDetected == HIGH && isRoofClosed) {
// No rain, open the roof
openRoof();
}
delay(1000); // Check every second
}
void closeRoof() {
Serial.println("Closing Roof...");
// Rotate motor to close the roof
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
// Beep the buzzer while closing the roof
digitalWrite(buzzerPin, HIGH);
// Assuming it takes 5 seconds to close the roof
delay(5000);
// Stop the motor and buzzer
stopMotor();
digitalWrite(buzzerPin, LOW);
isRoofClosed = true;
}
void openRoof() {
Serial.println("Opening Roof...");
// Rotate motor to open the roof
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
// Beep the buzzer while opening the roof
digitalWrite(buzzerPin, HIGH);
// Assuming it takes 5 seconds to open the roof
delay(5000);
// Stop the motor and buzzer
stopMotor();
digitalWrite(buzzerPin, LOW);
isRoofClosed = false;
}
void stopMotor() {
// Stop the motor by setting both inputs to LOW
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
This code initializes the pins and sets up the system to start with the roof open. It continuously checks the rain sensor and either opens or closes the roof based on the sensor's input. The buzzer provides audible feedback during the roof's operation.