Adafruit_CircuitPython_DHT: DHT sensor not found, check wiring

I’m using DHT11 on Rpi 4B and I turn to using Adafruit_CircuitPython_DHT today from Adafruit_Python_DHT. I had changed my code to fit the Adafruit_CircuitPython_DHT, but when I run it I meet this error:

Traceback (most recent call last):
  File "tmpv0.2.py", line 44, in <module>
    main()
  File "tmpv0.2.py", line 23, in main
    temperature = dht_device.temperature
  File "/usr/local/lib/python3.8/dist-packages/adafruit_dht.py", line 253, in temperature
    self.measure()
  File "/usr/local/lib/python3.8/dist-packages/adafruit_dht.py", line 205, in measure
    raise RuntimeError("DHT sensor not found, check wiring")
RuntimeError: DHT sensor not found, check wiring

I am sure the connection is fine, because after this error I run the former code and it work perfectly.

Here is the code that has been change to fit Adafruit_CircuitPython_DHT

import datetime
import time

import adafruit_dht
from board import D4
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import sh1106
from PIL import ImageFont


# OLED settings
serial = i2c(port=1, address=0x3C)
device = sh1106(serial)
font = ImageFont.truetype('simyou.ttf', 15)

# DHT11 settings
dht_device = adafruit_dht.DHT11(D4)


def main():
    while True:
        temperature = dht_device.temperature
        humidity = dht_device.humidity
        if humidity is not None and temperature is not None:
            Temperature = "温度: {0:0.1f}°C".format(temperature)
            Humidity = "湿度: {0:0.1f}%".format(humidity)
            print(Temperature, Humidity)
            for i in range(0, 10):
                now = datetime.datetime.now()
                today_time = now.strftime("%H:%M:%S")
                with canvas(device) as draw:
                    draw.text((30, 4), today_time, font=font, fill="white")
                    draw.text((10, 23), Temperature, font=font, fill="white")
                    draw.text((10, 42), Humidity, font=font, fill="white")
                time.sleep(0.2)

        else:
            print('失败')


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        device.cleanup()
        pass

And here is the former code

import datetime
import time

import Adafruit_DHT
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import sh1106
from PIL import ImageFont

# OLED settings
serial = i2c(port=1, address=0x3C)
device = sh1106(serial)
font = ImageFont.truetype('simyou.ttf', 15)

# DHT11 settings
sensor = Adafruit_DHT.DHT11
DHT11pin = 4


def main():
    while True:
        humidity, temperature = Adafruit_DHT.read_retry(sensor, DHT11pin)
        if humidity is not None and temperature is not None:
            Temperature = "温度: {0:0.1f}°C".format(temperature)
            Humidity = "湿度: {0:0.1f}%".format(humidity)
            print(Temperature, Humidity)
            for i in range(0, 10):
                now = datetime.datetime.now()
                today_time = now.strftime("%H:%M:%S")
                with canvas(device) as draw:
                    draw.text((30, 4), today_time, font=font, fill="white")
                    draw.text((10, 23), Temperature, font=font, fill="white")
                    draw.text((10, 42), Humidity, font=font, fill="white")
                time.sleep(0.2)

        else:
            print('失败')


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        device.cleanup()
        pass

I am new to python and Rpi, and thanks for any help!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 31 (1 by maintainers)

Most upvoted comments

@Tob1as I review the PR, and I was wrong. Sorry about that. The intended change in 3.5.6 was to addresses mentioned the trigger time for DHT11. However in doing so, I used the ‘//’ division instead of the / division, given a 0 every time. I have just submitted a new PR #64 to correct this. Thank you for pointing that out.

Hi,

i’m facing the same problem.

This does not run …

$ cat dev/simpletest.py 
#!/usr/bin/python3

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.D18, 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

    time.sleep(10.0)

which end up in “DHT sensor not found, check wiring”

$ sudo python3 simpletest.py 
DHT sensor not found, check wiring
DHT sensor not found, check wiring
DHT sensor not found, check wiring
received SIGINT

Traceback (most recent call last):
  File "simpletest.py", line 18, in <module>
^C    temperature_c = dhtDevice.temperature
  File "/usr/local/lib/python3.8/dist-packages/adafruit_dht.py", line 253, in temperature
    self.measure()
  File "/usr/local/lib/python3.8/dist-packages/adafruit_dht.py", line 205, in measure
    raise RuntimeError("DHT sensor not found, check wiring")
RuntimeError: DHT sensor not found, check wiring

while

$ cat PiTemp.py 
__version__= "0.1"
import sys
if sys.version_info < (3, 0):
    sys.stdout.write("Sorry, requires Python 3.x, not Python 2.x\n")
    sys.exit(1)

import time
import Adafruit_DHT

DELAY_SECONDS=15          # 15 Sec

DHT_SENSOR=Adafruit_DHT.DHT22
DHT_PIN=18

if __name__ == "__main__":
    print("Um das Programm anzubrechen, drücke STRG+C ...")

    while True:
        try:
            # Print the values to the serial port
            humidity, temp = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
            print("Temp: {0:0.1f} °C    Humidity: {1:0.1f} % ".format(temp, humidity))
        except RuntimeError as error:
            print(error.args[0])
            time.sleep(5.0) # In case of error retry after 5.0 seconds
            continue
        time.sleep(DELAY_SECONDS)

works like a charm.

$ sudo python3 PiTemp.py 
Um das Programm anzubrechen, drücke STRG+C ...
Temp: 20.8 °C    Humidity: 56.9 % 
Temp: 20.8 °C    Humidity: 56.9 % 

Any ideas?

@Tob1as Thanks, Changes are merged, they will be released soon!, and thanks fr the feeback and testing on the changes (y)

