#include <LiquidCrystal_I2C.h>
// The below are constants, which cannot be changed
#define LIGHT_SENSOR_PIN 33 // ESP32 pin GIOP36 (ADC0) connected to light sensor
#define LED_PIN 13 // ESP32 pin GIOP22 connected to LED
#define buzzer 27
#define echoPin 4 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 19 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT); // set ESP32 pin to output mode
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
pinMode(buzzer, OUTPUT);
LCD.init();
LCD.backlight();
LCD.setCursor(1, 0);
LCD.print("IOT SMART HOME");
LCD.setCursor(3, 1);
LCD.print("Light sensor");
delay(5000);
LCD.clear();
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// You can send any value at any time.
// Please don't send more that 10 values per second.
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
int LDRValue = analogRead(LIGHT_SENSOR_PIN); // read the value on analog pin
if (LDRValue < 600)
digitalWrite(LED_PIN, HIGH); // turn on LED
else
digitalWrite(LED_PIN, LOW); // turn off LED
if (distance< 200)
digitalWrite(buzzer, HIGH); // turn on LED
else
digitalWrite(buzzer, LOW); // turn off LED
LCD.setCursor(0,0);
LCD.print("Distance: ");
LCD.print(distance);
LCD.println(" cm");
LCD.setCursor(0,1);
LCD.print("LDRValue: ");
LCD.println(LDRValue);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("LDRValue: ");
Serial.println(LDRValue);
}