Python GPIO code for DHT 11 temperature sensor fails in PI 2

Bharadvaj Jayachandra picture Bharadvaj Jayachandra · Mar 7, 2015 · Viewed 39.3k times · Source

I am facing issues running DHT 11 temperature sensor in PI 2 with Python2.7 GPIO 0.5.11. I am referring to http://www.uugear.com/portfolio/dht11-humidity-temperature-sensor-module/ sample code.

Same code works fine on PI 1 B+. In PI 2 i get "ERR_RANGE" as Error. I tried debugging the issue seems like data read @ GPIO pin 4 needs to be increased.

After increasing the data read value to 2000, the value for temperature and humidity returned is 255 all the time. Has anyone faced the issue do help me on how to solve.

Thanks, Bharadvaj

Answer

Zoltán Szarvas picture Zoltán Szarvas · Nov 22, 2015

You can also check the following small library. It depends only on GPIO module:

https://github.com/szazo/DHT11_Python

Example:

import RPi.GPIO as GPIO
import dht11

# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

# read data using pin 14
instance = dht11.DHT11(pin = 14)
result = instance.read()

if result.is_valid():
    print("Temperature: %d C" % result.temperature)
    print("Humidity: %d %%" % result.humidity)
else:
    print("Error: %d" % result.error_code)