30
ILOVALAR
// Include necessary libraries
#include
// Define pin numbers
const int lightPin = 2; // Pin for controlling the light
const int dhtPin = 7; // Pin for the DHT temperature and humidity sensor
// Create a DHT object
DHT dht(dhtPin, DHT11);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set light pin as an output
pinMode(lightPin, OUTPUT);
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Read temperature and humidity from DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Print temperature and humidity values to the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
31
Serial.print("°C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Check if the temperature exceeds a threshold
if (temperature > 25.0) {
// If temperature is high, turn on the light
digitalWrite(lightPin, HIGH);
Serial.println("Light ON");
} else {
// If temperature is normal, turn off the light
digitalWrite(lightPin, LOW);
Serial.println("Light OFF");
}
// Add delay to avoid rapid sensor readings
delay(5000); // 5 seconds delay
}
|