Logo

dev-resources.site

for different kinds of informations.

DHT22 with MicroPython on ESP32

Published at
6/20/2024
Categories
micropython
esp32
arduino
robotics
Author
shemanto_sharkar
Author
16 person written this
shemanto_sharkar
open
DHT22 with MicroPython on ESP32

DHT22 with MicroPython on ESP32

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:

# 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")
Enter fullscreen mode Exit fullscreen mode

Detailed Code Breakdown

  1. Importing Necessary Modules:
   from machine import Pin
   from utime import sleep
   import dht
Enter fullscreen mode Exit fullscreen mode
  • 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.
  1. Setting Up the Sensor:
   sensor = dht.DHT22(Pin(13))
Enter fullscreen mode Exit fullscreen mode
  • 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.
  1. Reading Data Continuously:
   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")
Enter fullscreen mode Exit fullscreen mode
  • 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)
Enter fullscreen mode Exit fullscreen mode

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.

esp32 Article's
30 articles in total
Favicon
ESP32 Weather Dashboard with a WiFi Menu
Favicon
Prototipos rΓ‘pidos con Wokwi, ESP32 y AWS IoT Core
Favicon
Smart Home Security: Advanced Motion Detection with CCTV
Favicon
Matter protocol on a budget
Favicon
Ulanzi TC001 - ESP32 Programming / Custom Arduino firmware
Favicon
Rust on a $2 dev board
Favicon
Sliding Puzzle Next Move Suggesting Simple DL Model with ESP32 TensorFlow Lite
Favicon
Time-for-space reluctance for the Wi-Fi AP scan for ESP8266
Favicon
I created a Realtime Voice Assistant for my ESP-32, here is my journey - Part 2 : Node, OpenAI, Langchain
Favicon
I created a Realtime Voice Assistant for my ESP-32, here is my journey - Part 1 : Hardware, PlatformIO & C++
Favicon
ESP32 Smart Garden: Automating Plant Care with IoT
Favicon
Smart Connectivity: Leveraging Wi-Fi and Bluetooth Coexistence in ESP32
Favicon
Building a DIY Smart Home Device: Get S**t Done
Favicon
Top ESP32 Projects To Start in 2024
Favicon
Why You Should Choose MicroPython for Prototyping and Research Work
Favicon
How to build a smart home system with ESP32
Favicon
Advanced ESP32: Exploring Key Features and Versatile Uses
Favicon
Developing IoT Application with ESP32
Favicon
Simple Arduino Framework Photo Frame Implementation with Photos Downloaded from the Internet via DumbDisplay
Favicon
DHT22 with MicroPython on ESP32
Favicon
MicroPython ESP32: Blink LED
Favicon
Using an OLED Display with MicroPython on ESP32
Favicon
Interfacing HC-SR04 Ultrasonic Sensor with ESP32 Using MicroPython
Favicon
Configurar Arduino IDE para ESP32 en Windows 10 πŸ’»
Favicon
Arduino IDE 2 δΈŠε‚³ζͺ”ζ‘ˆεˆ° ESP32/ESP8266 ηš„ε€–ζŽ›
Favicon
ESP32 WiFiManager MQTT ArduinoJson 7
Favicon
How to Code with Lua on ESP32 with XEdge32
Favicon
Tiny E-Ink Picture Display
Favicon
Esp32 Rust Board on Macos M-chip in Docker
Favicon
ESP32 WiFiManager MQTT Spiffs config ArduinoJson 7

Featured ones: