Reading Temperature and Humidity with a DHT22 and Raspberry Pi
Reading Temperature and Humidity with a DHT22 and Raspberry Pi The DHT22 is a cheap, accurate temperature and humidity sensor. Here's wiring and code for the Raspberry Pi. DHT11 vs DHT22 Use DHT22 — the accuracy difference is worth it. Wiring Add a 10k ohm pull-up resistor between DATA and VCC. Installation bash pip install adafruit-circuitpython-dht sudo apt install libgpiod2 Python Code python import adafruitdht import board, time dht = adafruitdht.DHT22(board.D4) while True: try: temp = dht.temperature humidity = dht.humidity print(f"Temp: {temp:.1f}C Humidity: {humidity:.1f}%") except RuntimeError as e: print(f"Read error: {e}") # occasional errors are normal time.sleep(2) # DHT22 needs 2s between reads Logging to CSV python import csv from datetime import datetime with open('readings.csv', 'a', newline='') as f: writer = csv.writer(f) writer.writerow([datetime.now().isoformat(), temp, humidity])
Related Guides
- Raspberry Pi GPIO: Complete Beginner Guide with Python Examples
- I2C Wiring and Protocol Guide for Arduino, ESP32, and Raspberry Pi
- How to Install Klipper on Any 3D Printer: Complete Setup Guide
- How to Use Sensors with Arduino and ESP32: Temperature, Distance, Load, Current, and Hall Effect
- How to Set Up OpenCV Machine Vision on a Raspberry Pi
- Raspberry Pi: Complete Headless Setup Guide (No Monitor Needed)
- Raspberry Pi: Headless OS Setup
- How to Set Up a Raspberry Pi Headless with SSH and WiFi