In this MicroPython ESP32 Tutoria - Interfacing DHT11 Sensor, we will use the MicroPython code on MakePython ESP32 and DHT11 Humidity Temperature Sensor.
1. Overview
In this project, we will interface DHT11 Humidity Temperature Sensor with ESP32 using MicroPython Code. We will first interface the Sensor with ESP32 only and check the temperature and humidity reading in Shell Window. Similarly, we will add extra 0.96″/1.3″ I2C OLED Display to the same circuit and display the Humidity Temperature reading on OLED Screen.
But before beginning, I will highly recommend you to follow the following tutorial to boost up your MicroPython skill. I have explained how you can install MicroPython & use uPyCraft IDE. If you are aware of all this, then you can skip this step: Getting Started with MicroPython on ESP32 using PyCraft IDE.
2. DHT11 Humidity Temperature Sensor
The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed). You can find it from: https://www.makerfabs.com/dht11-temperature-humidity-module.html.
The sensor comes with a dedicated NTC to measure temperature and an 8-bit microcontroller to output the values of temperature and humidity as serial data. The sensor is also factory calibrated and hence easy to interface with other microcontrollers. The sensor can measure temperature from 0°C to 50°C and humidity from 20% to 90% with an accuracy of ±1°C and ±1%.
3. Interfacing DHT11 Sensor with ESP32 using MicroPython Code
Now let us assemble DHT11 Sensor & ESP32 & start with MicroPython Code for measuring Humidity & Temperature. The connection is fairly simple. Connect the DHT11 VCC & GND pin to ESP32 3.3V Pin & GND Pin. Connect the digital output pin to ESP32 GPIO5 Pin.
In my case I am using MakePython ESP32 Board. The connection for this Board is as shown in the image below.
4. MicroPython Code for ESP32 with DHT11 Sensor
The MiroPython Code for Interfacing DHT11 with ESP32 is given below.
Open uPyCraft IDE and open a New Tab. Paste the below Code and save it with a name “main.py”. Now you can connect the ESP32 Board & Upload the Code by clicking on Download button.
from machine import Pin from time import sleep import dht sensor = dht.DHT11(Pin(5)) while True: try: sleep(2) sensor.measure() t = sensor.temperature() h = sensor.humidity() print('Temperature: %3.1f C' %t) print('Humidity: %3.1f %%' %h) except OSError as e: print('Sensor Reading Failed') |
As soon as the code is uploaded, you can see the result in below Console Window or uPyCraft Shell.
5. Monitor DHT11 Humidity Temperature Data on OLED with MicroPython Code
Now let us add an extra OLED Display to the circuit. We will now display the humidity temperature data on OLED Display instead of uPyCrfat Window Shell.
Here is the connection diagram. The OLED Display is an I2C Module. So connect the SDA & SCL pin of OLED Display to ESP32 D21 & D22 Pin respectively.
In case if you are using MicroPython ESP32 Board, there is no need for connecting an OLED Display as it already has 1.3″ I2C OLED Display connected to it. Its SDA & SCL pin is connected to GPIO5 & GPIO4 respectively.
6. ESP32 DHT11 OLED Display MicroPython Code
The MicroPython Code for Interfacing OLED Display with ESP32 has two part.
1. SSD1306 Library
2. Main Code
The library to write to the OLED display isn’t part of the standard MicroPython library by default. So, first we need to upload the library to the ESP32 board.
6.1 SSD1306.py
First open a New Tab on uPyCraft IDE and paste the below code. Then save the file with the name “SSD1306.py“. Now you can upload the code.
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces currentBoard="" class SSD1306_I2C(SSD1306): def write_data(self, buf): class SSD1306_SPI(SSD1306): |
6.2 main.py
Now after uploading the code for SSD1306, you can open a new tab and paste the following code below. Make sure to define the SCL & SDA Pin in the code according to your ESP32 Board.
You can now upload the code.
from machine import Pin i2c = I2C(scl=Pin(5), sda=Pin(4)) #Init i2c p15=Pin(22, Pin.IN) while True: |
Once the code is uploaded, the OLED Display will start displaying the humidity and temperature value on OLED Screen.
This article is originally posted on How2electronics. If you have any further questions or need some PCBA customizations based on those MakePython IoT boards, feel free to contact service@makerfabs.com.