DHT11 (temperature and Humidity Sensor) + Arduino UNO: a Tutorial

by AustinS89 in Circuits > Arduino

330 Views, 0 Favorites, 0 Comments

DHT11 (temperature and Humidity Sensor) + Arduino UNO: a Tutorial

capture - 2024-07-29T103402.387.png

The DHT11 sensor is easy to use with the Arduino platform, and in this guide, I'll show you how to wire it up and write code to read temperature and humidity data.

Before getting started, I wanted to mention that I designed this circuit (wiring diagram and code) in Cirkit Designer, which is a web-based tool for designing electronics projects.

You can view and edit this circuit in Cirkit Designer here: https://app.cirkitdesigner.com/project/682edcc0-fb42-4786-8563-df7c175f1736

Supplies

For this project, you'll need the following parts:

  • DHT11 Humidity and Temperature Sensor
  • Arduino UNO
  • 10k Ohm Resistor
  • Breadboard and Jumper Wires

Wiring

capture - 2024-07-29T103402.387.png

Here's how to connect the DHT11 sensor to the Arduino UNO:

  1. Connect the 5V pin on the Arduino to the VDD pin on the DHT11 sensor.
  2. Connect a 10k Ohm resistor between the VDD and DATA pins of the DHT11 sensor.
  3. Connect the DATA pin of the DHT11 sensor to digital pin 2 on the Arduino.
  4. Connect the GND pin on the Arduino to the GND pin on the DHT11 sensor.

Make sure to connect your Arduino UNO to your computer via USB to upload the code.

Code

Cirkit Code.PNG

Now let's run the code to read data from the DHT11 sensor:

  1. Open this project in the Cirkit Designer Web Editor: https://app.cirkitdesigner.com/project/682edcc0-fb42-4786-8563-df7c175f1736
  2. Open the Code tab in the Cirkit Designer Editor
  3. Click Verify to make sure that the code compiles
  4. Click Upload to load this code to your Arduino UNO
  5. Open the Serial Monitor: the button to open can be found in the top-right of the screen
  6. Set the baud rate to 9600 to view the temperature and humidity readings

This code uses the Adafruit DHT library to interface with the sensor and prints out the temperature and humidity readings to the Serial Monitor.


Here is the full code:

/**
 * This example demonstrates how to collect temperature and humidity measurements
 * from the Adafruit DHT11 sensor. Measurements are printed out to the serial monitor.
 *
 * - Make sure to first install the following libraries through the Arduino Library Manager:
 *   - DHT Sensor Library (by Adafruit)
 *   - Adafruit Unified Sensor (by Adafruit)
 * - When you open the serial monitor to view measurements from the DHT11, make sure
 * that you select 9600 baud so that the serial monitor can receive data from the Arduino.
 *
 * This example was originally written by Adafruit Industries LLC.
 */


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>


#define DHTPIN 2     // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.


// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)


// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview


DHT_Unified dht(DHTPIN, DHTTYPE);


uint32_t delayMS;


void setup() {
  Serial.begin(9600);
  // Initialize device.
  dht.begin();
  Serial.println(F("DHTxx Unified Sensor Example"));
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  Serial.println(F("------------------------------------"));
  Serial.println(F("Temperature Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
  Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
  Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
  Serial.println(F("------------------------------------"));
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  Serial.println(F("Humidity Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("%"));
  Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("%"));
  Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("%"));
  Serial.println(F("------------------------------------"));
  // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;
}


void loop() {
  // Delay between measurements.
  delay(delayMS);
  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  }
  else {
    Serial.print(F("Temperature: "));
    Serial.print(event.temperature);
    Serial.println(F("°C"));
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
  }
}

Conclusion

After following these steps, you should have a working setup that can read temperature and humidity data from the DHT11 sensor and display it on the Serial Monitor. This project is a great starting point for integrating environmental data into your Arduino projects.