OC
OceanRemote
Low-code IoT platform
← Back to Course

DHT22 Temperature and Humidity Sensor picow

DHT22 Temperature and Humidity Sensor picow

🌑️ DHT22 Temperature and Humidity Sensor for Raspberry Pi Pico W

🌑️ What You'll Learn:

  • πŸ”Œ Wire DHT22 to Pico W (3 wires + pull-up resistor)
  • πŸ“¦ Install micropython-dht library in Thonny
  • πŸ’» Read temperature and humidity in MicroPython
  • ⚠️ Get alerts for heat waves and high humidity

πŸ”Œ Wiring Diagram

DHT22 Sensor              Raspberry Pi Pico W
══════════════            ════════════════════
VCC (Pin 1)     ─────►    3.3V (Pin 36)
GND (Pin 4)     ─────►    GND (Pin 38)
DATA (Pin 2)    ─────►    GP0 (Pin 1)

⚠️ IMPORTANT: Add 10kΩ pull-up resistor between DATA and 3.3V!
    
    3.3V ──┬── 10kΞ© ──┬── DHT22 DATA
           β”‚          β”‚
           └──────────┴── Pico GP0
    
    DHT22 pins (front view, pins facing you):
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ (1) VCC     β”‚
    β”‚ (2) DATA    β”‚
    β”‚ (3) NC      β”‚
    β”‚ (4) GND     β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    

πŸ“¦ Install DHT Library in Thonny

  1. Open Thonny IDE
  2. Click Tools β†’ Manage Packages
  3. Search for "micropython-dht"
  4. Click Install

πŸ“– Complete MicroPython Code

import dht
from machine import Pin
import time

# Initialize DHT22 on GP0
sensor = dht.DHT22(Pin(0))

print("🌑️ DHT22 Weather Monitor Started")
print("=================================")

while True:
    try:
        # Measure sensor (takes 0.5-1 second)
        sensor.measure()
        
        # Read values
        temp = sensor.temperature()
        hum = sensor.humidity()
        
        # Display results
        print(f"\n🌑️ Temperature: {temp}°C | {temp * 9/5 + 32:.1f}°F")
        print(f"πŸ’§ Humidity: {hum}%")
        
        # Alerts
        if temp > 35:
            print("⚠️ HEAT WARNING! Increase ventilation/irrigation")
        elif temp < 10:
            print("⚠️ COLD! Protect sensitive crops from frost")
        
        if hum > 85:
            print("⚠️ HIGH HUMIDITY! Risk of fungal disease")
        elif hum < 30:
            print("⚠️ LOW HUMIDITY! Plants under stress")
        
    except Exception as e:
        print("❌ Sensor error:", e)
        print("   Check wiring and pull-up resistor")
    
    time.sleep(10)  # Read every 10 seconds
    
πŸ’‘ Troubleshooting:
  • "Sensor error" or no readings: Check pull-up resistor (10kΞ© between DATA and 3.3V)
  • Wrong values: DHT22 needs 2 seconds between readings (delay at least 2 seconds)
  • No library found: Install micropython-dht via Tools β†’ Manage Packages
  • Temperature stuck at -40Β°C: Bad connection on DATA pin
🎯 Quick Reference:
  • πŸ”Œ Pins: VCCβ†’3.3V, GNDβ†’GND, DATAβ†’GP0
  • πŸ”§ 10kΞ© pull-up resistor REQUIRED between DATA and 3.3V
  • πŸ“¦ Library: micropython-dht
  • ⚠️ Temp >35Β°C = heat stress, Humidity >85% = fungus risk
πŸ’‘ Key Takeaways:
  • Apply these concepts directly to your farm or project.
  • Take notes on important details for the quiz.
  • Use the button below to track your progress.