Chapter 24 Hygrothermograph DHT11

In this chapter, we will learn about a commonly used sensor called a Hygrothermograph DHT11.

Project 24.1 Hygrothermograph

Hygrothermograph is an important tool in our lives to give us data on the temperature and humidity in our environment. In this project, we will use the ESP32 to read temperature and humidity data of the DHT11 Module.

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

Jumper M/M x4

Chapter01_05

DHT11 x1

Chapter24_00

Resistor 10kΩ x1

Chapter02_01

Component knowledge

The temperature & humidity sensor DHT11 is a compound temperature & humidity sensor, and the output digital signal has been calibrated by its manufacturer.

../../../_images/Chapter24_01.png

DHT11 uses customized single-line communication protocol, so we can use the library to read data more conveniently.

After being powered up, it will initialize in 1s. Its operating voltage is within the range of 3.3V-5.5V.

The SDA pin is a data pin, which is used to communicate with other devices.

The NC pin (Not Connected Pin) is a type of pin found on various integrated circuit packages. Those pins have no functional purpose to the outside circuit (but may have an unknown functionality during manufacture and test). Those pins should not be connected to any of the circuit connections.

Circuit

Schematic diagram

Chapter24_02

Hardware connection

If you need any support, please feel free to contact us via: support@freenove.com

Chapter24_03

Code

Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.

Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “24.1_Hygrothermograph” and double click “Hygrothermograph.py”.

24.1_Hygrothermograph

../../../_images/Chapter24_13.png

Click “Run current script”. If your DHT11 is connected incorrectly, the following information will be printed in “Shell”.

../../../_images/Chapter24_14.png

Make sure your circuit is correctly connected and you will see the following messages printed in “Shell”.

../../../_images/Chapter24_15.png

The following is the program code:

 1import machine
 2import time
 3import dht
 4
 5DHT = dht.DHT11(machine.Pin(13))
 6
 7while True:
 8    DHT.measure()
 9    print('temperature:',DHT.temperature(),'humidity:',DHT.humidity())
10    time.sleep_ms(2000)

Import machine, time and dht modules.

1import machine
2import time
3import dht

Associate DHT11 with Pin(13).

1DHT = dht.DHT11(machine.Pin(13))

Start DHT11 to measure data once.

1DHT.measure()

Call the built-in function of DHT to obtain temperature and humidity data and print them in “Shell”.

1print('temperature:',DHT.temperature(),'humidity:',DHT.humidity())

Obtain temperature and humidity data once per second and print them out.

1while True:
2    DHT.measure()
3    print('temperature:',DHT.temperature(),'humidity:',DHT.humidity())
4    time.sleep_ms(2000)

Reference

Class dht

Before each use of object dht , please add the statement “ import dht “ to the top of python file.

DHT11(): Object of DHT11

DHT12(): Object of DHT12

DHT11.measure(): Start DHT11 to measure temperature and humidity data once.

DHT11.temperature(): Return temperature data obtained by DHT11.

DHT11.humidity(): Return humidity data obtained by DHT11.

DHT12.measure(): Start DHT12 to measure temperature and humidity data once

DHT12.temperature(): Return temperature data obtained by DHT12.

DHT12.humidity(): Return humidity data obtained by DHT12.

Project 24.2 Hygrothermograph

In this project, we use L2C-LCD1602 to display data collected by DHT11.

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

LCD1602 Module x1

Chapter20_00

Resistor 10kΩ x1

Chapter02_01

Jumper F/M x4

Jumper M/M x4

Chapter24_08

DHT11 x1

Chapter24_00

Circuit

Schematic diagram

Chapter24_09

Hardware connection

If you need any support, please feel free to contact us via: support@freenove.com

Chapter24_10

Code

Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> 24.2_Hygrothermograph”. Select “I2C_LCD.py” and “LCD_API.py”, right click your mouse to select “Upload to /”, wait for “I2C_LCD.py” and “LCD_API.py” to be uploaded to ESP32-WROVER and then double click “Hygrothermograph.py”.

24.2_Hygrothermograph

../../../_images/Chapter24_16.png

Click “Run current script”. The first row of LCD1602 is temperature value and the second row is humidity. Try to “pinch” the DHT11 (without touching the leads) with your index finger and thumb for a brief time, you should see that the displayed value on LCD1602 changes.

../../../_images/Chapter24_17.png

The following is the program code:

 1from time import sleep_ms
 2from machine import I2C, Pin
 3from I2C_LCD import I2cLcd
 4import dht
 5
 6DHT = dht.DHT11(Pin(18))
 7i2c = I2C(scl=Pin(14), sda=Pin(13), freq=400000)
 8devices = i2c.scan()
 9if len(devices) == 0:
10    print("No i2c device !")
11else:
12    for device in devices:
13        print("I2C addr: "+hex(device))
14        lcd = I2cLcd(i2c, device, 2, 16)
15
16try:
17    while True:
18        DHT.measure()
19        lcd.move_to(0, 0)
20        lcd.putstr("Temperature: ")
21        lcd.putstr(str(DHT.temperature()))
22        lcd.move_to(0, 1)
23        lcd.putstr("Humidity:    ")
24        lcd.putstr(str(DHT.humidity()))
25        sleep_ms(2000)
26except:
27    pass

Import DHT11 and I2C LCD1602 modules.

1from time import sleep_ms
2from machine import I2C, Pin
3from I2C_LCD import I2cLcd
4import dht

Assign Pin(18) to DHT11, Pin(13) and Pin(14) to LCD1602.

1while True:
2    DHT.measure()
3    lcd.move_to(0, 0)
4    lcd.putstr("Temperature: ")
5    lcd.putstr(str(DHT.temperature()))
6    lcd.move_to(0, 1)
7    lcd.putstr("Humidity:    ")
8    lcd.putstr(str(DHT.humidity()))
9    sleep_ms(2000)