Temperature and Humidity Sensor
Temperature sensors measure the ambient temperature and are used in many applications. For example, they are used to control appliances, to turn off an oven when it reaches the required temperature or to turn on an air conditioner if the room temperature crosses a threshold. They are used to warn if engine in vehicles or machines in factories are overheating.
There are different types of temperature sensors categorized by the mechanism they use to detect temperature. Sensors can be caliberated to provide the temperature in Fahrenheit, Kelvin, or Celsius.
The common types of temperature sensors include:
-
Thermocouple: This sensor works on the principles of the Thermoelectric effect. When two dissimilar electrical conductors are joined together, they form what is known as an electrical junction. When the temperature applied at the junction changes, the voltage across the junction also changes. By measuring the voltage, the temperature can be calculated and calibrated based on the properties of the two materials.
-
Resistor Temperature Detectors / Thermistors: These sensors work on the principle where the resistance of a conductor is proportional to the temperature. As a general rule, the resistance of a conductor increases with an increase in temperature. These sensors require a current to be passed through, and as the temperature changes, so does the resistance, and therefore, the current. By measuring the current, the temperature can be calibrated based on the conductor material and its variance in resistance due to temperature.
-
Semiconductors: These sensors use the working principles of semiconductors and the fact that voltage across the p-n junction of a semiconductor changes with temperature. They are compact in size and have the highest accuracy and sensitivity, and work across a good range of temperatures (about -50 to +150 degrees Celsius), and hence are the most widely used.
Humidity sensors work by detecting the amount of water vapor in the air and converting that measurement into an electrical signal. They typically use a material that reacts to moisture, like a hygroscopic material, which absorbs or releases water based on humidity levels. This change in the material’s properties, like resistance or capacitance, is then measured and translated into a humidity reading.
Humidity sensors are used in several applications such as in storage units to control the humidity to prevent damage to the stored goods (especially food items) due to excessive moisture, in computer data centres where excessive humidity can damage computers, and for weather monitoring and prediction.
The three commonly used mechanisms are:
-
Capacitive Sensors: These sensors use a hygroscopic dielectric material (an insulator that absorbs moisture) between two electrodes. As humidity changes, the dielectric constant of the material changes, altering the capacitance of the sensor.
-
Resistive Sensors: These sensors use a material whose electrical resistance changes with humidity. For example, some resistive sensors use a film of hygroscopic material placed between two electrodes. When humidity changes, the resistance of the material changes, and this change is measured.
-
Thermal Sensors: These sensors use a dual sensor system. One sensor is housed in a dry environment, while the other measures ambient air. The difference in temperature between the two sensors indicates the humidity level.
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
Component | Purpose |
---|---|
Arduino Nano | This will be the microcontroller |
DHT11/22 | This will be the temperature and humidity sensor |
Circuit Diagram

Connections
DHT11/22
Nano Pin | DHT11/22 Pin |
---|---|
D2 | Output |
5V | VCC |
GND | GND |
Code
/*
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
}