/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-ldr-module
*/
// #define DO_PIN 2// Update to the correct pin mapping
// void setup() {
// // initialize serial communication
// Serial.begin(115200);
// // initialize the ESP8266's pin as an input
// pinMode(DO_PIN, INPUT);
// }
// void loop() {
// int lightState = digitalRead(DO_PIN);
// if (lightState == HIGH)
// Serial.println("The light is NOT present");
// else
// Serial.println("The light is present");
// }
// #include <ESP32Servo.h>
// #define DO_PIN 2 // Update to the correct pin mapping
// #define SERVO_PIN 13 // Choose the appropriate pin for the servo motor
// Servo myservo; // Create a servo object
// void setup() {
// // initialize serial communication
// Serial.begin(115200);
// // initialize the ESP8266's pin as an input
// pinMode(DO_PIN, INPUT);
// // attach the servo to the corresponding pin
// myservo.attach(SERVO_PIN);
// }
// void loop() {
// int lightState = digitalRead(DO_PIN);
// if (lightState == HIGH) {
// Serial.println("The light is NOT present");
// turnServo();
// } else {
// Serial.println("The light is present");
// // Call the function to turn the servo to 90 degrees
// }
// }
// void turnServo() {
// // Move the servo to 90 degrees
// myservo.write(90);
// delay(5000); // Adjust delay as needed
// myservo.write(0); // Return the servo to the initial position
// }
// #include <ESP32Servo.h>
// #define DO_PIN 2 // Update to the correct pin mapping
// #define SERVO_PIN 13 // Choose the appropriate pin for the servo motor
// #define LED_PIN 14 // Choose the appropriate pin for the LED
// Servo myservo; // Create a servo object
// void setup() {
// // initialize serial communication
// Serial.begin(115200);
// // initialize the ESP8266's pin as an input
// pinMode(DO_PIN, INPUT);
// // attach the servo to the corresponding pin
// myservo.attach(SERVO_PIN);
// // initialize the LED pin as an output
// pinMode(LED_PIN, OUTPUT);
// }
// void loop() {
// int lightState = digitalRead(DO_PIN);
// if (lightState == HIGH) {
// // Serial.println("The light is NOT present");
// turnServo();
// digitalWrite(LED_PIN, LOW); // Turn on the LED
// } else {
// // Serial.println("The light is present");
// digitalWrite(LED_PIN, HIGH); // Turn off the LED
// // Call the function to turn the servo to 90 degrees
// }
// }
// void turnServo() {
// // Move the servo to 90 degrees
// myservo.write(90);
// delay(1000); // Adjust delay as needed
// myservo.write(0); // Return the servo to the initial position
// }
#include <ESP32Servo.h>
#include <DHT_U.h>
#define DO_PIN 2 // Update to the correct pin mapping
#define SERVO_PIN 13 // Choose the appropriate pin for the servo motor
#define LED_PIN 14 // Choose the appropriate pin for the LED
#define DHT_PIN 12 // Choose the appropriate pin for the DHT22 sensor
Servo myservo; // Create a servo object
DHT dht(DHT_PIN, DHT22); // Create a DHT object
void setup() {
// initialize serial communication
Serial.begin(115200);
// initialize the ESP8266's pin as an input
pinMode(DO_PIN, INPUT);
// attach the servo to the corresponding pin
myservo.attach(SERVO_PIN);
// initialize the LED pin as an output
pinMode(LED_PIN, OUTPUT);
// initialize the DHT sensor
dht.begin();
}
void loop() {
int lightState = digitalRead(DO_PIN);
if (lightState == HIGH) {
// Serial.println("The light is NOT present");
turnServo();
digitalWrite(LED_PIN, LOW); // Turn on the LED
} else {
// Serial.println("The light is present");
digitalWrite(LED_PIN, HIGH); // Turn off the LED
printDHTData();
}
}
void turnServo() {
// Move the servo to 90 degrees
myservo.write(90);
delay(1000); // Adjust delay as needed
myservo.write(0); // Return the servo to the initial position
}
void printDHTData() {
// Read and print temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C | Humidity: ");
Serial.print(humidity);
Serial.println("%");
delay(1000); // Adjust delay as needed
}