Arduino Humidity Sensor: A beginner's guide to measuring humidity using an Arduino Uno and DHT11 sensor
Introduction
This beginners Arduino humidity sensor tutorial will allow you
to easily measure humidity using a DHT11 sensor connected to an Arduino
(or Nano - they have very similar pinouts, and use the same processor).
Measuring humidity is an important part of monitoring environmental
conditions. Whether you're creating a greenhouse monitoring system,
checking for proper storage conditions, or just wanting to track
humidity levels in your home, an Arduino humidity sensor can provide
valuable humidity data.
In this tutorial, we'll show you how to use a popular humidity sensor -
the DHT11 - with an Arduino board to measure humidity levels. The DHT11
is an affordable and easy-to-use sensor that reports both temperature
and humidity. It connects directly to any Arduino pin and requires just
one wire for communication.
Measuring Humidity has many useful
applications:
Home/greenhouse environment monitoring
3D printer enclosure humidity control
Laboratory/industrial process control
Weather stations
Swimming pool/spa equipment control
Building automation
Storage environment monitoring
Agricultural environment control
This beginner's project will teach how to use an affordable
DHT11 digital humidity sensor with an Arduino board to detect and
monitor ambient humidity levels.
You will connect the DHT11 to an Arduino, write code to read the humidity values, and display the results.
By learning humidity sensing, you'll gain skills applicable
to other sensors and environmental monitoring projects. This tutorial
will cover connecting the sensor, writing code, and testing the finished
circuit.
What is a DHT11 sensor?
The
DHT11 is a digital temperature
and humidity sensor. It is low-cost, easy to use, and provides reliable
humidity readings. The sensor has three pins - power, data, and ground -
making it simple to interface with a microcontroller like the Arduino.
Note that the "raw unit" has 4 pins one of which is not connected. On
breakout boards there are three pins.
Humidity readings are dependent on temperature - The amount
of moisture the air can hold is directly related to temperature. Warmer
air can hold more water vapor than cooler air. So to get an accurate
relative humidity reading, the temperature must also be known to
compensate calculations. This is why it can return both temperature and
humidity readings.
One thing you should know about the DHT series of humidity sensors is
that they use a special single wire protocol and it's not compatible
with other single wire protocols. It means that each DHT device must be
connected to a single I/O pin. You can't wire up several units on the
same pin (as you can with a DS18B20).
Required Components
Arduino Uno
DHT11 Humidity Sensor
Breadboard
Jumper wires
Computer with Arduino IDE
Arduino humidity sensor: Circuit Diagram
Diagram using Fritzing
Schematic Diagram
Diagram using Fritzing
Libraries needed
The library you need to install is "DHT".
This is the library from Adafruit and it may require you to install
dependent libraries - just say yes when asked (when you follow the
process below).
Installing an Arduino Library with IDE
Install the library as follows:
1st method:
Click the menu bar and follow the menu items:
Sketch-->Include Library-->Manage
Libraries...
2nd method:
Click the ICON on the left that looks like a set of books.
The library manager appears on the left of the screen.
In the search box type DHT. Now click the install button.
Arduino humidity sensor: Example Sketch
The following sketch reads the humidity sensor, and outputs
the obtained humidity and temperature readings to the serial monitor.
Note: There are accuracy specification on this device
(DHT11) and it is slightly less accurate than a DHT22 but it is still
very useful; see this page "Detailed DHT11 and DHT22 Information".
#include"DHT.h"
#defineDHTPIN3
#defineDHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
voidsetup(){
Serial.begin(9600);
dht.begin();
}
voidloop(){
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}
Short Code Explanation
Include DHT library
Define sensor pin and type
Initialize DHT object
Take readings in loop()
Print values to serial
Detailed Code Explanation
#include "DHT.h" - Includes the DHT sensor library which contains functions to read the sensor data
#define DHTPIN 2 - Defines the pin that will be connected to the sensor's data pin (pin 2 in this case)
#define DHTTYPE DHT11 - Defines the type of sensor used (DHT11)
DHT dht(DHTPIN, DHTTYPE); - Creates an instance of the DHT object and passes the pin number and sensor type
void setup() - Arduino setup function, runs once at startup
Serial.begin(9600); - Initializes serial communication at 9600 baud rate
dht.begin(); - Initializes the DHT sensor
void loop() - Main Arduino loop function, runs continuously after setup
delay(2000); - Waits 2 seconds between readings
float humidity = dht.readHumidity(); - Reads the humidity value from the sensor
float temperature = dht.readTemperature(); - Reads the temperature value
Serial.print - Prints the humidity and temperature values to the serial monitor
So in summary, it initializes serial communication and the
sensor, then takes readings in a loop and prints the values out
periodically.
Arduino Humidity sensor: Uploading the Code
There are a few steps to uploading the code using the Arduino IDE:
Connect the Arduino Uno to the PC with a USB cable.
Select the Arduino Uno hardware.
Open a new sketch.
Paste the code above into the new page (overwrite everything).
Press the upload button (right arrow at top).
You can find a more detailed tutorial on the Arduino IDE page.
Testing the Circuit
Check serial monitor for changing humidity readings.
Conclusions
This
concludes the tutorial on measuring humidity with an Arduino and
humidity sensor - specifically a DHT22 sensor. With additional sensors,
you can expand environmental monitoring e.g. with pressure monitoring,
sunlight detection etc.
Note that each DHT needs its own Arduino I/O pin - you can not connect multiple units to a single pin
For further reading this page has more details on the DHT11 and DHT22.
Written by John Main who has a degree in Electronic Engineering.
Note: Parts of this page were written using claude-instant
as a research assistant.
How to get accurate DHT22 digital humidity sensor readings with an Arduino. Did you know it also measures temperature as Well? Find out why, in this page...
A PIR sensor lets your Arduino sense movement without contact. This tutorial covers PIR sensor basics, connecting one to an Arduino board and coding a motion detector.
Arduino Hall Effect Sensor: Add magnetic sensing superpowers to your Arduino projects with an easy-to-use hall effect sensor. With full code and layout...
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.