himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

dht22 3pinout code

Sat Aug 13, 2016 1:29 pm

T am not getting any code for dht22 temp and humidity measurment...
I have tried this code but i m getting ERR_RANGE as output....Can anyone help

Code: Select all

import RPi.GPIO as GPIO
import time

def bin2dec(string_num):
    return str(int(string_num, 2))

data = []

GPIO.setmode(GPIO.BCM)

GPIO.setup(4,GPIO.OUT)
GPIO.output(4,GPIO.HIGH)
time.sleep(0.025)
GPIO.output(4,GPIO.LOW)
time.sleep(0.02)

GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)

for i in range(0,500):
    data.append(GPIO.input(4))

bit_count = 0
tmp = 0
count = 0
HumidityBit = ""
TemperatureBit = ""
crc = ""

try:
   while data[count] == 1:
      tmp = 1
      count = count + 1

   for i in range(0, 32):
      bit_count = 0

      while data[count] == 0:
         tmp = 1
         count = count + 1

      while data[count] == 1:
         bit_count = bit_count + 1
         count = count + 1

      if bit_count > 3:
         if i>=0 and i<8:
            HumidityBit = HumidityBit + "1"
         if i>=16 and i<24:
            TemperatureBit = TemperatureBit + "1"
      else:
         if i>=0 and i<8:
            HumidityBit = HumidityBit + "0"
         if i>=16 and i<24:
            TemperatureBit = TemperatureBit + "0"

except:
   print "ERR_RANGE"
   exit(0)

try:
   for i in range(0, 8):
      bit_count = 0

      while data[count] == 0:
         tmp = 1
         count = count + 1

      while data[count] == 1:
         bit_count = bit_count + 1
         count = count + 1

      if bit_count > 3:
         crc = crc + "1"
      else:
         crc = crc + "0"
except:
   print "ERR_RANGE"
   exit(0)

Humidity = bin2dec(HumidityBit)
Temperature = bin2dec(TemperatureBit)

if int(Humidity) + int(Temperature) - int(bin2dec(crc)) == 0:
   print Humidity
   print Temperature
else:
   pr
int "ERR_CRC"

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: dht22 3pinout code

Sat Aug 13, 2016 1:37 pm

The loop using the "for appends"will depends of the speed of the user process.
You will never be able to get the correct timing to read perfectly the DHT22 signal.

There is so many way to read the DHT22 correctly on the forum.

I do have code to read it from the SPI. You could use pigpio to read it . Adafruit has a library for it also.
Or you could use the kernel driver in the overlay .

Please search on the forum.

This is a post about reading the DHT11/DHT22 I made a year ago on the forum.
viewtopic.php?p=672406#p672406

himani18
Posts: 44
Joined: Fri Feb 19, 2016 4:52 pm

Re: dht22 3pinout code

Sat Aug 13, 2016 1:46 pm

I used the library of adafruit but i want to send sms or email of the current temperatue

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: dht22 3pinout code

Sun Aug 14, 2016 2:45 am

for i in range(0,500):
data.append(GPIO.input(4))
The DHT22 send 0 and 1 using different pulse width. This is time difference between edges.
Your loop use unspecific time to read the gpio 500 time. You don't know the timing and you don't care about the difference of pulse width between 0 and 1.

This link explain how the DHT22 works.
viewtopic.php?f=37&t=91326&hilit=dht22+spi

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: dht22 3pinout code

Sun Aug 14, 2016 2:45 am

for i in range(0,500):
data.append(GPIO.input(4))
The DHT22 send 0 and 1 using different pulse width. This is time difference between edges.
Your loop use unspecific time to read the gpio 500 time. You don't know the timing and you don't care about the difference of pulse width between 0 and 1.

This link explain how the DHT22 works.
viewtopic.php?f=37&t=91326&hilit=dht22+spi

Return to “General discussion”