Here is a simple Arduino sketch to read temperature and humidity data from a DHT11 sensor and display it on the Serial Monitor:
DHT11 Arduino Sketch (Using DHT Library)
Hardware Required
- Arduino board (e.g., Uno, Nano)
- DHT11 sensor
- 10kΩ resistor (optional, for pull-up on data pin)
- Jumper wires
Wiring (DHT11 with 3 pins or module)
Step 1: Install Library
In Arduino IDE:
Sketch → Include Library → Manage Libraries → Search "DHT sensor library" by Adafruit → Install
Also install:
"Adafruit Unified Sensor" library
Step 2: Arduino Code
cpp #include <DHT.h> // Define the pin where the DHT11 is connected #define DHTPIN 2 #define DHTTYPE DHT11 // DHT11 or DHT22 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); Serial.println("DHT11 Sensor Reading Started..."); } void loop() { delay(2000); // DHT11 needs 1-2 seconds delay between readings float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); // Celsius if (isnan(humidity) || isnan(temperature)) { Serial.println("Failed to read from DHT11 sensor!"); return; } Serial.print("Temperature: "); Serial.print(temperature); Serial.print("°C | Humidity: "); Serial.print(humidity); Serial.println("%"); }
Example Output in Serial Monitor
yaml DHT11 Sensor Reading Started... Temperature: 25.00°C | Humidity: 58.00% Temperature: 25.10°C | Humidity: 59.00%
Top comments (0)