Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DHT sensor not found, check wiring #49

Closed
@Talaxy009

Description

@Talaxy009

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions