HOW to Make a Smart Windows with ESP32
Description
In life, we can see all kinds of smart products, such as smart home. Smart homes include smart curtains, smart windows, smart TVs, smart lights, and more. In this experiment, we use a steam sensor to detect rainwater, and then achieve the effect of closing and opening the window by a servo.
Required Components
|
|
|
---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Keyestudio Steam Sensor*1 |
|
|
|
Servo*1 |
3P Dupont Wire*1 |
Micro USB Cable*1 |
Connection Diagram
Test Code
//**********************************************************************************
/*
* Filename : smart window
* Description : Water drop sensor controls steering gear rotation.
* Auther : http//www.keyestudio.com
*/
#include <ESP32Servo.h>//Import the steering gear library file
int adcVal = 0;//A variable that holds the ADC value output by the droplet sensor
int servoPin = 15; // Define the servo pin
Servo myservo;//Defines an instance of the steering gear class
#define PIN_ADC 34 //the pin of the Water drop sensor
void setup(){
Serial.begin(9600);
pinMode(PIN_ADC, INPUT);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 500, 2500); // attaches the servo on servoPin to the servo object
}
void loop(){
adcVal = analogRead(PIN_ADC);//The droplet sensor is connected to the analog port GP34
Serial.println(adcVal);
if (adcVal > 2000) {//The simulated value is greater than 2000
myservo.write(0);//close the window
delay(500);//Give the steering gear time to turn
} else {// no rain
myservo.write(180);//open the window
delay(500);//Delay 500ms
}
}
//**********************************************************************************
Code Explanation
We can control a servo to rotate by a threshold.
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. When the sensor detects a certain amount of water, the servo rotates to achieve the effect of closing or opening windows.