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

39,912

Solution 1

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)

Solution 2

Maybe more information will help to solve your problem. I have the same sensor like you.

I followed this tutorial : https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated

git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo apt-get update
sudo apt-get install build-essential python-dev
sudo python setup.py install

And this is my testing python script :

#!/usr/bin/python
import sys
import Adafruit_DHT
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
if humidity is not None and temperature is not None:
   print 'Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity)
else:
   print 'Failed to get reading. Try again!'

Save it as for example dht_test.py , Chmod : sudo chmod a+x dht_test.py and run as sudo : sudo ./dht_test.py Maybe this helps you.

Share:
39,912
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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

  • sevenOfNine
    sevenOfNine over 8 years
    I could get temperature and humidity according to your answer. But it seems that the fractional parts of both data are always 0. For example, Temp=25.0*C Humidity=35.0%.
  • sevenOfNine
    sevenOfNine over 8 years
    I found that DHT11 does not measure the fractional part. So, the measurement is successful.