Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 760
Bharadvaj Jayachandra Avatar asked Mar 07 '15 09:03

Bharadvaj Jayachandra


People also ask

Why is my DHT11 sensor not working?

Try to plug it to a USB hub powered by an external power source. It might also help replacing the USB cable with a better or shorter one. Having a USB port that supplies enough power or using a good USB cable often fixes this problem.

Which temperature sensor is used DHT11?

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air and spits out a digital signal on the data pin (no analog input pins needed).


2 Answers

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)
like image 124
Zoltán Szarvas Avatar answered Oct 01 '22 17:10

Zoltán Szarvas


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.

like image 33
Tomas Pytel Avatar answered Oct 01 '22 17:10

Tomas Pytel