β Back to Course
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
- Open Thonny IDE
- Click Tools β Manage Packages
- Search for "micropython-dht"
- 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.
×