/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
// 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 ANALOG_THRESHOLD 500
#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
#include "DHTesp.h"
const int DHT_PIN = 15;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
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
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
delay(1000);
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.
int LDRValue = analogRead(LIGHT_SENSOR_PIN); // read the value on analog pin
if (LDRValue < ANALOG_THRESHOLD)
digitalWrite(LED_PIN, HIGH); // turn on LED
else
digitalWrite(LED_PIN, LOW); // turn off LED
Serial.print("LDRValue: ");
Serial.println(LDRValue);
}