Electronic components: the DHT11 temperature and humidity sensor
This sensor is one of the first sensors you learn to use because everyone has a good application for it: building an indoor or outdoor thermometer.
Here you can see it mounted on a breakout board:



Note that the sensor has 4 output pins, although the breakout board I got only has 3 (the reason being the pin 3 of DHT11 is not connected to anything - don’t ask me why).
From the left, keeping our pins at the bottom, we have:
Vdd, the positive input (+)Vss, the negative input (-)- the output signal
The output is a 40-bit serialized signal that lasts 4ms.
This means that every 4ms the sensor sends the temperature information and to read it we must get the value and unserialize it.
In the Arduino program we use the DHT Sensor Library maintained by Adafruit (here’s the link to the repository) that makes it very simple for us to read the temperature:

You include it with #include <DHT.h>, then you initialize an object of class DHT by passing the sensor signal pin and the type, in this case using the constant DHT11.
The library can also work with other sensors like the more precise
DHT22andDHT21
Then you can call the readHumidity() and readTemperature() methods to get values as a float variable.
readTemperature() gets the value as Celsius, but the library also provides a convertCtoF() method to get the Fahrenheit value.
The library also provide other methods, like computeHeatIndex(). I recommend you to checkout the DHT.h header file source code on GitHub.
This simple Arduino program reads the data from a DHT11 connected with the signal pin on digital pin #2 and prints it to the serial monitor:
#include <DHT.h>
DHT dht(2, DHT11);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Cannot read values");
return;
}
Serial.println((String)"Humidity: " + h + "%, temperature: " + t + "C");
}


Humidity: 56.00%, temperature: 20.20C
Humidity: 56.00%, temperature: 20.20C
Humidity: 56.00%, temperature: 20.10C
Humidity: 56.00%, temperature: 20.20C download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.