If you have a humidifier that doesn’t turn on or off automatically, you can build this functionality on your own with a simple Arduino humidity sensor. With a DHT11 temperature and humidity sensor, Arduino projects can detect moisture levels in the air, and respond accordingly to automatically turn devices on and off. This guide will show you how.
Arduino humidity controller sensors work by measuring the relative humidity in the air. The relative humidity is calculated as the difference between the amount of water vapor in the air, versus the amount of water vapor needed for saturation. This is expressed as a percentage, ranging from 0-100%.
Internally, the sensor has two electrodes across a moisture-holding substrate. The two electrodes measure the electrical resistance between them to detect moisture levels in the air. As the substrate absorbs moisture in the air, the substrate becomes more conductive. The level of conductivity/resistance can then be converted into a usable humidity level by the sensor’s on-board IC.
Since this calculation is handled on the sensor itself, it only needs three pins: power, ground, and the sensor output pin. This is all you need to be able to detect both the humidity level as well as the temperature in the air, with the humidity sensor. Arduino projects can then perform actions based off that data, like turning on a humidifier, or running a fan.
For this project, we’ll be using the DHT11 temperature and humidity sensor. Arduino projects don’t inherently support the code we’ll be using, so we’ll be using this DHT library. Download the library .zip from this page and head to Sketch > Include library > Add .ZIP library and choose DHTLib.zip to make it available for the sketch we’ll be using.
To get started with this project, you’ll need a basic Arduino humidity sensor. There are several types out there, but for our purposes, we’ll be looking at the DHT11 model. Here’s the full list of what you’ll need to start building an Arduino temperature and humidity sensor:
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
One you have all the parts you’ll need, review the code in the next section before uploading it to your Arduino Uno. Then, head to the wiring section to learn how to put it all together.
Before you upload the sketch below, make sure you’ve added the DHTLib.zip library, as instructed above. Once that’s done, copy the code below into your Arduino IDE and upload it to your Arduino Uno.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include dht DHT; #define DHT11_PIN 2 void setup(){ Serial.begin(9600); } void loop(){ int chk = DHT.read11(DHT11_PIN); Serial.print("Temperature = "); Serial.println(DHT.temperature); Serial.print("Humidity = "); Serial.println(DHT.humidity); delay(2000); } |
Now, let’s review the important sections of this code, so you understand how it works.
1 2 3 4 5 |
#include dht DHT; #define DHT11_PIN 2 |
The first line here instructs the Arduino IDE to include the DHT.h library you uploaded before. Without this line, the sketch won’t work. Next, we create a dht object named DHT to control the sensor, and define a variable to store the pin that the sensor is connected to.
1 2 3 |
void setup(){ Serial.begin(9600); } |
In the setup() section, we’ll start the Serial monitor so you can see the temperature and humidity values that the sensor will return.
1 2 3 4 5 6 7 8 |
void loop(){ int chk = DHT.read11(DHT11_PIN); Serial.print("Temperature = "); Serial.println(DHT.temperature); Serial.print("Humidity = "); Serial.println(DHT.humidity); delay(2000); } |
Finally, in the loop() section, we’ll create a chk variable to store the input from the sensor. In the next four lines, we’ll print the temperature and humidity values, as returned by the DHT object. The temperature will be returned in a Celsius value, while the humidity value will read as a number between 0 to 100, reflecting the relative humidity value currently in the air.
The final line in the loop will pause the sketch for two seconds. Anything shorter than this and the sketch can sometimes return erratic values.
Once your code is uploaded, you’re ready to connect the sensor to your Arduino project. To do so, follow these steps:
Now that everything is connected, you can turn your Arduino Uno on by connecting it via USB. Open the Serial monitor in your Arduino IDE by heading to Tools > Serial Monitor. Here, you’ll be able to see your updated temperature and humidity values every two seconds.
You can add on to this section by taking action based on these values. For example, you can try turning on a device like a humidifier or a fan using a relay when the humidity dips below a level you decide for too long. Just be sure to keep the relay on for a while, so the Arduino doesn’t end up turning your devices on and off rapidly if the humidity.