DHT11 is a low-cost digital sensor for sensing temperature and humidity and can be easily interfaced with ESP32 on Micropython.
DHT11 sensor consists of a capacitive humidity sensing element and a thermistor for sensing temperature.
Temperature range of DHT11 is from 0 to 50 degree Celsius with a 2-degree accuracy.
Humidity range of this sensor is from 20 to 80% with 5% accuracy.
The sampling rate of this sensor is 1Hz .i.e. it gives one reading for every second.
DHT11 operating voltage from 3 to 5 volts. The maximum current consumed is 2.5mA.
DHT11 sensor has four pins- VCC, GND, Data Pin and a not connected pin. A pull-up resistor
of 5k to 10k ohms is needed for communication between sensor and micro-controller.
When you buy as Module , the resistor is provided on pcb , and only 3 pins available.
On module there is also a power LED provided.
Connect GPIO 15 —> Data pin of DHT11
Gnd to Gnd , Vcc to 3.3v
OLED connections :
SCL –> GPIO 22
SDA –> GPIO 21
Gnd to Gnd , Vcc to 3.3v
Thonny Python is used as IDE to develop and upload code on to ESP32.
OLED driver module (library) to be downloaded from :
https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py
File –> open
browse to the location of ssd1306.py file to select it.
File –> Save as
select Micropython device and save it as ssd1306.py file.
From interpreter of Thonny Python code can be tested line by line.
Import from machine module Pin and I2C Class.
Import dht module , which is inbuilt in Micropython.
Declare Pin 15 WHERE WE CONNECT data OF dht11 AS iNPUT.
Create d , a dht object using class DHT11 ( from dht module) and pass on the argument pin 15.
DHT11 Class is in module dht.
so we use dht.DHT11 instantiate an object from that class.
measure() method is called to read the temperature and humidity values.
temperature() method gives temp value
humidity() method gives % of humidity.
Driver module for OLED and main.py files can be downloaded from :
http://www.alselectro.com/files/micropython_oled_dht_ultrasonic.zip
You can open the downloaded main.py file in Thonny python.
Click File –> Save as and select Micropython device
save as main.py file.
Do not change the name.It must be main.py , as Micropython after executing boot.py looks for main.py
In the example code
we import classes Pin,I2C
Then we import modules ssd1306, dht and time
i2c is initiated with scl pin GPIO22 & sda pin GPIO21
oled object is initiated by passing arguments width 128, height 64, protocol i2c and address 0x3c.
d object is initiated using dht.DHT11 Class by passing on GPIO15
Inside never ending loop
measure() method used to read temperature and humidity values.
temperature()
humidity()
methods used to get the result.
While printing on OLED we need to convert the data to STRING
oled.text( str ( t ),80,20)
Click on F5 to execute.
You can see the Temperature reading in Celcius & Humidity reading in % on OLED.
Video :