0

$USD

$USD
TUTORIALS ESP32

ESP32 Espruino DHT22 Tutorial: Getting temperature and humidity

DFRobot Jul 30 2018 1964

Introduction

In this esp32 tutorial we will check how to get temperature and humidity measurements from a DHT22 sensor using Espruino, running on the ESP32.

To use a simple interface that abstracts from us the complexity of interacting with the DHT22 using the single wire protocol it supports, we will use Espruino’s DHT22 module.

Important: The DHT library doesn’t work in some older Espruino firmware versions. My recommendation is to update to the latest version available on the Espruino download page. For these tests I’m using version 1.99.

To facilitate the interaction with the sensor, I’m using a DFRobot DHT22 module which already has all the electronics needed and exposes a wiring terminal that facilitates the connections.

The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.


The electric diagram

As stated in the introductory section, I’m assuming the use of a ready to use DHT22 sensor module, which contains all the electronics needed for everything to work. Thus, when using such a module, we can simply connect its data pin directly to a GPIO of the ESP32, as illustrated in figure 1.

ESP32 DHT22 electric schematic

Figure 1 – Connection diagram between the ESP32 and the DHT22 module.

Depending on your module, the labels may be different or it may not be labeled at all. In my case, for the module I’m using, there are no labels on the board and we need to take in consideration the colors of the wires connected to it:

  • Black: GND
  • Red: VCC
  • Green: Data

Depending on your ESP32 board, it may be able to provide the power needed for the DHT22 to work from a power pin. If you are not sure if your board has one, my recommendation is to use an external power supply such as this.


The code

As usual, the code needed to get the measurements is very simple and compact, due to the fact that we are using a higher level framework.

We will start by declaring the number of the ESP32 GPIO that is connected to the DHT22 module. I’ve used pin 25 but you can test with others.

var dhtPin =25;

In order to avoid having to worry about the lower level details of the communication protocol between the microcontroller and the ESP32, we will use an Espruino module that will handle that interaction for us.

To load a module, we simple use the require function, passing as input the name of the module [1]. When we use the require function, upon sending the code to the board, the Espruino IDE will automatically look online for the module, download it and load it into the board [1].

After loading the module, we can simply call the connect method on the returned object, passing as input the number of the ESP32 pin connected to the sensor [2]. This will return a DHT22 object from which we can get the measurements [2].

var dht = require("DHT22").connect(dhtPin);

To get the actual measurements, we simply need to call the read method of the DHT22 object [2].

This method receives as single input a function that is executed when the read operation is complete. As argument of this user defined function, the read method will pass an object that contains both the temperature and humidity measurements [2].

The temperature can be obtained from the temp property of the mentioned object, and the humidity from its rh property. We can then print the values of those properties inside the function we will pass to the read method.

dht.read(function (measurements) {

    console.log("Humidity: " + measurements.rh + "%");
    console.log("Temperature: " + measurements.temp + "ºC");

});

The final complete code can be seen below.

var dhtPin =25;

var dht = require("DHT22").connect(dhtPin);

dht.read(function (measurements) {

    console.log("Humidity: " + measurements.rh + "%");
    console.log("Temperature: " + measurements.temp + "ºC");

});


Testing the code

To test the previous code, simply upload the previous script to your ESP32 board, using the Espruino IDE. As already mentioned, it will be during the loading procedure that the DHT22 module will be loaded, so it may take a while before the code starts executing.

After the execution finishes, you should get an output similar to figure 2, which shows the two measurements being printed.

ESP32 Espruino DHT22 get temperature and humidity.png

Figure 2 – Obtaining temperature and humidity with Espruino and a DHT22 sensor.