DHT22 with MicroPython on ESP32
Tags: micropython, esp32, arduino, robotics
Published: 2024-06-20 07:56:29


![DHT22 with MicroPython on ESP32](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzkedroibnegnxz5hdy2.png)


#### Introduction

In this tutorial, we will learn how to interface a DHT22 temperature and humidity sensor with an ESP32 microcontroller using MicroPython. The DHT22 is a reliable sensor for measuring temperature and humidity, making it perfect for environmental monitoring projects.

#### Prerequisites

Before we dive into the code, ensure you have the following:
- ESP32 microcontroller
- DHT22 sensor
- Breadboard and jumper wires
- MicroPython installed on the ESP32
- Thonny IDE or any other suitable IDE for writing and uploading MicroPython code

#### Code Explanation

Here is the complete code to read temperature and humidity from the DHT22 sensor and print the values to the console:

```python
# code written by Shemanto Sharkar (let's connect on LinkedIn: https://www.linkedin.com/in/shemanto/)
# step-1: importing necessary modules
from machine import Pin
from utime import sleep
import dht

# step-2: telling ESP32 where our sensor's data pin is connected
sensor = dht.DHT22(Pin(13))

# step-3: reading data continuously inside loop
while True:
  try:
    sensor.measure()
    t = sensor.temperature()
    h = sensor.humidity()

    print("temp=" + str(t))
    print("humidity=" + str(h))
    sleep(2)
    print("")
    
  except OSError as e: # Error Handling  
    print("Error Data")
```

#### Detailed Code Breakdown

1. **Importing Necessary Modules:**
   ```python
   from machine import Pin
   from utime import sleep
   import dht
   ```
   - `from machine import Pin`: Imports the `Pin` class from the `machine` module, used for configuring GPIO pins.
   - `from utime import sleep`: Imports the `sleep` function from the `utime` module for introducing delays.
   - `import dht`: Imports the DHT library to interact with the DHT22 sensor.

2. **Setting Up the Sensor:**
   ```python
   sensor = dht.DHT22(Pin(13))
   ```
   - `sensor = dht.DHT22(Pin(13))`: Initializes the DHT22 sensor on GPIO pin 13. This line tells the ESP32 where the sensor's data pin is connected.

3. **Reading Data Continuously:**
   ```python
   while True:
     try:
       sensor.measure()
       t = sensor.temperature()
       h = sensor.humidity()

       print("temp=" + str(t))
       print("humidity=" + str(h))
       sleep(2)
       print("")
    
     except OSError as e:
       print("Error Data")
   ```
   - `while True`: Starts an infinite loop to continuously read data from the sensor.
   - `sensor.measure()`: Triggers the sensor to measure temperature and humidity.
   - `t = sensor.temperature()`: Reads the temperature value from the sensor.
   - `h = sensor.humidity()`: Reads the humidity value from the sensor.
   - `print("temp=" + str(t))`: Prints the temperature value to the console.
   - `print("humidity=" + str(h))`: Prints the humidity value to the console.
   - `sleep(2)`: Introduces a delay of 2 seconds before the next reading.
   - `except OSError as e`: Catches any errors that occur during the reading process and prints an error message.

#### Diagram

Here's a diagram illustrating the connections:

```
ESP32 Microcontroller:
----------------------
        ___________
       |           |
       |           |
       |           |
       |    13     |--------> DHT22 (Data Pin)
       |           |
       |           |
       |___________|
         |
         |
       GND
       VCC (3.3V)
```

**Connections:**
- Connect the VCC pin of the DHT22 to the 3.3V pin of the ESP32.
- Connect the GND pin of the DHT22 to the GND pin of the ESP32.
- Connect the Data pin of the DHT22 to GPIO pin 13 of the ESP32.

#### Conclusion

By following this tutorial, you will be able to read temperature and humidity data from a DHT22 sensor using an ESP32 microcontroller with MicroPython. This basic setup can be extended for various applications like weather stations, smart home systems, and more. Happy coding!

If you have any questions or need further assistance, feel free to reach out on LinkedIn: [Shemanto Sharkar](https://www.linkedin.com/in/shemanto/).