Arduino Experiment: Read Data from DHT11 Sensor

Difficulty Level: Beginner

In this experiment, you will learn how to interface a DHT11 temperature and humidity sensor with an Arduino and display the readings on the Serial Monitor.

Needed Components with eBay Links

Understanding the DHT11 Sensor

The DHT11 sensor measures both temperature and humidity and outputs these values digitally to the Arduino. It is simple to use but has limited precision, making it an ideal sensor for beginner-level experiments.

Key Functions

Wiring the Circuit

Connect the DHT11 sensor to your Arduino as follows:

Arduino Code to Read Data from DHT11

Ensure the DHT sensor library is installed before uploading this code. You can install it from the Library Manager in the Arduino IDE by searching for DHT sensor library.


// Include the DHT library
#include "DHT.h"

// Define pin and type for the sensor
#define DHTPIN 2     // Digital pin connected to the DHT11
#define DHTTYPE DHT11   // DHT 11 sensor type

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);  // Initialize the serial communication
  dht.begin();  // Start the DHT sensor
}

void loop() {
  delay(2000);  // Wait a few seconds between measurements

  // Reading temperature and humidity
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Check if any reading failed
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Display results in Serial Monitor
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C");
}
            

Upload & Test

  1. Connect your Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and copy the code above into the editor.
  3. Install the DHT library if you haven't already.
  4. Select your board and the correct port in the IDE.
  5. Upload the code by clicking the upload button.
  6. Open the Serial Monitor (Ctrl+Shift+M) to see the temperature and humidity readings.

Code Breakdown

The code begins by including the DHT library, which contains pre-written functions to simplify sensor usage. In the setup() function, we initialize the sensor and Serial Monitor. The loop() function continuously reads the humidity and temperature, then prints them to the Serial Monitor.

Conclusion

This experiment demonstrates how to interface with a DHT11 sensor using Arduino. It provides basic weather station-like functionality, allowing you to monitor the surrounding environment. You can further expand this project by adding an LCD display or integrating the data into an IoT project.