Many times you might wish to sense the temperature around you. But you can not directly use the sensor just by making the circuit and writing code. You need to use the specific library for the sensor which has many pre-defined functions used by the sensor. In this article we will discuss how to install the library and use the DHT11/ DHT22 temperature sensor for both Arduino and NodeMCU.
Contents:
- How DHT11/ DHT22 sensor works?
- Use of a library
- Downloading library for Arduino board
- Downloading library for NodeMCU
- Extracting code
How DHTxx sensor works ?
The DHT11/ DHT22 consist of a humidity sensing component and a NTC temperature sensor (or thermistor). Furthermore, there is an IC on the back side of the sensor. For measuring humidity, humidity sensing component is used. It has two electrodes with moisture holding substrate between them.
Almost all sensors have an Analog Interface which defines the way they extract information. Consider the DHT11 sensor. It senses all the temperatures that lie in a particular range. This is the analog input which the sensor gives to the microcontroller. But since the controller is a digital device, so this analog is first converted to digital by ADC (Analog to Digital Converter).
Use of a Library
The Arduino environment can be extended through the use of libraries, just like most programming platforms. Libraries provide extra functionality for use in sketches, e.g. working with hardware or manipulating data.
After getting the inputs, various functions in the library process it further. This is where a library plays a role. It stores the data in registers and processes it to output in the user understandable form. However, many times, a DAC (Digital to Analog Converter) is used after the controller has processed the data.
Downloading DHT11/ DHT22 library for an Arduino board
Libraries are majorly downloaded in two ways. First, they can be downloaded directly from the Library Manager. Alternatively, you can download the zipped format from the Internet. Many libraries are not present in the manager. For them, you need to use the second method. Furthermore, you can also create your own libraries. See the API Style Guide for information on making a good Arduino-style API for your library.
However, in our case we can use the first method as the library is available in the manager. So let us begin!
Open the Arduino IDE and click on the Sketch option. In the options listed below, click on the ‘Include Library‘ option. You will see the list of all the libraries installed in your IDE. Now, have a look at the first two options. This is what we discussed in the above paragraph. The second option of Zipped file is used when we want to install a library downloaded from the internet. For using library manager, click on the ‘Manage Libraries‘ option.
After this, you will see the window shown above. Search for the DHTxx library and then install the ‘DHT sensor library‘ as shown.
Great, you have successfully installed the library for the Arduino board.
Downloading the library for NodeMCU
The library used for the ESP8266 is not this same. Rather, there is a different library used for it. You first need to set your ESP board in the IDE. Read this, for how to do so. After that, refer the same steps as above and go to the Library manager. Then install the library named “DHT sensor library for ESPx” as shown below.
Using it with the IDE and extracting code
After successful installation, we can now use the libraries in whatever code we wish to. Also, we can use some inbuilt examples present in the both the libraries. Although they consist of some basic code, but it is enough to see the functionality of the sensor. Refer the image given below.
I hope you were able to get through the article and able to use the libraries for the two boards. Feel free to write in the comment section if you face any difficulty. Happy Learning!