This project teaches how to interface with a digital sensor to read environmental data like temperature and humidity. Students will learn about digital sensor communication protocols.
Connect the VCC pin of the DHT11 module to the 3.3V or 5V pin on the ESP32 (check the module’s voltage requirements; it often works with both). Connect the GND pin to GND on the ESP32. Connect the DATA pin of the DHT11 module to a digital GPIO pin on the ESP32. The DHT11 typically has an internal pull-up resistor, but if not, a 10k ohm pull-up resistor between the DATA pin and VCC is recommended.
The MicroPython code will use a library (often available for MicroPython) to communicate with the DHT11 sensor. The code will initiate a reading from the sensor, wait for the data, and then parse the received data to obtain the temperature and humidity values. These values will be printed to the console.
A web interface could display the current temperature and humidity readings, potentially with a timestamp.
# This script reads temperature and humidity from a DHT11 sensor # and displays the readings on an OLED screen. from machine import Pin, I2C # Import Pin and I2C import time # Import time # Import DHT11 and SSD1306 libraries. try: import dht except ImportError: print("DHT library not found. Please upload 'dht.py'.") class MockDHT11: def __init__(self, pin): print(f"MockDHT11 initialized on pin {pin}") def measure(self): print("MockDHT11 measure() called. (DHT library not available)") def temperature(self): return -999 def humidity(self): return -999 dht = type('dht', (object,), {'DHT11': MockDHT11})() try: import ssd1306 except ImportError: print("ssd1306.py library not found. Please upload it.") class MockSSD1306: def __init__(self, width, height, i2c): print(f"MockSSD1306 initialized ({width}x{height}).") def fill(self, color): pass def text(self, text, x, y, color): pass def show(self): print(f"OLED display content not shown (ssd1306 library missing): {text}") ssd1306 = type('ssd1306', (object,), {'SSD1306_I2C': MockSSD1306})() # --- DHT11 Setup --- dht_pin = 2 sensor = dht.DHT11(Pin(dht_pin)) # --- OLED Setup --- i2c_sda = 21 i2c_scl = 22 oled_width = 128 oled_height = 32 i2c = I2C(0, sda=Pin(i2c_sda), scl=Pin(i2c_scl), freq=400000) oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c) print("Starting DHT11 monitoring with OLED display...") # Start an infinite loop to continuously read and display sensor data. while True: try: # Perform a measurement. sensor.measure() # Get the readings. temperature_c = sensor.temperature() humidity = sensor.humidity() # --- Display on OLED --- oled.fill(0) # Clear display # Display temperature and humidity. # With a 128x32 screen, we have about 4 lines of text. oled.text("Temp: {:.1f} C".format(temperature_c), 0, 0, 1) oled.text("Humidity: {:.1f} %".format(humidity), 0, 16, 1) # Second line # Update the OLED display. oled.show() # Print to console as well. print("Temperature: {:.1f}°C, Humidity: {:.1f}%".format(temperature_c, humidity)) except OSError as e: print("Failed to read from DHT11 sensor:", e) # Optionally display error on OLED oled.fill(0) oled.text("DHT11 Error!", 0, 0, 1) oled.text(str(e), 0, 16, 1) oled.show() # Wait before the next reading. time.sleep(5) # DHT11 requires at least 1-2 seconds between readings # This loop will run forever. # Troubleshooting notes: # - See troubleshooting notes from Project 6 and Project 2 (OLED). # - Ensure both dht.py and ssd1306.py are on the ESP32.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.