028 - MicroPython TechNotes: DHT11 Temperature and Humidity Sensor

Introduction

In this article, we will learn how to use a DHT11 sensor with ESP32 using MicroPython programming language.

Pinout

  1. G – for the ground pin.
  2. V – for the supply voltage.
  3. S – for the signal pin.

Bill Of Materials

  1. ESP32 development board to serve as the brain of the experiment.
  2. GorillaCell ESP32 shield to extend ESP32 pins to pin headers for easy circuit connection.
  3. 3-pin female-female dupont jumper wires to attach DHT11 sensor module to ESP32 shield.
  4. DHT11 sensor module.

Hardware Instruction

  1. First, attach the ESP32 on top of the ESP32 shield and make sure that both USB ports are on the same side.
  2. Next, attach the dupont wires to DHT11 sensor module by following the color coding which is black for the GND, red for the VCC, and yellow for the SIGNAL pin.
  3. Next, attach the other side of the dupont wires to the ESP32 shield by matching the colors of the wires to the colors of the pin headers which is black to black, red to red, and yellow to yellow pin headers. For this experiment, I choose GPIO 23 to serve as the signal pin from DHT11 sensor module.
  4. Next, power the ESP32 shield with an external power supply with a type-C USB cable and make sure that the power switch is set to ON state.
  5. Lastly, connect the ESP32 to the computer with a micro-USB cable.

Software Instruction

  1. Copy and paste to Thonny the example source codes.
  2. Play with it and adapt it according to your needs.
  3. Enjoy learning.

Video Demonstration

Call To Action

For any concern, write your message in the comment section.

You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.

Thank you and have a good days ahead.

See you,

– George Bantique | tech.to.tinker@gmail.com

Source Code

1. Example # 1, basics of using DHT11:

 1
 2# More details can be found in TechToTinker.blogspot.com 
 3# George Bantique | tech.to.tinker@gmail.com
 4
 5from machine import Pin
 6from dht import DHT11
 7d = DHT11(Pin(23))
 8
 9# # This line of code should be tested using the REPL
10# d.measure()
11# d.temperature()
12# d.humidity()

2. Example # 2, display DHT11 sensor readings to 16×2 LCD:

 1
 2# More details can be found in TechToTinker.blogspot.com 
 3# George Bantique | tech.to.tinker@gmail.com
 4
 5from machine import Pin
 6from time import sleep_ms
 7from dht import DHT11
 8from machine import SoftI2C
 9from i2c_lcd import I2cLcd
10
11d = DHT11(Pin(23))
12
13DEFAULT_I2C_ADDR = 0x20
14i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000) 
15lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)
16
17lcd.clear()
18lcd.move_to(0, 0)
19lcd.putstr("Temp:    ^C.")
20lcd.move_to(0, 1)
21lcd.putstr("Humi:    %.")
22
23while True:
24    d.measure()
25    lcd.move_to(6, 0)
26    lcd.putstr(str(d.temperature()))
27    lcd.move_to(6, 1)
28    lcd.putstr(str(d.humidity()))
29    sleep_ms(3000)

3. Example # 3, simple application of DHT11:

 1
 2# More details can be found in TechToTinker.blogspot.com 
 3# George Bantique | tech.to.tinker@gmail.com
 4
 5from machine import Pin
 6from time import sleep_ms
 7from dht import DHT11
 8from machine import SoftI2C
 9from i2c_lcd import I2cLcd
10
11d = DHT11(Pin(23))
12
13DEFAULT_I2C_ADDR = 0x20
14i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000) 
15lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)
16
17lcd.clear()
18lcd.move_to(0, 0)
19lcd.putstr("Temp:    ^C.")
20lcd.move_to(0, 1)
21lcd.putstr("Humi:    %.")
22
23count = 0
24
25while True:
26    d.measure()
27    t = d.temperature()
28    h = d.humidity()
29    lcd.move_to(6, 0)
30    lcd.putstr(str(t))
31    lcd.move_to(6, 1)
32    lcd.putstr(str(h))
33    if t >= 30:
34        count += 1
35        print("Alert! Count {}".format(count))
36    else:
37        count = 0
38        
39    sleep_ms(1000)

References And Credits

  1. Purchased your Gorillacell ESP32 development kit at: https://gorillacell.kr


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.