/* How to use the DHT-22 sensor with Arduino uno
Temperature and humidity sensor
*/
//Libraries
#include <DHT.h>;
//Constants
const int ldrPin = A0; // Analog pin for LDR sensor
const int ledPin = 9; // Digital pin for LED
const int threshold = 150;
#define DHTPIN 7
#define LED 4
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
//Variables
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
dht.begin();
}
void loop()
{
delay(10);
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
temp= dht.readTemperature();
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
if(temp>40){
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
delay(10); //Delay 2 sec.
int ldrValue = analogRead(ldrPin);
// Print the LDR value to the serial monitor (optional)
Serial.print("LDR Value: ");
Serial.println(ldrValue);
// Check if the LDR value is less than the threshold
if (ldrValue < 150) {
digitalWrite(ledPin, HIGH);
// Turn on the LED
} else {
digitalWrite(ledPin, LOW);
// Turn off the LED
}
delay(100); // Delay for a second (adjust as needed)
}