DHT22

Basic tutorial of how to setup an DHT22 Temperature and Humidity sensor with the Raspberry Pi.

DHT22 Specifications: https://cdn-shop.adafruit.com/datasheets/Digital+humidity+and+temperature+sensor+AM2302.pdf

PARTS:

CanaKit Raspberry Pi 4 4GB Starter Kit – https://amzn.to/2Jrlbfj

DHT22 Breakout Board – https://amzn.to/2LraGtj

 

SCHEMATIC:

RPI  |  DHT22

3.3V <–> +

GPIO4 <–> OUT

GND <–> –

SETUP:

1. Install DHT22 Library

sudo pip3 install adafruit-circuitpython-dht

 

CODE:

Adafruit_CircuitPython_DHT github: https://github.com/adafruit/Adafruit_CircuitPython_DHT

import time
import board
import adafruit_dht

# Initial the dht device, with data pin connected to:
# dhtDevice = adafruit_dht.DHT22(board.D18)

# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)

while True:
    try:
        # Print the values to the serial port
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
        print(
            "Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(
                temperature_f, temperature_c, humidity
            )
        )

    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
        print(error.args[0])
        time.sleep(2.0)
        continue
    except Exception as error:
        dhtDevice.exit()
        raise error
    except KeyboardInterrupt:
        dhtDevice.exit()
        print('exiting script')
    time.sleep(2.0)