Project Banao Rain Detection Model Project Kit | Arduino UNO Rain Sensor & Water Detection DIY Science STEM Project | Automatic Rain Detector with Servo Motor, Sensor Module & Electronics Components

–

by

in

#include <Servo.h>

const int RAIN_PIN = A0;
const int SERVO_PIN = 9;
const int WET_LIMIT = 550;  // Adjust after checking readings
const int DRY_LIMIT = 650;  // Must be higher than WET_LIMIT
const int OPEN_ANGLE = 0;
const int CLOSED_ANGLE = 90;

Servo coverServo;
bool coverClosed = false;
int wetCount = 0;
int dryCount = 0;

void setup() {
  Serial.begin(9600);
  coverServo.attach(SERVO_PIN);
  coverServo.write(OPEN_ANGLE);
}

void loop() {
  int reading = analogRead(RAIN_PIN);
  Serial.println(reading);

  if (!coverClosed && reading <= WET_LIMIT) {
    wetCount++;
  } else {
    wetCount = 0;
  }
  if (coverClosed && reading >= DRY_LIMIT) {
    dryCount++;
  } else {
    dryCount = 0;
  }

  if (wetCount >= 3) {
    coverServo.write(CLOSED_ANGLE);
    coverClosed = true;
    wetCount = 0;
  }
  if (dryCount >= 3) {
    coverServo.write(OPEN_ANGLE);
    coverClosed = false;
    dryCount = 0;
  }
  delay(200);
}

Leave a Reply

Your email address will not be published. Required fields are marked *