

Similarly, to read temperature use the readTemperature() method. Note that the readHumidity() method returns a value of type float. In this case, we’re saving the humidity in the h variable. To get humidity, you just need to use the readHumidity() method on the dht object. Reading temperature and humidity is very simple. The maximum sampling rate is two seconds for the DHT22 and one second for the DHT11. This delay is needed to give enough time for the sensor to take readings. In the loop(), at the beginning, there’s a delay of 2 seconds.
ARDUINO SENSORS EXAMPLE CODE SERIAL
In the setup(), initialize the Serial Monitor at a baud rate of 9600 for debugging purposes. Then, initialize a DHT object called dht with the pin and type you’ve defined previously: DHT dht(DHTPIN, DHTTYPE) If you’re using another DHT sensor, you need to comment the previous line and uncomment one of the following: //#define DHTTYPE DHT22 // DHT 22 (AM2302) Then, you need to define the DHT sensor type you’re using. #define DHTPIN 2 // what digital pin we're connected to In this case it is connected to digital pin 2. Then, you define the pin that the DHT sensor is connected to. You start by including the DHT library: #include "DHT.h" Serial.println("Failed to read from DHT sensor!") Check if any reads failed and exit early (to try again). Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) Reading temperature or humidity takes about 250 milliseconds! Wait a few seconds between measurements. Initialize DHT sensor for normal 16mhz Arduino #define DHTPIN 2 // what pin we're connected to Example testing sketch for various DHT humidity/temperature sensors It reads temperature and humidity, and displays the results in the Serial Monitor.

In your Arduino IDE, go to File > Examples > DHT Sensor library > DHTtester
ARDUINO SENSORS EXAMPLE CODE CODE
CodeĪfter installing the necessary libraries, you can upload an example code from the library. You can use the preceding links or go directly to /tools to find all the parts for your projects at the best price!įollow the next schematic diagram to wire the DHT11 (or DHT22) temperature and humidity sensor to the Arduino.Īfter installing the libraries, restart your Arduino IDE. Arduino UNO – read Best Arduino Starter Kits.To complete this tutorial, you need the following components: In this section, we’ll build a simple project with the Arduino that reads temperature and humidity and displays the results on the Serial Monitor. You can check Maker Advisor Tools‘ page and find the best price for these modules:ĭHT11 Temperature and Humidity Sensor with Arduino When the sensor is facing you, pin numbering starts at 1 from left to right DHT pinĪny digital GPIO also connect a 10k Ohm pull-up resistor The following table shows the DHT22 and DHT11 pinout. However, if you get your DHT sensor in a breakout board, it comes with only three pins and with an internal pull-up resistor on pin 2. DHT PinoutĭHT sensors have four pins as shown in the following figure. You just need to select in the code the sensor type you’re using. It’s also a bit cheaper.ĭespite their differences, they work in a similar way, and you can use the same code to read temperature and humidity. However, you can request sensor readings every second. The DHT11 has a smaller range and it’s less accurate. However, it is a bit more expensive, and you can only request readings with 2 seconds interval. The DHT22 sensor has a better resolution and a wider temperature and humidity measurement range. For a more in-depth analysis of these sensors, please check the sensors’ datasheet. The following table compares some of the most important specifications of the DHT11 and DHT22 temperature and humidity sensors. The DHT11 and DHT22 are very similar, but differ in their specifications.
