PDA

View Full Version : DHT-11 Humidity/temperature sensor and PIC16F628



RuudNL
- 25th February 2012, 18:54
I started to experiment with the cheap DHT-11 humidity/temperature sensor.
Unfortunately, there are not very much code examples available in PicBasic.
I have slightly adapted this code that I found on the Internet.


INCLUDE "modedefs.bas"
DEFINE OSC 20 ' declare xtal to give exact delays
ip var Byte ' hi pulse from sensor
temp var Byte
hum var Byte
hum1 var Byte
chksm var Byte
test var Byte
i var Byte
TRISB.0 = 0 ' PORTB.0 = output, OneWire sensor
TRISB.1 = 0 ' PORTB.1 = output, serout
Pause 1000 ' give sensor time to settle
mainloop:
chksm = 0
' TRISB.0 = 0 ' portb o/p
PORTB.0 = 1 ' make high
pause 50 ' wait for a while
PORTB.0 = 0 : pause 18 ' send 18ms low
PORTB.0 = 1 : pauseus 30 ' send 30us hi
PulsIn PORTB.0, 1, ip ' wait for hi
If ip < 130 Then GoTo subloop ' if < 80us loop
ip = 0
For i = 7 To 0 Step -1 ' 8 bits (1..8)
PulsIn PORTB.0, 1, ip ' receive pulses from sensor
If ip > 70 Then
hum = hum | 1 << i ' SetBit hum, i
Else
hum = hum xor (1 << i) ' ClearBit hum, i
EndIf
Next i
For i = 7 To 0 Step -1 ' 8 bits (9..16)
PulsIn PORTB.0,1, ip ' receive pulses from sensor
If ip > 70 Then
hum1 = hum1 | 1 << i ' SetBit hum1, i
Else
hum1 = hum1 XOR (1 << i) ' ClearBit hum1,i
EndIf
Next i
For i = 7 To 0 Step -1 ' 8 bits (17..24)
PulsIn PORTB.0, 1, ip ' receive pulses from sensor
If ip > 70 Then
temp = temp | 1 << i ' SetBit temp, i
Else
temp = temp xor (1 << i) ' ClearBit temp,i
EndIf
Next i
For i = 7 To 0 Step -1 ' 8 bits (25..32)
PulsIn PORTB.0, 1, ip ' receive pulses from sensor
If ip > 70 Then
temp1 = temp1 | 1 << i ' SetBit temp1, i
Else
temp1 = temp1 xor (1 << i) ' ClearBit temp1,i
EndIf
Next i
For i = 7 To 0 Step -1 ' 8 bits (33..40)
PulsIn PORTB.0, 1, ip
If ip > 70 Then
chksm = chksm | 1 << i ' SetBit chksm,i
Else
chksm = chksm xor (1 << i) ' ClearBit chksm,i
EndIf
Next i
SerOut PORTB.1, 4, ["Check sum = ", #chksm, 13]
serout PORTB.1, 4, ["Humidity = ", #hum1, 13]
serout PORTB.1, 4, ["Temperature = ", #temp, "C.", 13]
test = (temp1 + hum1 + hum + temp)
If chksm <> test Then serout PORTB.1, 4, ["Checksum error", 13] 'HRSOut "error",13
pause 3000 ' give sensor time to settle
GoTo mainloop
subloop:
serout PORTB.1, 4, ["Sensor not ready", 13]
GoTo mainloop


I have some doubs:

The line: If ip < 130 Then GoTo subloop ' if < 80us loop has two values. Which one could be correct? 130 or 80 uS?

Also I changed SetBit value, bit to: value = value | 1 << bit and
ClearBit value, bit to: value = value xor (1 << bit)

(Since PicBasic has afaik not got SetBit and ClearBit.)

Where do I go wrong? Is there an experienced PicBasic user who can give me a hint?

Many thanks in advance!

Normnet
- 25th February 2012, 20:52
This may not help but wouldn't it be more readable to use something like:

For i = 7 To 0 Step -1 ' 8 bits (1..8)
PulsIn PORTB.0, 1, ip ' receive pulses from sensor
If ip > 70 Then
hum[i] = 1 ' SetBit hum, i
Else
hum[i] = 0 ' ClearBit hum, i
Next

Norm

RuudNL
- 26th February 2012, 09:21
If ip > 70 Then
hum[i] = 1 ' SetBit hum, i
Else
hum[i] = 0 ' ClearBit hum, i

Never thought about this!
Indeed, this is much easier to read!

RuudNL
- 3rd April 2012, 13:08
Many experiments later it is still not working...
The problem is that the DHT-11 is not recognised and the code jumps to the label "subloop".
Nobody got an idea or experience with the DHT-11 sensor?

(I have programmed the PIC16F628 with this hexfile https://github.com/candrian/Thermometer-V2.0/tree/master/firmware/ThermometerV2 and then it is working, so the DHT-11 isn't the problem. It is even working with a 4 MHz resonator. But I would like to add some other functions in PicBasic.)

brunom
- 13th October 2013, 05:13
Here is my code and it works.

Good luck!



dht var byte[32]
humidite var byte
haut var byte
bas var byte
temp var byte
x var byte
dht11 var portb.0


main:
TRISB.0 = 0 ' portb.0 = output
dht11 = 1 ' sortie à 1
pause 2000 ' wait 2 sec
dht11 = 0 : pause 25 ' send 20ms low
dht11 = 1 : pauseus 40 ' send 40us hi

PulsIn PORTB.0, 1, haut 'reponse du dht-11
if haut < 15 then goto main

for x = 31 to 0 step-1 'capture des 8 bits entier humidite
PulsIn PORTB.0, 1, dht[x] ' 1
next x

For x = 31 to 0 step-1 'conversion en 1 ou 0: 14 ou 5
if dht[x] > 9 then
dht[x]=1 'partie décimale de la temp toujours à 0
else
dht[x]=0
endif
next x

humidite=dht[31]*128+dht[30]*64+dht[29]*32+dht[28]*16+dht[27]*8+dht[26]*4+dht[25]*2+dht[24]*1
temp=dht[15]*128+ dht[14]*64+dht[13]*32+dht[12]*16+dht[11]*8+dht[10]*4+dht[9]*2+dht[8]*1
LCDOUT $FE,$80,"Humidite = ",#humidite,"% "
LCDOUT $FE,$C0,"Temperature = ",#temp,"C "
goto main

brunom
- 13th October 2013, 13:16
I forgot to mention i'm using a pic16F88 @ 8 MHz. So the pulsin is in 5 us increment (ex.: 14 = 14 * 5 us = 70 us and 5 =5 * 5us= 25 us) etc...

RuudNL
- 14th October 2013, 12:27
Thank you very much! I will try this and see if it works here!