5.4.10 Project 5.2 Close the Window

1. Description

We will work to use a servo and a raindrop sensor to make an device closing windows automatically when raining.

2. Component Knowledge

Raindrop Sensor: This is an analog input module, the greater the area covered by water on the detection surface, the greater the value returned (range 0~4096).

3. Test Code

#include <ESP32Servo.h>

#define servoPin 5
#define waterPin 34
Servo myservo;


void setup() {
  Serial.begin(9600);
  pinMode(waterPin, INPUT);

    // Allow allocation of all timers
    ESP32PWM::allocateTimer(0);
    ESP32PWM::allocateTimer(1);
    ESP32PWM::allocateTimer(2);
    ESP32PWM::allocateTimer(3);
    myservo.setPeriodHertz(50);    // standard 50 hz servo
    myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object
    // using default min/max of 1000us and 2000us
    // different servos may require different min/max settings
    // for an accurate 0 to 180 sweep

  delay(200);
}

void loop() {
  int water_val = analogRead(waterPin);
  Serial.println(water_val);
  if(water_val > 1500) {
    myservo.write(0);
    delay(200);
  }
  else {
    myservo.write(176);
    delay(200);
  }
}

4. Test Result

At first, the window opens automatically, and when you touch the raindrop sensor with your hand (which has water on the skin), the window will close.