Zerobite
Posts: 4
Joined: Mon Feb 12, 2024 3:00 am

Raspberry pi 5 with DHT11

Sat Feb 24, 2024 3:18 pm

how to read DHT11 values using pythin in Raspberry pi 5

danjperron
Posts: 4743
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Raspberry pi 5 with DHT11

Sat Feb 24, 2024 6:35 pm

First install the dtoverlay driver in linux

Connect DHT11 VCC to 3.3V
Connect DHT11 GND to GND
Connect DHT11 DAT pin to an unused GPIO PIN.

On my example I choose GPIO21

Now install the DTOVERLAY DHT11

type sudo nano /boot/firmware/config.txt
Just after the [ALL] add this line

Code: Select all

[all]
dtoverlay=dht11,gpiopin=21
CTRL-x and yes to save

Reboot

Now you should see the DHT11 sensor appears "/sys/bus/iio/devices" folder

Code: Select all

daniel@Pi5:~ $ cd /sys/bus/iio/devices
daniel@Pi5:/sys/bus/iio/devices $ ls
iio:device0
daniel@Pi5:/sys/bus/iio/devices $ cd iio:device0
daniel@Pi5:/sys/bus/iio/devices/iio:device0 $ ls
in_humidityrelative_input  name     power      uevent
in_temp_input              of_node  subsystem  waiting_for_supplier
daniel@Pi5:/sys/bus/iio/devices/iio:device0 $ cat in_humidityrelative_input 
36000
daniel@Pi5:/sys/bus/iio/devices/iio:device0 $ cat in_temp_input 
20000
daniel@Pi5:/sys/bus/iio/devices/iio:device0 $ 
if iio:device0 exist then you sensor works!

And this is a small script to access the DHT11 via python

Code: Select all

#!/bin/python3
import time

device0 = "/sys/bus/iio/devices/iio:device0"


#function to read first line and return integer

def readFirstLine(filename):
    try:
        f = open(filename,"rt")
        value =  int(f.readline())
        f.close()
        return True, value
    except ValueError:
        f.close()
        return False,-1
    except OSError:
        return False,0


try:
    while True:
        Flag, Temperature = readFirstLine(device0+"/in_temp_input")
        print("Temperature:",end="")
        if Flag:
            print(Temperature // 1000,"\u2103",end="\t")
        else:
            print("N.A.",end="\t")

        Flag, Humidity = readFirstLine(device0+"/in_humidityrelative_input")
        print("Humidity:",end="")
        if Flag:
            print(Humidity // 1000,"%")
        else:
            print("N.A.")
        time.sleep(2.0)
except KeyboardInterrupt:
    pass
The result

Code: Select all

daniel@Pi5:~ $ python readDHT11.py
Temperature:27 ℃	Humidity:31 %
Temperature:26 ℃	Humidity:32 %
Temperature:26 ℃	Humidity:32 %
Temperature:26 ℃	Humidity:32 %
Temperature:26 ℃	Humidity:32 %
Temperature:26 ℃	Humidity:32 %
^Cdaniel@Pi5:~ $ 

Zerobite
Posts: 4
Joined: Mon Feb 12, 2024 3:00 am

Re: Raspberry pi 5 with DHT11

Sat Feb 24, 2024 11:59 pm

Thank you. it look likes that Raspberry pi 5 needs more coding than pi 4 to get most of sunfounder kits parts work. I have an issue with LCD and gpiozero doesn't support it. any idea how to make lcd work with Pi 5.

danjperron
Posts: 4743
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Raspberry pi 5 with DHT11

Sun Feb 25, 2024 1:03 am

Zillions of different LCD displays.

I can't give you an answer not knowing what is the type of display you have and what type of protocol it uses.

Zerobite
Posts: 4
Joined: Mon Feb 12, 2024 3:00 am

Re: Raspberry pi 5 with DHT11

Sun Feb 25, 2024 1:39 am

LCD1602

danjperron
Posts: 4743
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Raspberry pi 5 with DHT11

Sun Feb 25, 2024 3:02 am

I assume that you have the one without I2C!

Well this Adafruit demonstration should work!.
https://learn.adafruit.com/drive-a-16x2 ... ython-code

Just one word. before.
With the new python3.11 you should run your code in virtual environment!

Just create a folder for you project and set "venv" , Activate the "venv" and pip install what you need.

ex:

Code: Select all

 cd
 mkdir lcd_project
 cd lcd_project
 python -m venv lcd_venv
 source lcd_venv/bin/activate
 pip3 install gpiod
 pip3 install adafruit-blinka
 pip3 install adafruit-circuitpython-charlcd
 
Then upload the demo python project and run the script. Don't forget to set all the wires.

Return to “Python”