Write a program to interface DHT sensor with LED. The program will switch ON LED once the temperature rises above 25 degrees?
Last Updated : 15 Jul 2025
Solution:
#include "DHT.h"
#define DHTPIN 2 // Pin connected to the DHT11 sensor
#define DHTTYPE DHT11 // Define the type of DHT sensor (DHT11)
#define LEDPIN 5 // Pin connected to the LED
DHT dht(DHTPIN, DHTTYPE);
void setup(){
dht.begin(); // Initialize the DHT sensor
pinMode(LEDPIN, OUTPUT); // Set LED pin as output
}
void loop(){
float temp = dht.readTemperature(); // Read the temperature in Celsius
if (temp > 25) {
digitalWrite(LEDPIN, HIGH); // Turn on the LED if temperature is greater than 25°C
} else {
digitalWrite(LEDPIN, LOW); // Turn off the LED otherwise
}
}
Report an error
Meanwhile you can watch this video
Watch Video