DHT11 / DHT22 on a PICAXE - Solved in software

bobladell

New Member
Hi All

This might just be the solution everyone has been looking for - to be able to read the DHT11 / 22 humidity and temperature without any extra hardware like 555 timers. Just software - and all the way down to 16MHz. (Correction - not to 16MHz - external resonator fixes freq to 4 x resonator - see Technical post below)

To be fair to everyone else who has put loads of time into this, the solution has already been published several times but always with a timing / reliability / need for repeat readings / need for hardware support caveat.

All the pulsin C.2, 1, @bptrinc based solutions are correct. However, the key items that I haven't seen brought together anywhere before are :-

1 The DHT11 / 22 appear to have a quirk that demands a hard pull up at the end of the 18mS intialisation pulse (Credits to Craig Ringer's "Beware of Soapy Frogs" blog for this tip)

2 It only works on a PICAXE with external resonator - limiting it to a 28X2 or a 40X2. It will not work on any PICAXE that I've tried by using the internal resonator - so that lets out all the M2 / M2+ series - and lets out the 28X2 / 40X2 on setfreq mxx for the internal resonator. Must be external resonator to work.

3 Reliable detection of the end of the DHT acknowledgement - the 80uS low followed by 80uS high before sending data - so that the PICAXE can be synchronised to the start of the series of data pulses.

Note - The DHT11 datasheet is incorrect. It states that RH and T are in the 2nd and 4th bytes respectively. The DHT11 I have returns an integer value for each in the 1st and 3rd bytes. Bytes 2 and 4 are all zeros.

The solution is as follows :-

1 Choose a 28X2 or 40X2 based PICAXE board. I used the AXE401 Shield Base.

2 Fit a 16MHz external resonator

3 Use ports C.1 and C.2. C1 provides the 18mS DHT initialisation pulse and is then set to be an input. C.2 is an input only with the pulsin command behind it. It appears that the PICAXE takes some time to turn an output into an input hence the need for 2 ports. Using just one port failed to read any data.

4 Use the code below - which is the test software I used with the simple read out into Debug. I don't use any of the bx registers so it makes for an easy read in Debug.

ReadDHT2:
high C.1 'set port 1 high
let bptr = 0

' *************************************
' ***** Sensor Driver - read data *****
' *************************************

ReadSensor2:
input C.2 'Data in - make c.2 an input - 5k resistor pull up
setfreq em64 'Have to use em64 due to ext resonator = 16MHz. Can not use m16 etc internal resonator settings 'See Technical post below

'PICAXE Request To Send data
pulsout C.1,18000 'c.1 = low for 18mS (0.625uS / unit at 64MHz = 11.25mS)
input C.1

'RTSLow: if pinC.2 = 1 then goto RTSLow '(80uS) - Too slow so don't use this approach

'Ignore DHT 80uS low response, wait for high pulse, count it, store in b0 just for fun, start data store from b1

pulsin C.2, 1,@bptrinc 'wait for DHT rising edge - (80uS) DHT DSR then move on

'Having asked sensor for data then poll pin as fast as poss and store result

'RH1 'Inter data pulse 50uS = 80 (which doesn't show up on Debug)
pulsin C.2, 1,@bptrinc 'rising edge = start of 1st data pulse = bit 7 of RH Integer
pulsin C.2, 1,@bptrinc 'should be about 43 (27uS) for a 0 and 112 (70uS) for a 1 @ 64MHz - or any clock speed down to 16MHz
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc 'Note instead of @bptrinc I also used direct write to a b register e.g. b4 for this line. Works just as well.
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
'RH2
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
'T1
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
'T2
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
'Checksum
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc
pulsin C.2, 1,@bptrinc

setfreq m4
return


Have passed a copy of this lot to Rev-Ed for their review, so they are aware of the internal / external resonator quirk.

If anyone has any insight into the differences between the internal / external resonator operation that would be very interesting.

Otherwise - please enjoy :)


Bob
Edit 1 - 17/10/12 - updates following Technical response below
 
Last edited by a moderator:

Technical

Technical Support
Staff member
Thanks for the interesting feedback.
If you fit a 16MHz resonator to a 28x2 you are actually running at 64MHz due to the internal 4xPLL. So even though you (incorrectly) typed em16, the chip is actually running at em64 whatever emXX you use. The external speed is set by the physical resonator, not the emXX.
So that is probably why you can't use internal, as 64MHz is not possible internally on a 28X2.
 

Paix

Senior Member
Off Thead observation:
Either Bob is a very patient forum member who has been reading for a long time or is a more recent member and the Join Date mechanism if not being populated correctly.

Good to have a solution to a problem that has dogged several people Bob and thanks to Technical for demystifying the external resonator emm issue for you.
 

bobladell

New Member
Many thanks Technical.

There appears to be an anomaly in the manual - P221 of V7.9 - the first part lists all the m / em options for all parts indicating that even with an external resonator the PICAXE changes divider ratios. Further down the page it does state the fixed nature on using an external resonator and the fixed nature of the clock.

Explains why I was getting the same pulsin counts at all frequencies.

Would be good to understand the difference between internal and external resonators to see if we can operate at 32MHz - which the pulsin count numbers suggest should be possible.

Thanks again

Bob
 

bobladell

New Member
Read a lot - but not that patient. A 2012 PICAXE newbie - presume the join date etc is a web / forum thing.

Hope you find the solution - and the DHT - useful.

Bob
 

Goeytex

Senior Member
@ Technical.

Does a 28X2 with a 16mHz external resonator ( 64Mhz), have less command overhead than a 20x2 set for 64Mhz? I am trying to understand why a 28X2 can move from one Pulsin command to the next faster (< 50 us) than a 20X2 (>55us) given the same 64MHz clock frequency.
 

Technical

Technical Support
Staff member
@ Technical.

Does a 28X2 with a 16mHz external resonator ( 64Mhz), have less command overhead than a 20x2 set for 64Mhz? I am trying to understand why a 28X2 can move from one Pulsin command to the next faster (< 50 us) than a 20X2 (>55us) given the same 64MHz clock frequency.
Yes. The slight difference is with port redirection. 28 pin chip ports match the Microchip ports exactly. 20 pin chip i/o pin/ports need redirection, because Microchip do not use the more logical 'portC' on left - 'portB' on right layout that PICAXE uses (the raw PIC ports are very mixed up - study the Microchip datasheet for more info), and so it takes a few extra us to redirect the pin/port allocations on all i/o based commands.

So it is nothing to do with resonators, it is do with the way that 20 pin chips redirect ports whilst 28/40 pin chips don't.
 

Technical

Technical Support
Staff member
Would be good to understand the difference between internal and external resonators to see if we can operate at 32MHz - which the pulsin count numbers suggest should be possible.
There is no difference firmware wise, the PICAXE runs exactly the same code regardless of where the clock source is coming from.
 

Paix

Senior Member
Welcome aboard Bob and have lots of fun with your PICAXES even if you will drool over a lot more techniques than you will ever implement. it's all good stuff and makes so much possible with a little knowledge and not a lot of cash.

The Join Date in the left hand margin just below your username is indeed the forum item that I mentioned. 01 Jan 1970 is the Linux/Unix epoch date and indicates that the numeric date field came up empty. In the early days, see Hippy and Dippy etc. the feature didn't work or wasn't there and so when the Bulletin Board software implemented the feature there was no meaningful data to be added and so in traditional database practice it remained at 0 with the result that January 1970 is displayed. I suspect that if you are a 2012 new member, that something has gone wrong with the joining-up process as the field should be filled in automatically when your account is registered. Small potatoes and not as much fun as the chips and ideas that go with them, but it no one points out the little cracks they won't get fixed and could be indicative of an oversight that is better fixed sooner rather than later or not at all.
 

tcpip

New Member
Thanks for the solution. I've build the circuit and loaded the software. After adding this code:
Code:
setfreq m4 

bptr = 0
	for b8 = 0 to 40
		
		sertxd(#@bptrinc,",")
	next b8
	sertxd(13,10)
	sertxd(13,10)
This is the outcome:129,42,42,43,42,42,43,42,116,117,43,117,118,42,118,42,116,42,42,42,42,42,42,42,41,117,118,42,42,117,42,117,116,117,42,42,43,42,42,43,116,

How can I turn this into the RH and Temp?
 

g6ejd

Senior Member
I make that:

129,42,42,43,42,42,43,42 116,117,43,117,118,42,118,42 116,42,42,42,42,42,42,42 41,117,118,42 ,42,117,42,117 116,117,42,42,43,42,42,43 116,

10000000 11011010 10000000 01100101 11000000 1 - unsure about last bit, you have 41 readings (0-40) is that correct?

if @bptrinc contents is say > 100 = 1 else 0, or perhaps (129+42)/2 is the threshold, so if @bptrinc > 85 then value = 1 else 0, then as you walk down the bits you multple each result by 128, then 64, then 32 adding each partial result as you go. It depends if the sensor data is BigEndian or LittleEndian, or in simple terms is first bit a 0 or a 128.

It's Bigendian data: Data consists of decimal and integral parts. A complete data transmission is 40bit, and the sensor sends higher data bit first.

Data format: 8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data + 8bit check sum. If the data transmission is right, the check-sum should be the last 8bit of "8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data".

73% RH and not sure about the units 19C - that's a guess on the data format.

It's not clear what the check-sum is or what the data format is yet...it looks like 2's complement and odd or even parity bit for all 40-bits.

OK, this is the correct data format: There are 5-bytes, first two are RH.RHdecimal next two are Temp.Tempdecimal last byte is the check sum:

DATA = 8 bits of integral RH data + 8 bits of decimal RH data + 8 bits of integral temperature data + 8 bits
of decimal temperature data + 8 bit check-sum

these are the data bytes:
10000000 11011010 10000000 01100101

Now begining to make sense, anyone figured this out yet?

humidity = 0 'Clear humidity variable B2
for ptr = 1 to 8 'Read bits 1 - 8 (ignoring bit 0)
bit_num = 8 - ptr 'Bit# 7 - 0 for Humidity Var B2

if @ptr > 85 then
setbit humidity,bit_num 'set b2 bit(x) to 1
endif
if @ptr < 85 then 'set b2 bit(x) to 0
clearbit humidity,bit_num
endif
next


Temperature = 0 'Clear Temperature variable B4
for ptr = 17 to 24 'Read DHT-11 bits 17 - 24
bit_num = 24 - ptr 'Bit# 7 - 0 for Temperature Var B2

'Last Humidity reading is now in variable b2

if @ptr > 85 then 'set b4 bit(x) to 1
setbit temperature,bit_num
endif
if @ptr < 85 then 'set b4 bit(x) to 0
clearbit temperature,bit_num
endif
next

'Last Temperature reading is now in variable b4

sertxd ("Temperature: ",#Temperature,176,"C",cr,lf)
sertxd ("Rel Humidity: ",#humidity,"%",cr,lf,cr,lf)
 
Last edited:

Goeytex

Senior Member
The first bit "129" represents the DHT's 80us response signal and needs to be ignored. (See Datasheet). Then calculate Humidity & Temperature data from the next 40 bits starting at scratchpad location 1.

Code:
                            Humidity                 Temperature               Checksum   
                     High Byte   Low Byte         High Byte  Low Byte           
So the binary is:    00000001    10110101         00000000   11001011          10000001     
                     DEC   437                    DEC    203                                              
                      43.7%  RH                     20.3 C
 

tcpip

New Member
Thanks for the help, g6ejd and Goeytex!
Now I still need to figure out how to calculate Humidity & Temperature data from the scratchpad (40 bits). Any idea?
 

Goeytex

Senior Member
I have some code for that around here somewhere. I'll dig it up and post it later.

Edit: Here it is

Code:
[COLOR="#008000"]'===== Partial Code For DHT22 =========== [/COLOR]

Symbol bit_num = b0  [COLOR="#008000"]'resuable variable[/COLOR]
Symbol Humidity = W3
Symbol Temperature = W4

[COLOR="#008000"]' Assuming Sensor has be read and 41 bytes of data in Scratchpad beginning at
'location 0, where location 0 holds irrelevant byte.  [/COLOR]


[COLOR="#008000"]'To Do: Write subroutine for checksum  [/COLOR]


Main: 

    Do 
        Gosub Calc_Humidity
        Calc_Temperature
   Loop



Calc_Humidity:
[COLOR="#008000"]'==========================================================[/COLOR]
  humidity = 0  [COLOR="#008000"]' Clear the variable [/COLOR]

  for ptr = 1 to 16  [COLOR="#008000"]'Ignore first byte so start at location 1 [/COLOR]
      bit_num = 16 - ptr     
    
    	if @ptr > 30 and @ptr < 90 then
    		clearbit humidity ,bit_num  [COLOR="#008000"]'Redundant. It is already 0 [/COLOR]
    	
    	elseif @ptr > 90 and @ptr < 125 then
    		setbit humidity,bit_num  
    	endif
  next
 
  bintoascii humidity,b0,b0,b1,b2,b3 
  sertxd (b0,b1,b2,".",b3,"% RH",cr,lf) 

return
[COLOR="#008000"]'==================  END SUB ====================================[/COLOR]



Calc_Temperature: 
[COLOR="#008000"]'===============================================================  [/COLOR]
  temperature = 0

  for ptr = 17 to 32  
      
      bit_num = 32 - ptr     
    
    	if @ptr > 30 and @ptr < 90 then
    		clearbit temperature,bit_num  [COLOR="#008000"]'Redundant. It is already 0[/COLOR]
    	
    	elseif @ptr > 90 and @ptr < 125 then
    		setbit temperature,bit_num  
    	endif
  next 
 
 bintoascii temperature,b0,b0,b1,b2,b3
 sertxd (b0,b1,b2,".",b3,186,"C",cr,lf)   [COLOR="#008000"] '186 should be degree character.[/COLOR]

return 
[COLOR="#008000"]'======================== End SUB =======================================[/COLOR]
 
Last edited:

planchibus

New Member
Hello:
I have a board 18M2 chip CHI035 +. What changes I have to do in your code for me to work?. I'm a little lost.
thanks

Hi All

This might just be the solution everyone has been looking for - to be able to read the DHT11 / 22 humidity and temperature without any extra hardware like 555 timers. Just software - and all the way down to 16MHz. (Correction - not to 16MHz - external resonator fixes freq to 4 x resonator - see Technical post below)

To be fair to everyone else who has put loads of time into this, the solution has already been published several times but always with a timing / reliability / need for repeat readings / need for hardware support caveat.

All the pulsin C.2, 1, @bptrinc based solutions are correct. However, the key items that I haven't seen brought together anywhere before are :-

1 The DHT11 / 22 appear to have a quirk that demands a hard pull up at the end of the 18mS intialisation pulse (Credits to Craig Ringer's "Beware of Soapy Frogs" blog for this tip)

2 It only works on a PICAXE with external resonator - limiting it to a 28X2 or a 40X2. It will not work on any PICAXE that I've tried by using the internal resonator - so that lets out all the M2 / M2+ series - and lets out the 28X2 / 40X2 on setfreq mxx for the internal resonator. Must be external resonator to work.

3 Reliable detection of the end of the DHT acknowledgement - the 80uS low followed by 80uS high before sending data - so that the PICAXE can be synchronised to the start of the series of data pulses.

Note - The DHT11 datasheet is incorrect. It states that RH and T are in the 2nd and 4th bytes respectively. The DHT11 I have returns an integer value for each in the 1st and 3rd bytes. Bytes 2 and 4 are all zeros.

The solution is as follows :-

1 Choose a 28X2 or 40X2 based PICAXE board. I used the AXE401 Shield Base.

2 Fit a 16MHz external resonator

3 Use ports C.1 and C.2. C1 provides the 18mS DHT initialisation pulse and is then set to be an input. C.2 is an input only with the pulsin command behind it. It appears that the PICAXE takes some time to turn an output into an input hence the need for 2 ports. Using just one port failed to read any data.

4 Use the code below - which is the test software I used with the simple read out into Debug. I don't use any of the bx registers so it makes for an easy read in Debug.

Code:
ReadDHT2:
	high C.1					'set port 1 high
	let bptr = 0
	
' *************************************
' ***** Sensor Driver - read data *****
' *************************************

ReadSensor2:
	input C.2					'Data in - make c.2 an input - 5k resistor pull up
	setfreq em64				'Have to use em64 due to ext resonator = 16MHz. Can not use m16 etc internal resonator settings	                                                                       'See Technical post below

'PICAXE Request To Send data
	pulsout C.1,18000				'c.1 = low for 18mS (0.625uS / unit at 64MHz = 11.25mS)
	input C.1	

'RTSLow:	if pinC.2 = 1 then goto RTSLow	              '(80uS) - Too slow so don't use this approach

	'Ignore DHT 80uS low response, wait for high pulse, count it, store in b0 just for fun, start data store from b1

	pulsin C.2, 1,@bptrinc			'wait for DHT rising edge - (80uS) DHT DSR then move on

'Having asked sensor for data then poll pin as fast as poss and store result

'RH1						'Inter data pulse 50uS = 80 (which doesn't show up on Debug)
	pulsin C.2, 1,@bptrinc			'rising edge = start of 1st data pulse = bit 7 of RH Integer
	pulsin C.2, 1,@bptrinc			'should be about 43 (27uS) for a 0 and 112 (70uS) for a 1 @ 64MHz - or any clock speed down to 16MHz
	pulsin C.2, 1,@bptrinc                                  
	pulsin C.2, 1,@bptrinc                                       'Note instead of @bptrinc I also used direct write to a b register e.g. b4 for this line. Works just as well.
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
'RH2	
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
'T1
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
'T2
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
'Checksum
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc
	pulsin C.2, 1,@bptrinc

setfreq m4	
	return
Have passed a copy of this lot to Rev-Ed for their review, so they are aware of the internal / external resonator quirk.

If anyone has any insight into the differences between the internal / external resonator operation that would be very interesting.

Otherwise - please enjoy :)


Bob
Edit 1 - 17/10/12 - updates following Technical response below
 
Last edited by a moderator:

hippy

Technical Support
Staff member
Hello:
I have a board 18M2 chip CHI035 +. What changes I have to do in your code for me to work?. I'm a little lost.
thanks
From the quote you included -

2 It only works on a PICAXE with external resonator - limiting it to a 28X2 or a 40X2. It will not work on any PICAXE that I've tried by using the internal resonator - so that lets out all the M2 / M2+ series
It would appear that you cannot use the code with your PICAXE-18M2, you would need to use a PICAXE 28X2 or 40X2 and using a 16MHz resonator.
 

Axel87

Senior Member
I am currently working on this build, tried with my first attempt to build this with an 8m2 but no win.

Found this thread but am confused with Technical's responses and original posters findings.
Originally stated, the DHT11 on a picaxe requires a external resonator-
Technical- "There is no difference firmware wise, the PICAXE runs exactly the same code regardless of where the clock source is coming from. " and
"So it is nothing to do with resonators, it is do with the way that 20 pin chips redirect ports whilst 28/40 pin chips don't. "

Willing to try this out, either way but need to know if I should order up a resonator or not :/?

Does anyone have a suggestion for a better temp/humidity sensor that can easily be integrated with PICAXE? This one is way over my head but Ill try and keep up!
 

Goeytex

Senior Member
I posted a somewhat simpler way of using a DHT11 with a Picaxe. However it requires a Picaxe 20X2 combined with either either a 74HC221 or a 555 timer.

Another humidity sensing device can that be used with an M2 series Picaxe is the HR202. There are circuits and code posted on the Internet to draw from. The trick with these is to use 2 IO Pins and one ADC pin on the Picaxe.
 

westaust55

Moderator
@Axel87,
Keep in mind that if you are intending to use an 08M2 (or other M or M2 part) that there is no provision to add a resonator.
Only X1 and X2 parts have pins available for an external resonator
 

Axel87

Senior Member
@Axel87,
Keep in mind that if you are intending to use an 08M2 (or other M or M2 part) that there is no provision to add a resonator.
Only X1 and X2 parts have pins available for an external resonator
Perfect, I ordered the Honeywell sensor. Got 2x, figure good things come in pairs right ? ;0
Anyhow, I wasent set on using a 08, was just reading GoeyTex thread( Nicely done btw!!) and fig those were alot of wasted pins and tried an 08. But realized later the 08 wasent possible.
I appear to have ordered quiet a few of the DHT11, so I will give it another shot with a higher picaxe. I believe I have more lying around.
Thanks again!
 
Last edited:

Goeytex

Senior Member
If you decide to use the DHT11 with a Picaxe 20X2 with the code I posted, the Picaxe frequency can be set to 64Mhz for reading the sensor and then reduced for the rest of the code. This will save power in a battery powered project.

I have had that setup in several egg incubators where the Picaxe controls the humidity and the turning motor. Been working without a hitch for about three years now. I will be replacing the humidity sensors this year as routine maintenance since they are in a fairly harsh environment and are probably contaminated with all kinds crud & of residue .

I have also used the HIH-4000 series sensors. They are accurate and probably the easiest to interface with a Picaxe. Just a bit pricey.
 

Skiwi

New Member
I have not tried the other devices mentioned, but a comment on the DHT11 and DHT22 mentioned. They are the same, but different in 4 ways
The DHT22 is 5 times the price
The DHT22 is more accurate
The DHT22 measures negative temperature
The DHT22 data format is different.
As mentioned previously in this thread, the DHT11 format is 8bit integralRH+8bit decimalRH+8bitintegralT+8bit decimalT+8bit checksum

There are 2 versions of datasheet on the DHT22. One from Aosong Electronics gives the same data format as the DHT11. When I tried to make it work, I got rubbish.
The I found the Maxdetect datasheet with the correct data format.
16bits RH data + 16bits T data + checksum
The RH and T data is decimal x10 So divide by 10 to get the answer (eg datasheet example, 010 1000 1101 = 652 decimal, 652/10 = 65.2% )

I have used the code given previously in this forum and edited for the DHT22
It seems to be much better that the DHT11, I compare with data from the NZ Metservice, and also temperature data from a DSB1820

Code:
#picaxe 40x2



let dirsc = %00000010

#no_table
setfreq em32

init: 

symbol bit_num = b0
symbol Humidity_MSB = b3		'MSB  W1
symbol Humidity_LSB = b2		'LSB
Symbol Temp_MSB = b5			'MSB W2
Symbol Temp_LSB = b4			'LSB 
Symbol Checksum = b6
b38=0

get_data:
	pause 5500  '  test speed up12500             'Stabilize 2 seconds
	setfreq em64             'Must read Sensor @ 64mhz ! 
	ptr =0                 'Set Scratch Pad pointer to zero 
	high C.0

	pulsout c.0, 1800	'35000    'Request Data  1800 for DHT22,  35000 for DHT11 
					
	pauseus 10				'Let MCU start signal settle


	pulsin c.1,1,@ptrinc    'First bit (will be ignored later)
		
	pulsin c.1,1,@ptrinc    'Humidity MS bits  SP Location 1
	pulsin c.1,1,@ptrinc    'SP location 2
	pulsin c.1,1,@ptrinc	'SP location 3 
	pulsin c.1,1,@ptrinc	'SP location 5
	pulsin c.1,1,@ptrinc	'SP location 6
	pulsin c.1,1,@ptrinc	'SP location 7
	pulsin c.1,1,@ptrinc	'SP location 8
	pulsin c.1,1,@ptrinc	'SP location 9

	pulsin c.1,1,@ptrinc   'Humidity MS bits  SP Location 9
	pulsin c.1,1,@ptrinc   '   
	pulsin c.1,1,@ptrinc   '
	pulsin c.1,1,@ptrinc   '  
	pulsin c.1,1,@ptrinc   '  
	pulsin c.1,1,@ptrinc   '  
	pulsin c.1,1,@ptrinc   '  
	pulsin c.1,1,@ptrinc   '  


	pulsin c.1,1,@ptrinc  ' Temperature MS bits  SP 17
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc

	pulsin c.1,1,@ptrinc  ' Temperature LS bits	SP 25
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc

	pulsin c.1,1,@ptrinc  ' CHECKSUM bits		SP 33
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc


	setfreq m8               'Reduce Frequency for Serial @9600 
	pause 100

' HUMIDITY MSB DECODING

	Humidity_MSB = 0              'Clear humidity MSB variable B3
	for ptr = 1 to 8          'Read bits 1 - 8 (ignoring bit 0) 
	bit_num = 8 - ptr         'Bit# 7 - 0 for Humidity Var b3 
	
	if @ptr > 80 then         
	setbit Humidity_MSB,bit_num   'set b3 bit(x) to 1
	endif
	if @ptr < 60 then         'set b3 bit(x) to 0
	clearbit Humidity_MSB,bit_num 
	endif
	next

' HUMIDITY LSB DECODING

	Humidity_LSB = 0              '
	for ptr = 9 to 16          '
	bit_num = 16 - ptr         ' 

	if @ptr > 80 then         
	setbit Humidity_LSB,bit_num   'set b2 bit(x) to 1
	endif
	if @ptr < 60 then         'set b2 bit(x) to 0
	clearbit Humidity_LSB,bit_num 
	endif
	next



'TEMPERATURE MSB DECODING
	'*** Not using temperature for this application ************

Temp_MSB = 0             'Clear Temperature variable b5
for ptr = 17 to 24          '
bit_num = 24 - ptr          '



if @ptr > 80 then            'set b5 bit(x) to 1
setbit Temp_MSB,bit_num
endif 
if @ptr < 60 then            'set b5 bit(x) to 0 
clearbit Temp_MSB,bit_num
endif
next

'TEMPERATURE LSB DECODING


Temp_LSB = 0             'Clear Temperature variable b4
for ptr = 25 to 32          '
bit_num = 32 - ptr          '


if @ptr > 80 then            'set b5 bit(x) to 1
setbit Temp_LSB,bit_num
endif 
if @ptr < 60 then            'set b5 bit(x) to 0 
clearbit Temp_LSB,bit_num
endif
next


'CHECKSUM BIT DECODING


'CHECKSUM = 0             'Clear Checksum
'for ptr = 33 to 40          '
'bit_num = 40 - ptr          '

'CHECKSUM reading is now in variable b6

'if @ptr > 80 then            'set b4 bit(x) to 1
'setbit CHECKSUM,bit_num
'endif 
'if @ptr < 60 then            'set b4 bit(x) to 0 
'clearbit CHECKSUM,bit_num
'endif
'next


'CALCULATE DATA CHECK TO COMPARE TO CHECKSUM

'b7 = b2+b3+b4+b5

'sertxd ("Checksum ",#b6,"  Calculated check ",#b7,cr,lf)




	put 87, word W2	'temp
	PUT 55, word W1   ' =humidity


	b38=b38+1
	if b38>=2 then
	b38=0
	goto last_line
	else goto get_data
	endif

	Last_line:
 

Axel87

Senior Member
I have not tried the other devices mentioned, but a comment on the DHT11 and DHT22 mentioned. They are the same, but different in 4 ways
The DHT22 is 5 times the price
The DHT22 is more accurate
The DHT22 measures negative temperature
The DHT22 data format is different.
As mentioned previously in this thread, the DHT11 format is 8bit integralRH+8bit decimalRH+8bitintegralT+8bit decimalT+8bit checksum

There are 2 versions of datasheet on the DHT22. One from Aosong Electronics gives the same data format as the DHT11. When I tried to make it work, I got rubbish.
The I found the Maxdetect datasheet with the correct data format.
16bits RH data + 16bits T data + checksum
The RH and T data is decimal x10 So divide by 10 to get the answer (eg datasheet example, 010 1000 1101 = 652 decimal, 652/10 = 65.2% )

I have used the code given previously in this forum and edited for the DHT22
It seems to be much better that the DHT11, I compare with data from the NZ Metservice, and also temperature data from a DSB1820

Code:
#picaxe 40x2



let dirsc = %00000010

#no_table
setfreq em32

init: 

symbol bit_num = b0
symbol Humidity_MSB = b3		'MSB  W1
symbol Humidity_LSB = b2		'LSB
Symbol Temp_MSB = b5			'MSB W2
Symbol Temp_LSB = b4			'LSB 
Symbol Checksum = b6
b38=0

get_data:
	pause 5500  '  test speed up12500             'Stabilize 2 seconds
	setfreq em64             'Must read Sensor @ 64mhz ! 
	ptr =0                 'Set Scratch Pad pointer to zero 
	high C.0

	pulsout c.0, 1800	'35000    'Request Data  1800 for DHT22,  35000 for DHT11 
					
	pauseus 10				'Let MCU start signal settle


	pulsin c.1,1,@ptrinc    'First bit (will be ignored later)
		
	pulsin c.1,1,@ptrinc    'Humidity MS bits  SP Location 1
	pulsin c.1,1,@ptrinc    'SP location 2
	pulsin c.1,1,@ptrinc	'SP location 3 
	pulsin c.1,1,@ptrinc	'SP location 5
	pulsin c.1,1,@ptrinc	'SP location 6
	pulsin c.1,1,@ptrinc	'SP location 7
	pulsin c.1,1,@ptrinc	'SP location 8
	pulsin c.1,1,@ptrinc	'SP location 9

	pulsin c.1,1,@ptrinc   'Humidity MS bits  SP Location 9
	pulsin c.1,1,@ptrinc   '   
	pulsin c.1,1,@ptrinc   '
	pulsin c.1,1,@ptrinc   '  
	pulsin c.1,1,@ptrinc   '  
	pulsin c.1,1,@ptrinc   '  
	pulsin c.1,1,@ptrinc   '  
	pulsin c.1,1,@ptrinc   '  


	pulsin c.1,1,@ptrinc  ' Temperature MS bits  SP 17
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc

	pulsin c.1,1,@ptrinc  ' Temperature LS bits	SP 25
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc

	pulsin c.1,1,@ptrinc  ' CHECKSUM bits		SP 33
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc
	pulsin c.1,1,@ptrinc


	setfreq m8               'Reduce Frequency for Serial @9600 
	pause 100

' HUMIDITY MSB DECODING

	Humidity_MSB = 0              'Clear humidity MSB variable B3
	for ptr = 1 to 8          'Read bits 1 - 8 (ignoring bit 0) 
	bit_num = 8 - ptr         'Bit# 7 - 0 for Humidity Var b3 
	
	if @ptr > 80 then         
	setbit Humidity_MSB,bit_num   'set b3 bit(x) to 1
	endif
	if @ptr < 60 then         'set b3 bit(x) to 0
	clearbit Humidity_MSB,bit_num 
	endif
	next

' HUMIDITY LSB DECODING

	Humidity_LSB = 0              '
	for ptr = 9 to 16          '
	bit_num = 16 - ptr         ' 

	if @ptr > 80 then         
	setbit Humidity_LSB,bit_num   'set b2 bit(x) to 1
	endif
	if @ptr < 60 then         'set b2 bit(x) to 0
	clearbit Humidity_LSB,bit_num 
	endif
	next



'TEMPERATURE MSB DECODING
	'*** Not using temperature for this application ************

Temp_MSB = 0             'Clear Temperature variable b5
for ptr = 17 to 24          '
bit_num = 24 - ptr          '



if @ptr > 80 then            'set b5 bit(x) to 1
setbit Temp_MSB,bit_num
endif 
if @ptr < 60 then            'set b5 bit(x) to 0 
clearbit Temp_MSB,bit_num
endif
next

'TEMPERATURE LSB DECODING


Temp_LSB = 0             'Clear Temperature variable b4
for ptr = 25 to 32          '
bit_num = 32 - ptr          '


if @ptr > 80 then            'set b5 bit(x) to 1
setbit Temp_LSB,bit_num
endif 
if @ptr < 60 then            'set b5 bit(x) to 0 
clearbit Temp_LSB,bit_num
endif
next


'CHECKSUM BIT DECODING


'CHECKSUM = 0             'Clear Checksum
'for ptr = 33 to 40          '
'bit_num = 40 - ptr          '

'CHECKSUM reading is now in variable b6

'if @ptr > 80 then            'set b4 bit(x) to 1
'setbit CHECKSUM,bit_num
'endif 
'if @ptr < 60 then            'set b4 bit(x) to 0 
'clearbit CHECKSUM,bit_num
'endif
'next


'CALCULATE DATA CHECK TO COMPARE TO CHECKSUM

'b7 = b2+b3+b4+b5

'sertxd ("Checksum ",#b6,"  Calculated check ",#b7,cr,lf)




	put 87, word W2	'temp
	PUT 55, word W1   ' =humidity


	b38=b38+1
	if b38>=2 then
	b38=0
	goto last_line
	else goto get_data
	endif

	Last_line:
Thanks for the input Skiwi, I did happen to have a 40x2 lying around so I am trying to wire it up right now.
I am just going to list my questions as I work on this so I dont forget to ask something as this goes.
The connection diagram I am using is from pg 97 in Section 1
I am basing my connections off of Goeytex diagram
I copied Skiwi's code above as it was based for the same chip I had available.

1- First thing I noticed is that with the 40x2 is co vs C.0 with the 8m2. Why the change from c0 to C.0?
Connection
5V- pin 11
GND- pin 12
Anode end of diode to pin 15 c0
pin 3 of NE555 to pin 16 c1

Upon programming- no errors.
I open up the serial terminal and I am not receiving any information.
I apologize, this is above my head so I dont know were to begin troubleshooting.
This 40x2 has been sitting on my shelf for months as I new when I used this I would be opening a whole can of worms :/.

Any ideas guys?
Should I be getting some sort of information right now in serial terminal?
 

Goeytex

Senior Member
You will never get any terminal output with the code you posted, because it is incomplete and has no serout or sertxd command to tx the data to the terminal. The program abruptly ends when it goes to "last_line" and never repeats.

Go back and put the sections of the code back in that send data to the terminal.

EDIT: The code posted by Skiwi is for the DHT22. Furthermore, it is incomplete in that it does not send the decoded and formatted data to the serial terminal. Since you are using a DHT11, you should be using the code I posted ==> [URL="http://www.picaxeforum.co.uk/showthread.php?19459-DHT11-Humidity-Sensor&p=182634&viewfull=1#post182634"HERE <== as a starting point.

IF you do not have a scope, it may be tricky to get the pulse width correct for the 555 timer. It needs to be 65 microseconds. If you do not have a scope, you can write a simple program for the Picaxe using pulsin to calibrate the 555 timer output.
 
Last edited:

Axel87

Senior Member
You will never get any terminal output with the code you posted, because it is incomplete and has no serout or sertxd command to tx the data to the terminal. The program abruptly ends when it goes to "last_line" and never repeats.

Go back and put the sections of the code back in that send data to the terminal.

EDIT: The code posted by Skiwi is for the DHT22. Furthermore, it is incomplete in that it does not send the decoded and formatted data to the serial terminal. Since you are using a DHT11, you should be using the code I posted ==> [URL="http://www.picaxeforum.co.uk/showthread.php?19459-DHT11-Humidity-Sensor&p=182634&viewfull=1#post182634"HERE <== as a starting point.

IF you do not have a scope, it may be tricky to get the pulse width correct for the 555 timer. It needs to be 65 microseconds. If you do not have a scope, you can write a simple program for the Picaxe using pulsin to calibrate the 555 timer output.
Goeytex- I also tried your code, and got the same results with no output on the serial terminal.

I do have a scope, although I am rusty, if I have some basics steps on how/what to look for I could maybe get it done. I usually use it for basic audio tests.
I will triple check my breadboard layout, and wait to hear back for any other help.
Thanks guys!
 

Skiwi

New Member
Axel87, hope I havent confused your DHT11 project. The intention of my post was to draw attention to the differences between the DHT11 and the DHT22.
As correctly observed, my code will not give an output, as it was written as the code for Slot 3, in a Weather station project with a BMP085 code in slot 2, and snow and rain measurement code in slot 1, and interface to a NET001 in slot0. The code in the Slot 0 takes the Word values to process/output.
So to re-use my code, you would need to write something to show the values in Humitidy_MSB and Humidity_LSB relevant to your application.
What is also important to note, as observed, is that there are 2 ports used as per the original DHT11 code of Goeytex. I have added a clip from my circuit to show what I did. circuit clip.jpg
 

Goeytex

Senior Member
My code has serial out routines and SHOULD output data to the terminal. The Skiwi code code will NEVER output data to the terminal. If My code does not send data to the terminal, then here are some possibilities:

1. The connections are wrong.
2. The DHT11 has no power/ ground or is wired backwards
3. Pulsin is not receiving data from the DT11 and is timing out
4. You have modified the code so that it not longer works
5. The 555 timer is not generating a pulse.

I will help you get my code working. I will not help you with the other code. Fair enough ?

Troubleshooting should be a deliberate, methodical process. I find it easiest to troubleshoot hardware first and then software/code. Code can many times be used to troubleshoot the hardware. Troubleshooting code is best done one small section at a time. I prefer to use sertxd for debugging since "debug" does not play nice with some other commands.

First, wire the 555 timer as show in the diagram I posted, but use a 10K pot for R3 and set it to center position. Use the same pins (c.0, C.1) for I/O that I used for the 20X2 so that the code labels/ pins do not need to be changed. If you have changed the 555 timing capacitor to something other than 10nf, you need to tell me. If you are not using a "CMOS" 555 timer, you need to tell me. You need to disclose ANY deviations from the original circuit.

Then load up the code below for troubleshooting. You should see the Messages on the terminal, and then a line that displays the nine data bytes.

After running this, let us know exactly what was displayed.

Code:
#picaxe 40X2
#no_table
#terminal 38400  

setfreq EM32
high c.0



INIT: 

symbol bit_num = b0
symbol humidity = b2
Symbol temperature = b4 


' ********  For Debugging  **************************

pause 8000 '// Give terminal time to open

sertxd ("Testing DHT11 with Picacxe 40X2",cr,lf,cr,lf)

pause 5000

sertxd ("Quering DHT11 .....",cr,lf)


TEST: 

	setfreq EM64	'//  "Assuming 40X2 with external 16Mhz crystal/ resonator)
               		'//  40X2 will not operate at 64 mhz without a 16MHZ  External resonator 
                     

	ptr = 0                 '// Set Scratch Pad pointer to zero 

	pulsout c.0, 35000      '// About 22ms to Req Data from DHT-11  

	pulsin c.1,0,@ptrinc   
	pulsin c.1,0,@ptrinc    
	pulsin c.1,0,@ptrinc    
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc

	pause 1000


	setfreq EM32

		for ptr = 0 to 8   ;// 9 bytes.  
   			sertxd (#@ptr, "  ")
		next

	sertxd (CR,LF)
	sertxd ( "Done reading first 9 bits",cr,lf,cr,lf)

	pause 12500  '// Delay between reads 

GOTO TEST

'************************  End Debuging Test  ***************************
 

Axel87

Senior Member
My code has serial out routines and SHOULD output data to the terminal. The Skiwi code code will NEVER output data to the terminal. If My code does not send data to the terminal, then here are some possibilities:

1. The connections are wrong.
2. The DHT11 has no power/ ground or is wired backwards
3. Pulsin is not receiving data from the DT11 and is timing out
4. You have modified the code so that it not longer works
5. The 555 timer is not generating a pulse.

I will help you get my code working. I will not help you with the other code. Fair enough ?

Troubleshooting should be a deliberate, methodical process. I find it easiest to troubleshoot hardware first and then software/code. Code can many times be used to troubleshoot the hardware. Troubleshooting code is best done one small section at a time. I prefer to use sertxd for debugging since "debug" does not play nice with some other commands.

First, wire the 555 timer as show in the diagram I posted, but use a 10K pot for R3 and set it to center position. Use the same pins (c.0, C.1) for I/O that I used for the 20X2 so that the code labels/ pins do not need to be changed. If you have changed the 555 timing capacitor to something other than 10nf, you need to tell me. If you are not using a "CMOS" 555 timer, you need to tell me. You need to disclose ANY deviations from the original circuit.

Then load up the code below for troubleshooting. You should see the Messages on the terminal, and then a line that displays the nine data bytes.

After running this, let us know exactly what was displayed.

Code:
#picaxe 40X2
#no_table
#terminal 38400  

setfreq EM32
high c.0



INIT: 

symbol bit_num = b0
symbol humidity = b2
Symbol temperature = b4 


' ********  For Debugging  **************************

pause 8000 '// Give terminal time to open

sertxd ("Testing DHT11 with Picacxe 40X2",cr,lf,cr,lf)

pause 5000

sertxd ("Quering DHT11 .....",cr,lf)


TEST: 

	setfreq EM64	'//  "Assuming 40X2 with external 16Mhz crystal/ resonator)
               		'//  40X2 will not operate at 64 mhz without a 16MHZ  External resonator 
                     

	ptr = 0                 '// Set Scratch Pad pointer to zero 

	pulsout c.0, 35000      '// About 22ms to Req Data from DHT-11  

	pulsin c.1,0,@ptrinc   
	pulsin c.1,0,@ptrinc    
	pulsin c.1,0,@ptrinc    
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc	
	pulsin c.1,0,@ptrinc

	pause 1000


	setfreq EM32

		for ptr = 0 to 8   ;// 9 bytes.  
   			sertxd (#@ptr, "  ")
		next

	sertxd (CR,LF)
	sertxd ( "Done reading first 9 bits",cr,lf,cr,lf)

	pause 12500  '// Delay between reads 

GOTO TEST

'************************  End Debuging Test  ***************************
Thanks Goeytex,
I believe I understand my mistake.
I have not had experience with the sertxd command yet.
Believe I have a few of the NE555 timers lying around, will try swapping out. Maybe this ones faulty.
I am using a 40x2, which should be similar correct? The pin numbers will be different.
This should be the only deviations from your original post.
I will be out of town for a couple of days, but will get back to you.
Thank you for your continued support!
 

Skiwi

New Member
I have also added my DHT11 version of code, the original work was done by others in this forum, I cant remember who.

It does not use the 555 as Goeytex,s version does, rather does it all in software, as per the thread title.
It still requires the 16M crystal/resonator.
It also uses the 2 ports C.0 and C.1, with the diode, as per my previous post jpg
But the difference between Goeytex's code is in the pulsin command. He uses pulsin c.1,0, I use pulsin c.1,1. This will be because of the way the 555 changes the phase of the signal pulse from the DHT11. My version (adapation) times the data pulse from the leading edge of the pulse from the DHT11 (see pulsin command in picaxe manual2)

Also, I found that the pause at the start is critical. Here I used Wait 6, being a 2 second wait, when the chip is being clocked at em16.
(On the DHT22, I used pause 5500. Anything under 4000 was too fast, over 12500 too slow)
get this wrong, you get no data.

Hope Im adding ideas, not confusing the issue

cheers


Code:
#picaxe 40x2
#no_table
setfreq m8
high c.0
init: 
ptr = 0
b38=0
symbol bit_num = b50
symbol humidity = b51
Symbol temperature = b52 

let dirsc = %00000010

get_data:


wait 6             'Stabilize 2 seconds

setfreq em64             'Must run at 64MHz to get Sensor pulse timing fast enough ! 
ptr = 0                 'Set Scratch Pad pointer to zero 

'	C.0 and C.1 in parallel to allow write/read fast enough.

pulsout c.0, 35000    '(20000 seemed to work better?)  'Req Data from DHT-11  

pulsin c.1,1,@ptrinc    'First bit (DHT response, will be ignored later)

pulsin c.1,1,@ptrinc    'Humidity Integer bits  SP Location 1
pulsin c.1,1,@ptrinc    'SP location 2
pulsin c.1,1,@ptrinc	'SP location 3 
pulsin c.1,1,@ptrinc	'SP location 5
pulsin c.1,1,@ptrinc	'SP location 6
pulsin c.1,1,@ptrinc	'SP location 7
pulsin c.1,1,@ptrinc	'SP location 8
pulsin c.1,1,@ptrinc	'SP location 9

pulsin c.1,1,@ptrinc   'Humidity Decimal bits not used but must be read  
pulsin c.1,1,@ptrinc   'not used   
pulsin c.1,1,@ptrinc   'not used  
pulsin c.1,1,@ptrinc   'not used  
pulsin c.1,1,@ptrinc   'not used  
pulsin c.1,1,@ptrinc   'not used  
pulsin c.1,1,@ptrinc   'not used  
pulsin c.1,1,@ptrinc   'not used  


pulsin c.1,1,@ptrinc  ' Temperature Integral bits
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc

'pulsin c.1,1,@ptrinc  ' Temperature Decimal bits
'pulsin c.1,1,@ptrinc
'pulsin c.1,1,@ptrinc
'pulsin c.1,1,@ptrinc
'pulsin c.1,1,@ptrinc
'pulsin c.1,1,@ptrinc
'pulsin c.1,1,@ptrinc
'pulsin c.1,1,@ptrinc


setfreq m16               'Reduce Frequency for Serial @19200 
pause 100

humidity = 0              'Clear humidity variable B2
for ptr = 1 to 8          'Read bits 1 - 8 (ignoring bit 0) 
bit_num = 8 - ptr         'Bit# 7 - 0 for Humidity Var B2 

if @ptr > 80 then         
setbit humidity,bit_num   'set b51 bit(x) to 1
endif
if @ptr < 60 then         'set b51 bit(x) to 0
clearbit humidity,bit_num 
endif
next

'Last Humidity reading is now in variable b51


Temperature = 0             'Clear Temperature variable b4
for ptr = 17 to 24          'Read DHT-11 bits 17 - 24 
bit_num = 24 - ptr          'Bit# 7 - 0 for Temperature Var B2 



if @ptr > 80 then            'set b52 bit(x) to 1
setbit temperature,bit_num
endif 
if @ptr < 60 then            'set b52 bit(x) to 0 
clearbit temperature,bit_num
endif
next
'b51=34
'b52=56
'Last Temperature reading is now in variable b52

sertxd ("Temperature:  ",#Temperature,176,"C",cr,lf)
sertxd ("Rel Humidity:  ",#humidity,"%",cr,lf,cr,lf)


'PUT 56, Temperature  - not used
PUT 55, humidity


goto get_data
 

Goeytex

Senior Member
@Skiwi

Hmmm, that looks like a slightly modified version of the code I wrote and posted here back in 9 - 2011. Even the comments and symbols are the same. While it only bugs me a little that you call it your code. Others may not be so nice. It is generally good etiquette to give credit to the actual source of any code that you copy. "Can't remember who" , is a poor excuse.
 

Skiwi

New Member
Goeytex
I apologise for being lazy, I do tend to rush writing as one issue I have with this forum, is that it logs me out real quick if I go away for a few minutes.
But I did refer to your original code in my post yesterday (...used as per the original DHT11 code of Goeytex.)
And my comment today was that my version was an adaptation, meaning adaption of your code. What I did when copying 'my code' into the post, was I clipped some of my notes from the top and bottom, which I actually have references to the origin of the code. Again, I apologise for not giving the credit, I will be more aware in future.
I do complement you on the code, it is a very clever solution.
One thing that is great about this forum is the sharing of ideas, and making complex projects possible.
 
Top