30 de octubre de 2021

How to measure Temperature and Humidity from two DHT22 sensors conected to Raspberry-Pi and Python3

For this project I used 2 DHT22 sensors modules that include the resistor.

Sensor has 3 wires to connect to GPio: Data, GND and VCC (+5V or 3.3V).

Can be use 5V or 3.3V to provide power to sensor.

With 3.3V sometimes DHT22 sensor did no respond or lost the connection (?), in my case using 5V solve the problem.

Script are coded in Python3, let's install previously the libraries. 

 

   1) The Python script:

#!/usr/bin/python3
####################################################
# (C)by MisNotasLinux 2021
# DHT22_01: data wire to gpio 23

# DHT22_02: data wire to gpio 24
# Conect VCC of
DHTs to 5V or 3.3V rpi gpio pin.
# Conect GND of DHTs to GND rpi gpio pin.
####################################################

import Adafruit_DHT
import time
import datetime

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_01 = 23   # data rpi gpio pin for dht sensor 01
DHT_02 = 24   #
data rpi gpio pin for dht sensor 02

t = 10    # time for repeat measurement

while True:
    print(datetime.datetime.now())
    print("----------------------------------------------")

    h01, t01 = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_01)

    if h01 is not None and t01 is not None:
        print("Temp_01={0:0.1f}*C  Humidity_01={1:0.1f}%".format(t01, h01))
    else:
        print("Failed to retrieve data from humidity sensor 01")

    h02, t02 = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_02)

    if h02 is not None and t02 is not None:
        print("Temp_02={0:0.1f}*C  Humidity_02={1:0.1f}%".format(t02, h02))
    else:
        print("Failed to retrieve data from humidity sensor 02")

    print("Sleeping " + str(t) + " sec ...")
    print("----------------------------------------------")

 

   2) References:

https://github.com/adafruit/Adafruit_Python_DHT

No hay comentarios:

Publicar un comentario