Skip to Content
This is the Beta version of our new Learning Paths approach. Please email feedback.

Temperature and Humidity Sensor

The DHT11 and DHT22 are basic, low-cost digital temperature and humidity sensors. They use a capacitive humidity sensor and a thermistor to measure the surrounding air and provides a digital value on the data pin.

This project reads temperature values from a DHT11/22 sensor and displays the readings on the Serial Monitor.

Components
ComponentPurpose
Arduino NanoThis will be the microcontroller
DHT11/22This will be the temperature and humidity sensor
Circuit Diagram
nano_temperature_humidity_sensor_fritzing
Connections

DHT11/22

Nano PinDHT11/22 Pin
D2Output
5VVCC
GNDGND
Code
temperature_humidity_sensor.ino
/* Project: Physical Computing Circuit: Temperature and Humidity Sensor Boards Supported: UNO R3/R4, Nano, ESP32 Function: This project reads the temperature and humidity values from a temperature and humidity sensor (a DHT11 or DHT22). Author: STEMVentor Educonsulting This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use. */ // LIBRARIES // You will need to install the library named "DHT sensor library" by Adafruit. // This will also need you to install the library named "Adafruit Unified Sensor" by Adafruit. #include <DHT.h> //for DHT11/DHT22 sensor // PIN DEFINITIONS #define DHT_PIN 2 // GLOBAL CONSTANTS // GLOBAL VARIABLES // INITIALIZE OBJECTS // DHT configuration #define DHT_TYPE DHT22 // if using DHT 22 // #define DHTTYPE DHT11 // if using DHT 11 DHT dht(DHT_PIN, DHT_TYPE); //DHT object /* LOCAL FUNCTIONS */ // Setup DHT22 sensor. void DHTSetup() { // Initialize the DHT object. dht.begin(); Serial.println("The DHT22 sensor is ready!"); } // Read DHT22 sensor values. void readDHTValues() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor) float humidity_reading = dht.readHumidity(); // Read temperature as Celsius (parameter: false, the default) float temperature_reading_celsius = dht.readTemperature(false); // Read temperature as Fahrenheit (parameter: true) float temperature_reading_fahrenheit = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(humidity_reading) || isnan(temperature_reading_celsius) || isnan(temperature_reading_fahrenheit)) { Serial.println(F("Failed to read from DHT sensor!")); return; } // The library provides a function to calculate the heat index which // is value derived with a formula with temperature and humidity values. // Compute heat index with temperature in Celsius (last parameter: false) float heat_index_celsius = dht.computeHeatIndex(temperature_reading_celsius, humidity_reading, false); // Compute heat index with temperature in Fahrenheit (last parameter: true) float heat_index_fahrenheit = dht.computeHeatIndex(temperature_reading_fahrenheit, humidity_reading, true); // Print the temperature in °C and °F Serial.print("Temperature:" + String(temperature_reading_celsius) + "°C"); Serial.print(F("/")); Serial.println(String(temperature_reading_fahrenheit) + "°F"); // Print the humidity in % Serial.println("Humidity:" + String(humidity_reading) + "%"); // Print the heat index in °C and °F Serial.print("Heat Index:" + String(heat_index_celsius) + "°C"); Serial.print("/"); Serial.println(String(heat_index_fahrenheit) + "°F"); } // THE setup FUNCTION RUNS ONCE WHEN YOU PRESS RESET OR POWER THE BOARD. void setup() { Serial.begin(9600); DHTSetup(); Serial.println("DHT Sensor setup."); } // THE loop FUNCTION RUNS OVER AND OVER AGAIN FOREVER UNTIL THE BOARD IS POWERED OFF. void loop() { readDHTValues(); delay(1000*10); // In milliseconds }