Now it’s getting serious: Using your new libraries, you’ll measure temperature and humidity with the DHT11 and display these values in your Serial Monitor.
Let’s first take a look at the libraries.
Importing Libraries in the Sketch
To use a library (that you’ve previously installed), you need to import it at the beginning of your sketch. This allows your program to access the functions stored in the library.
You can easily import the library for your sensor with a single line of code:
#include "DHT.h"
___STEADY_PAYWALL___
As you can see, you use the #include
command followed by the name of the library with the extension .h in quotation marks. Important: Lines with #include
should not end with a semicolon ; – an exception that would otherwise lead to an error.
You don’t need to import the Adafruit Unified Sensor library, as it only works in the background.
Often the names of libraries in the library manager and after the #include
command are identical. In this case, they are exceptionally not the same – please don’t let this confuse you.
By the way: Almost all libraries install examples after installation. You can find them in the menu File -> Examples. For our library, select the entry DHT sensor library. There you’ll find example sketches with more information about importing and functions.
Specifying Connection and Sensor
So that the library knows which pin the sensor is connected to and which sensor (DHT11 or DHT22, the “bigger brother” of the DHT11) it is, you must first define these two parameters:
#define DHTPIN 4
#define DHTTYPE DHT11
You also do this at the beginning of the sketch, right after importing the library. For this, you use the function #define
– which, like #include
, does not end with a semicolon ;.
Variables for Temperature & Humidity
To temporarily store your measurements, you need two variables – one for temperature and one for humidity:
float temp;
float humidity;
Since your sensor also measures tenths of a degree or percentage, these variables must be of type float.
Starting the Sensor
Next, you create an object based on the library called dht. You provide this instance with the connection pin and sensor type defined above:
DHT dht(DHTPIN, DHTTYPE);
Now follows the setup function. Here you first start the Serial Monitor and then the sensor:
void setup() {
Serial.begin(9600);
dht.begin();
}
Measuring and Displaying Values
Time for the loop. Here you can use two functions provided by the sensor’s library. With these functions, you store the current measured values in the respective variables:
temp = dht.readTemperature();
humidity = dht.readHumidity();
As you’ve probably already recognized, the function dht.readTemperature()
reads the temperature and dht.readHumidity()
reads the humidity. You can assign these values to variables as usual, which you can then use later.
The way the functions are written is interesting. At the beginning, you see the object dht – this “knows” by now which sensor you’re using and which pin it’s connected to. After that – separated by a dot – you access a “sub-function” provided by your object: either readTemperature()
or readHumidity()
.
Next, you display your measurements in the Serial Monitor. You already know how this works from previous lessons. As you can see, the two variables temp and humidity are now used:
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
To ensure your sensor doesn’t rush from one measurement to the next, end the loop with a delay – e.g., two seconds:
delay(2000);
And that’s it! Upload the sketch from the Exercise Files for this lesson to your Arduino and open your Serial Monitor. You should now see the current temperature and humidity.