EMB3: Use of integrated temperature and humidity sensor

In this scenario, we will introduce a popular DHT11 sensor. The DHT series covers DHT11, DHT22, and AM2302. Those sensors differ in accuracy and physical dimensions but can all read environmental temperature and humidity. This scenario can be run stand-alone to read weather data in the laboratory nodes' room. The DHT11 sensor is controlled with one GPIO (in all our laboratory nodes, it is GPIO 47) and uses a proprietary protocol.


Air temperature and humidity can be easily read using a dedicated library. Actually, you need to include two of them, as presented below:

  lib_deps = 
             adafruit/DHT sensor library@^1.4.4
	     adafruit/Adafruit Unified Sensor@^1.1.9

Sensor readings can be sent over the network or presented on one of the node's displays (e.g. LCD), so understanding how to handle at least one of the displays is essential:

It is also possible to present the temperature as the LED colour change with PWM controlled LED or LED stripe. Their usage is described here:

A good understanding of the hardware timers is essential if you plan to use asynchronous programming (see note below). Consider getting familiar with the following:

Suggested Readings and Knowledge Resources

Hands-on Lab Scenario

In this scenario, we only focus on reading the sensor (temperature and humidity). Information on how to display measurements is part of other scenarios that you should refer to to create a fully functional solution (see links above).

Task to be implemented

Present the current temperature, and humidity on any display (e.g. LCD). Remember to add units (C, %Rh).


A general check to see if you can see the chosen display in the camera field of view is necessary. No other actions are required before starting development.


The steps below present only interaction with the sensor. Those steps should be supplied to present the data (or send it over the network) using other scenarios accordingly.

Step 1

Include the DHT library and related sensor library.

#include <Adafruit_Sensor.h>
#include <DHT.h>

Step 2

Declare type of the sensor and GPIO pin:

#define DHTTYPE DHT11   // DHT 11
#define DHTPIN 47

Step 3

Declare controller class and variables to store data:

static DHT dht(DHTPIN, DHTTYPE,50);
static float hum = 0;
static float temp = 0;
static boolean isDHTOk = false;

Step 4

Initialise sensor (mind the delay(100); after initialisation as the DHT11 sensor requires some time to initialise before one can read the temperature and humidity:


Step 5

Reat the data and check whether the sensor works OK. In the case of the DHT sensor and its controller class, we check the correctness of the readings once the reading is finished. The sensor does not return any status but checks if the reading is okay. This can be done by comparing the readings with the NaN (not a number) value, each separately:

   hum = dht.readHumidity();
   temp = dht.readTemperature();
   (isnan(hum) || isnan(temp))?isDHTOk = false:isDHTOk = true;
Do not read the sensor too frequently! Doing so will cause lots of NaN numbers. Please give it some 250ms, at least, between consecutive readings, whether you do it asynchronously or using a blocking call of delay(250); in the loop.

Result validation

The observed temperature is usually between 19 and 24C, with humidity about 40-70%, depending on the weather. On rainy days, it can even go higher.

If you ever notice the temperature going beyond 25C, please drop a note to the administrators: it means that our air conditioning is faulty and needs maintenance ;-).


I've got NaN (Not a Number) readings. What to do?: Check if GPIO is OK (should be D22), check if you initialised controller class and most of all, give the sensor some recovery time (at least 250ms) between consecutive readings.

Project information

This Intellectual Output was implemented under the Erasmus+ KA2.
Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, companies and labour force in the global IoT market.
Project number: 2022-1-PL01-KA220-HED-000085090.

Erasmus+ Disclaimer
This project has been funded with support from the European Commission.
This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein.

Copyright Notice
This content was created by the IOT-OPEN.EU Reloaded consortium, 2022,2024.
The content is Copyrighted and distributed under CC BY-NC Creative Commons Licence, free for Non-Commercial use.

en/iot-open/practical/hardware/sut/esp32/emb3_1.txt · Last modified: 2024/04/22 17:08 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0