FWIW I’m using the below code that I set up last week (13 Feb) and it works perfectly with a DHT22 connected to pin 4. Swap out the 22 for an 11 and change the dhtDevice setting to DHT11 and it gives me DHT sensor not found (same wiring - literally just changed out the 22 for an 11). I’ve tried it with 4 different DHT11’s and 2 DHT22’s - the 22’s both work fine (about 50/50 valid reading + error messages various) but the 11’s don’t work at all.

import time
import board
import adafruit_dht

#Initialise the dht device with the data pin connected to
# dhtDevice = adafruit_dht.dht22(board.D4)

# you can pass DHT 22 use_pulseio=False if you don't want to use pulseio
# this may be necessary on the Pi but will not work in
# circuit python

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:
        # Expect DHTs to produce regular errors
        print (error.args[0])
        time.sleep(2.0)
        continue
    except Exception as error:
        dhtDevice.exit()
        raise error

    time.sleep(2.0)

from https://tutorials-raspberrypi.com/raspberry-pi-measure-humidity-temperature-dht11-dht22/

@bsolino Agree with this kind of sensor everything could go wrong, and is very difficult to troubleshoot. These Sensor are very time dependent, and this is something that the RP is not very good at. At least in my experience. But the RP3 is indeed strange both have worked in my case, inconsistent but worked.

@bsolino Hello, I did a little investigation into this, you can follow my results here https://github.com/jposada202020/DHT_Investigation But I have never been able to read a DHT sensor in a Raspberry PI Zero. I tried both the DHT11 and the DHT22. I saw that you followed the recommendation to not to use Pin 4. In my tests I used pin D17. However, I can read in both sensor in the RP3/4 do you happen to have one in hand?

I’ve been struggling with this issue too. I know the wiring is right because it works sometimes. However, I’m trying now a DHT22 and it is extremely temperamental, sometimes taking several minutes before correct readings. I’ve also encountered a few instances of “A full buffer was not returned. Try again.”. I have modified the code to actually show the number of pulses received, and oddly enough it usually doesn’t receive any pulses.

As an example, I have this code that checks the sensor every two seconds. I’m using here a DHT22 sensor on a RaspberryPi Zero W, pin 4, and using the pulseio method. I’ve also tried pin 18 (as recommended above), with similar results.

$ ./run_22.sh
2021-02-16 23:41:16.472722 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:18.746235 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:21.006975 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:23.266578 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:25.526662 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:23.266578 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:25.526662 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:27.787561 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:30.056836 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:32.317642 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:34.586726 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:36.846722      Temp: 11.5 C    Humidity: 59.9 %
2021-02-16 23:41:39.551465 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:41.826696 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:44.095804 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:46.356680 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:48.627296 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:50.887131 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:53.146693 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:55.406092 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:57.667552 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:41:59.936223 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:02.196585 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:04.456705 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:06.719312 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:08.986958 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:11.246717 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:13.506496      Temp: 11.5 C    Humidity: 60.4 %
2021-02-16 23:42:16.195411 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:18.466762 A full buffer was not returned. Try again. 79 pulses.
2021-02-16 23:42:20.764793 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:23.036675 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:25.303123 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:27.566617 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:29.827433 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:32.086793 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:34.346725 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:36.606720 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:38.937387 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:41.194744 A full buffer was not returned. Try again. 17 pulses.
2021-02-16 23:42:43.465054 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:45.737242 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:47.996850 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:50.253569 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:52.513441 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:54.773724 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:52.513441 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:54.773724 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:57.033494 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:42:59.293654 A full buffer was not returned. Try again. 31 pulses.
2021-02-16 23:43:01.568428 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:03.843388 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:06.103259 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:08.364260 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:10.623567 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:12.896756 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:15.153514 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:17.413947 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:19.673497 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:21.933526 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:24.203646 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:26.463453 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:28.732867 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:30.996279 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:33.253776 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:35.513584 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:37.773997 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:40.033550 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:42.293416 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:44.553456 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:46.813505 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:49.073909 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:51.343570 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:53.603567 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:55.864107 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:43:58.118187 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:44:00.376169 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:44:02.633598 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:44:04.943199 DHT sensor not found, check wiring. 0 pulses.
2021-02-16 23:44:07.197233  Temp: 11.5 C    Humidity: 60.6 %

I used to have a DHT11 sensor, and that one also had this issue often, but not as frequently. I could usually get 5 correct readings in 1 or 2 minutes. Unfortunately, it seems I’ve burned it so I can’t do tests with it anymore.

For reference, my pip list:

$ pip list
Package                    Version             Location
-------------------------- ------------------- ------------------------------------------------------------------                                               
Adafruit-Blinka            6.2.2
adafruit-circuitpython-dht 3.5.6.dev3+g8695a11 /home/pi/projects/libraries/raspberrypi/Adafruit_CircuitPython_DHT                                               
Adafruit-PlatformDetect    3.1.1
Adafruit-PureIO            1.1.8
et-xmlfile                 1.0.1
jdcal                      1.4.1
openpyxl                   3.0.6
pip                        21.0.1
pkg-resources              0.0.0
pyftdi                     0.52.9
pyserial                   3.5
pyusb                      1.1.1
rpi-ws281x                 4.2.5
RPi.GPIO                   0.7.0
setuptools                 40.8.0
sysv-ipc                   1.1.0

Same issue except my GPIO pin is 4. At this point I don’t think the DHT11 even works with an RPi4…any solutions would be nice

Try this one -> Adafruit_Python_DHT? DHT11 work well with my RPi4 when I use this library.

I think it wrong to solve a problem by using a deprecated libraray which links to this (CircuitPython) library. However it’s a dirty but working hack.