#include <DHT.h>
#define DHTPIN 2 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // Type of DHT sensor
#define LDRPIN 34 // Pin where the LDR sensor is connected
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
delay(2000); // Wait for 2 seconds to avoid flooding the serial monitor
float temperature = dht.readTemperature(); // Read temperature in Celsius
float humidity = dht.readHumidity(); // Read humidity
// Check if any reads failed and exit early (to try again)
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
int ldrValue = analogRead(LDRPIN); // Read LDR sensor value
float ldrVoltage = ldrValue * (5.0 / 1023.0); // Convert to voltage
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.print(" %\tLDR Voltage: ");
Serial.print(ldrVoltage);
Serial.println(" V");
}