The DHT11 sensor is a cheap import available on Ebay and elsewhere for less than $3.00 (US). It uses a propriatary one-wire interface where after a 18ms low start signal is received, it returns 40 "pulses" or bits of data. Each pulse represents a bit value where a 27us pulse is a "0" and a 72us pulse is a "1". Each pulse is separated by a 50us low period. 16 humidity bits are sent first followed by 16 temperature bits and then 8 checksum bits. The DHT11 has a resolution 1% RH and 1 degree Celsius and therefore sends all zeros for humidity bits 8 - 15 and temperature bits 8 - 15. So with the DHT11 we only need to process the 8 MSBits for both temperature and humidity and 8 bits for the checksum if error checking is used.
Included is the code that I came up with. It works well, but I am sure it could be improved since I struggled with the bit fiddling. I am used to Picaxe Basic where there are simple "setbit" and "clearbit" commands that operate on byte and word variables. In any case, I managed to muddle my way through it with ARMbasic, but likely in an inefficient manner.
Connecting the the DHT11 to the Basic Chip is straight forward. The output (Pin2) of the DHT11 is pulled up to 3.3v via a 4K7 resistor and goes to IO(7) on the Basic Chip. The DHT1l is supplied by 3.3v with a 100nf decoupling cap across the supply Pins.
Although the code works well, the performance of the DHT11 is less than stellar. It is slow to respond to changes in both temperature and humidity but is probably good enough for use in applications where fast response is not needed.
DHT11 Datasheet: http://www.micropik.com/PDF/dht11.pdf
EDIT: Removed unnecessary line : IO(7) = 1
Code: Select all
'************************************
'*** DHT-11 Humidity Sensor Demo ****
'************************************
#include <pulse.bas>
DIM a(8) as byte 'Array for bit twiddling
DIM temperature as integer
DIM humidity as integer
DIM checksum as integer
DIM i as integer
DIM c as integer
DIM tempvar as integer
MAIN:
wait(200) 'Stabilize
'======== Exercise DHT11 ==========================
gosub Sendstart 'Two cycles to exercise DHT11
wait(1000) 'at at startup. This should also
gosub sendstart 'be done if there are long pauses
wait (1000) 'between reads. eg minutes or hours
'===================================================
DO
c = c + 1 'Loop counter
gosub sendstart
a(0) = Pulsin(7,1) 'We don't don't use this bit
'but we need to read it
'===== Read Humidity Bits =========
humidity = 0
for i = 7 downto 0
a(i) = pulsin (7,1)
if a(i) < 30 then 'Bit is a "0"
a(i)= 0
else if a(i) >= 60 then 'Bit is a "1"
a(i) = 1
end if
a(i) = a(i) << i 'shift bit
humidity = humidity + a(i) 'SUM the bits
next i
'=================================
'=================================
for i = 7 downto 0 'Dummy reads.
a(i) = pulsin (7,1) 'With DHT22 we would read
next i 'Decimal bits here
'===================================
'====== Read Temperature bits ======
temperature = 0
for i = 7 downto 0
a(i) = pulsin (7,1)
if a(i) < 30 then 'Bit is a "0"
a(i)= 0
else if a(i) >= 60 then 'Bit is a "1"
a(i) = 1
end if
a(i) = a(i) << i
temperature = temperature + a(i) 'SUM the bits
next i
'====================================
'=====================================
for i = 7 downto 0 'Dummy reads.
a(i) = pulsin (7,1) 'With DHT22 we would read
next i 'Decimal bits here
'===================================
'===== Checksum =====================
checksum = 0
for i = 7 downto 0
a(i) = pulsin (7,1)
if a(i) < 30 then 'Bit is a "0"
a(i) = 0
else if a(i) >= 60 then 'Bit is a "1"
a(i) = 1
end if
a(i) = a(i) << i
checksum = checksum + a(i) 'SUM
next i
'=======================================
tempvar = temperature + humidity
Print "Loop ";c
Print "Humidity = ";humidity;chr(37);"RH"
Print "Temperature = ";temperature;chr(186);"C"
if tempvar = checksum then
print "Checksum Good"
else
print "Checksum Bad"
end if
Print 'Blank line
wait(2000) 'Minimum of 1000ms between reads
LOOP
SendStart:
WAIT(20)
PULSOUT(7,18000) '18ms "low" start signal to DHT11
Input(7) 'Pullup to high
return