Introduction: DIY Raspberry Pi Temperature and Humidity Sensor

About: This is TechSparks, we are an organization of electronics enthusiasts from China, you can find a lot of content about electronics on our website, including design projects, manufacturer lists, etc.

My initial Raspberry Pi project revolves around utilizing the DHT22 temperature and humidity sensor. This sensor, also referred to as DHT22 or AM2302, is an integrated digital device adept at gauging temperature and humidity levels in the surrounding environment. It operates by converting these measurements into digital signals, enabling precise and convenient retrieval of environmental data.

The functionality of the DHT22 sensor hinges on digital signal transmission. Within its compact structure, it incorporates both temperature and humidity sensing elements. Accompanying these elements is a microcontroller responsible for processing the gathered data and transforming it into digital signals. These signals can be conveniently accessed by connecting the sensor to microcontrollers like Arduino or Raspberry Pi.

In essence, the DHT22 sensor simplifies the task of capturing and interpreting temperature and humidity data. Its ability to convert such measurements into digital signals streamlines the process and makes it accessible for integration into various projects, including my introductory Raspberry Pi endeavor.

Supplies

  • Raspberry Pi (any model will do)
  • Breadboard
  • Male and female jumper wires (3)
  • DHT22 temperature and humidity sensor
  • 10KΩ resistor

Step 1: Wiring

As depicted in the image below, the wiring for this project maintains simplicity, necessitating minimal additional components beyond the core sensor:

  • Connect the VCC (pin 1) of the DHT22 sensor to the 5V (pin 04) port on the Raspberry Pi.
  • Establish a connection from the Data pin (pin 2) of the sensor to the GPIO04 (pin 07) on the Raspberry Pi.
  • Complete the setup by linking the GND (pin 4) of the sensor to the GND (pin 06) of the Raspberry Pi.
  • To ensure proper functioning, integrate a 10KΩ resistor between the VCC (pin 1) and the data pin (pin 2) on the DHT22 sensor.

This uncomplicated wiring configuration simplifies the connection process, making it accessible even for those new to Raspberry Pi projects.

Step 2: Code

Importing Libraries:

import Adafruit_DHT

import time

We import the Adafruit_DHT library to interact with the DHT22 sensor and the time library for performing wait operations.

Setting Sensor Type and Pin:

sensor = Adafruit_DHT.DHT22

pin = 4 # GPIO04

We define the sensor type as DHT22 and specify the pin to which the sensor is connected, which in this case is GPIO04.

Main Loop:

while True:

This initiates an infinite loop where the code will continuously run.

Trying to Read Sensor Data:

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

We attempt to read humidity and temperature data from the sensor using the read_retry function from the Adafruit_DHT library.

Checking Data Validity:

if humidity is not None and temperature is not None:

  print('Temperature: {0:.1f}°C'.format(temperature))

  print('Humidity: {0:.1f}%'.format(humidity))

else:

  print('Failed to retrieve data. Check sensor connection.')

We check whether the read data is valid. If the data is valid, we use the print function to display the temperature and humidity on the terminal. If data retrieval fails, we display an error message.

Waiting Before Reading Data Again:

time.sleep(2)

We use the time.sleep function to make the code wait for 2 seconds before reading sensor data again.

Step 3: Summarize

Overall, this project is simpler, whether it is code or wiring, through which you can monitor the temperature and humidity of the environment in real time, understand the changes in the environment and build more complex projects on this basis. This project comes from TechSparks article Raspberry Pi DHT22 temperature and humidity sensor project, if you are interested in it, please check it out.