If you want to get the temperature in Fahrenheit degrees, you need to pass the true parameter as argument to the readTemperature() method. Getting the humidity and temperature is as easy as using the readHumidity() and readTemperature() methods on the dht object, as shown below: float h = dht.readHumidity() We create float variables h, t, and f to save the humidity, temperature in Celsius and temperature in Fahrenheit, respectively. The temperature and humidity are returned in float format. So, we can only get readings every two seconds. The loop() starts with a 2000 ms (2 seconds) delay, because the DHT22 maximum sampling period is 2 seconds. Serial.begin(9600) įinally, initialize the DHT sensor. In the setup(), initialize the Serial debugging at a baud rate of 9600, and print a message in the Serial Monitor. #define DHTTYPE DHT11 // DHT 11Ĭreate a DHT object called dht on the pin and with the sensor type you’ve specified previously. In this case, we’re using the DHT22 sensor. Uncomment the sensor type you’re using and comment all the others. The library supports DHT11, DHT22, and DHT21. Then, you need to select the DHT sensor type you’re using. #define DHTPIN 4 // Digital pin connected to the DHT sensor Then, define the digital pin that the DHT sensor data pin is connected to. How the Code Worksįirst, you need to import the DHT library: #include "DHT.h" Continue reading to learn how the code works. So, you might want to take a look at the comments. There are many comments throughout the code with useful information. Compute heat index in Celsius (isFahreheit = false)įloat hic = dht.computeHeatIndex(t, h, false) Compute heat index in Fahrenheit (the default) Serial.println(F("Failed to read from DHT sensor!")) Check if any reads failed and exit early (to try again). Read temperature as Fahrenheit (isFahrenheit = true) Read temperature as Celsius (the default) 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. as the current DHT reading algorithm adjusts itself to work on faster procs. tweak the timings for faster processors. Note that older versions of this library took an optional third parameter to Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor Connect pin 4 (on the right) of the sensor to GROUND Connect pin 2 of the sensor to whatever your DHTPIN is NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 Connect pin 1 (on the left) of the sensor to +5V Pin 15 can work but DHT must be disconnected during program upload. REQUIRES the following Arduino libraries: Example testing sketch for various DHT humidity/temperature sensors written by ladyada Copy the following code to your Arduino IDE. To read temperature and humidity from the DHT sensor, we’ll use an example based on the Adafruit DHT library. ESP32 Reading Temperature and Humidity Sketch 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.Īfter installing the libraries, restart your Arduino IDE. 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. Guide for DHT11/DHT22 Humidity and Temperature Sensor with Arduino.If you’re looking to use these sensors with the Arduino board, you can read the following tutorial: This makes them very easy to use with any microcontroller. These sensors contain a chip that does analog to digital conversion and spit out a digital signal with the temperature and humidity.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |