DHT22 on the Raspberry Pi

DHT22 is a digital temperature and humidity sensor that can be used with the Raspberry Pi. It’s a low-cost, easy-to-use sensor that provides accurate readings of temperature and humidity levels. In this article, we will discuss what DHT22 is and how to utilize it on the Raspberry Pi.

What is DHT22?

DHT22 is a sensor that measures temperature and humidity. It’s a digital sensor, which means it outputs data in digital format. The sensor has a built-in capacitive humidity sensor and a thermistor that measures the temperature. It’s a low-cost sensor that is widely used in DIY projects and commercial applications.

DHT22 Pinout

The DHT22 sensor has four pins: VCC, GND, Data, and NC (Not Connected). Here’s a breakdown of each pin:

  • VCC: This is the power pin. Connect it to 3.3V or 5V on the Raspberry Pi.
  • GND: This is the ground pin. Connect it to a ground pin on the Raspberry Pi.
  • Data: This is the data pin. Connect it to a GPIO pin on the Raspberry Pi.
  • NC: This pin is not connected and should be left unconnected.

How to Utilize DHT22 on the Raspberry Pi

To utilize the DHT22 sensor on the Raspberry Pi, you will need to install the required software and write a Python script to read the data from the sensor.

Step 1: Install Required Software

Before you can use the DHT22 sensor, you need to install the Adafruit Python DHT library. Open the terminal on your Raspberry Pi and enter the following command:

sudo pip3 install Adafruit_DHT

Step 2: Connect DHT22 Sensor to Raspberry Pi

Connect the DHT22 sensor to your Raspberry Pi according to the pinout shown above.

Step 3: Write Python Script

To read data from the DHT22 sensor, you need to write a Python script. Here’s an example script that reads temperature and humidity data from the sensor:

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22
pin = 4

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:
    print('Temperature={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
    print('Failed to read data from DHT22 sensor')

Save this script as `dht22.py`. This script imports the Adafruit_DHT library, sets the sensor type to DHT22 and the GPIO pin to 4. It then reads the data from the sensor and prints the temperature and humidity readings.

Step 4: Run Python Script

To run the Python script, open the terminal on your Raspberry Pi and navigate to the directory where you saved the script. Enter the following command to run the script:

python3 dht22.py

This will run the script and display the temperature and humidity readings in the terminal.


The DHT22 sensor is a low-cost, easy-to-use sensor that provides accurate temperature and humidity readings. By following the steps outlined in this article, you can easily utilize the DHT22 sensor on your Raspberry Pi. With this sensor, you can monitor temperature and humidity levels in your home or office and use the data for various applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top