Adafruit_DHT11 with RPi 4b

Hi there,

I’m quite new to RPi and was working on the tutorials of Paul McWorther.

Now I came across the DHT11 temperature sensor where I was first notified about the ‘unkown platform error’ which I understand is linked to me using the RPi 4b (instead of older versions). With many searches I managed to work around this error but now I get the message ‘int’ object not callable. If you can help me out would be appreciated.

Here is the code I’m using:

import RPi.GPIO as GPIO
import Adafruit_DHT
import digitalio
import board
import time

Initial the dht device, with data pin connected to GPIO4

sensor = Adafruit_DHT.DHT11(board.D4)

while True:
try:

Print the values to the serial port

temperature_c = sensor.temperature
humidity = sensor.humidity
print("Temp: {:.1f} C Humidity: {}% ".format(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:
    sensor.exit()
    raise error

time.sleep(2.0)

Possibly because of this line….

sensor = Adafruit_DHT.DHT11(board.D4)

Adafruit_DHT.DHT11 is actually an int constant, not a class.

Just a best guess

The error “int object not callable” happens because two different DHT libraries are being mixed together in the code.

In your current script, this line:

sensor = Adafruit_DHT.DHT11(board.D4)

is the issue.

Adafruit_DHT.DHT11 is actually just an integer constant, not a function. So Python is trying to “call” an integer, which causes the error.

If you are following Paul McWhorter’s tutorial, please modify your code like this:

import Adafruit_DHT

import time

sensor = Adafruit_DHT.DHT11

pin = 4 # GPIO4

while True:

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

if humidity is not None and temperature is not None:

    print(f"Temp: {temperature:.1f} C  Humidity: {humidity}%")

else:

    print("Sensor failure. Please check wiring.")

time.sleep(2)

Hello, and many thanks for your response to my question.
However, I already tried this, and tried now once more, but with this code the following error is raised:

RuntimeError(‘Unknown platform’)

As I understand this has to do with the fact that I run this code on a RPi 4b which is apparently not defined in “platform_detect.py”. If you might have an idea how to solve this issue, would be appreciated.

Hi Spf650,

Thanks for your suggestion. Do you perhaps also know how to define it as a class?
Any suggestion is welcome.

Im not an expert here. All i can give you is generic debug information rather than anything specific to adafruit

For the unknown platform error, You could try basic stuff like

sudo apt update
sudo apt install python3-blinka
sudo reboot

But, You would need to give a lot more information for me to have a chance e.g exact os that you are using, using a venv? Inside a docker?

Probably better to do a general google, as you already said you’ve solved this previously anyway.

The error you’re seeing — RuntimeError: ‘Unknown platform’ — is not caused by your wiring or Python syntax. It happens because the Adafruit_DHT library is an older driver and it may not recognize newer Raspberry Pi OS, such as Bookwarm, Trixie, so it reports “Unknown platform”.

The best solution is to use a newer, actively maintained DHT library.

  1. Install dependencies

sudo apt update

sudo apt install -y python3-pip libgpiod2

  1. Install the modern DHT library

pip3 install adafruit-circuitpython-dht --break-system-packages

  1. Test script

import time

import board

import adafruit_dht

# GPIO4

dht = adafruit_dht.DHT11(board.D4)

while True:

try:

    temperature = dht.temperature

    humidity = dht.humidity

    print(f"Temp: {temperature}°C  Humidity: {humidity}%")

except RuntimeError as e:

    \# DHT sensors can occasionally fail a read — retrying is normal

    print("Read error:", e)

time.sleep(2)