View previous topic :: View next topic |
Author |
Message |
PICdawg
Joined: 13 Dec 2007 Posts: 17
|
DHT22 code desired (RH and Temp sensor) |
Posted: Mon Oct 21, 2013 8:41 am |
|
|
Anyone have any code they'd like to share for interfacing to a DHT22 sensor?
I have the Great Cow Basic program from Nuts and Volts and the Arduino library stuff regarding this part, but my preference is to use CCS and 16F pics.
I'd rather not spend a single yoctosecond converting something or writing from scratch if one of you kind souls already has something that works! I searched this forum for DHT22(AM2302 also) and DHT11 hits and haven't found anything.
Thanks in advance!...
Kelly |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Oct 22, 2013 6:15 am |
|
|
I don't have that device so I can't code the driver as I have no way of properly testing it in the real world.
You should 'google' that part.I found over 100,000 'hits' so you're NOT the only one using it.Chances are very good that you can 'port' over to CCS C. If not, take the BASIC code you have and translate into CCS C.
It really is not that hard,just be sure to read the datasheet to get proper timings!
It is NOT that hard to do, maybe 1-2 hours and should give you a better appreciation for all the drivers and test programs CCS supplies.Coding the driver will also teach you valuable knowledge you can use for other device,'tweak' other drivers,etc.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Oct 22, 2013 7:19 am |
|
|
Search for SHT11 instead....
The DHT11, is meant to be a direct replacement for the older SHT11. Several threads have run here in the past with code for the older sensor, so it should be 99% done.
Best Wishes |
|
|
PICdawg
Joined: 13 Dec 2007 Posts: 17
|
|
Posted: Tue Oct 22, 2013 8:58 am |
|
|
Thanks for the tips but unfortunately the SHT11 and DHT22 are not related. The SHT11 part is made by Sensiron and uses a synchronous 2 wire interface (clock and data). The DHT22 and its older brother the DHT11 are made by Aosong and use an asynchronous 1 wire bidirectional protocol. They are both Temp and RH sensors and it's probably no coincidence Aosong named their part(s) similar to the Sensiron ones.
In any event it looks like an opportunity for me to post a DHT22 driver to the code library when I get it done. It's not a big deal but when you are an old guy like me, all those yoctoseconds add up! I always like to check if someone else has already blazed the trail. Thanks again for the suggestions. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Oct 22, 2013 9:26 am |
|
|
Hi,
You are correct, the 'SHT11' and the 'DHT11' are totally different sensors, with totally different interfaces, and totally different specs.
Here are a couple of subroutines, and some other code to get you started. Finishing a working program with these bits will only take you a short while!
Here are two routines to initialize the sensor, and to extract data from the sensor:
Code: |
void DHT_Start(void)
{
// Here we send the 'start' sequence to the DHT sensor! We pull the DHT_IO pin low for 25mS, and
// then high for 30mS. The DHT sensor then responds by pulling the DHT_IO pin low, and then pulling it
// back high.
Output_low(DHT_IO);
delay_ms(25);
Output_high(DHT_IO);
delay_us(30);
// Here we wait while the DHT_IO pin remains low.....
bExit = False;
set_timer0(0);
while (!input(DHT_IO) && !bExit);
// Here we wait while the DHT_IO pin remains high.....
bExit = False;
set_timer0(0);
while (input(DHT_IO) && !bExit);
}
int8 DHT_ReadData(void)
{
// This subroutine reads a byte of data (8 bits) from the DHT sensor...
int8 iIndex;
int8 iValue = 0;
for(iIndex = 0; iIndex < 8 ; iIndex++)
{
bExit = False;
set_timer0(0);
while (!input(DHT_IO) && !bExit);
delay_us(30);
if (input(DHT_IO))
{
iValue = iValue |(1<<(7 - iIndex));
bExit = False;
set_timer0(0);
while (input(DHT_IO) && !bExit);
}
}
return iValue;
}
|
Here is the code to use these routines:
Code: |
// Here we send the initialization sequence to "wake up" the DHT sensor, and put it into
// data transmission mode.
DHT_Start();
// Here we read a total of 5 bytes of data (40 bits) from the DHT sensor. For the DHT-11 sensor,
// the integer data is contained in the 1st (Humidity), and 3rd (Temperature) bytes transmitted,
// and the 2nd and 4th decimal bytes are always zero (0). For the DHT-22 sensor, the integer data is
// is contained in the 1st (Humidity), and 3rd (Temperature) bytes transmitted, and the decimal
// data is contained in the 2nd (Humidity), and 4th (Temperature) bytes transmitted. The 5th byte
// contains the CRC byte for both sensor types.
for (iByteIndex = 0; iByteIndex < 5 ; iByteIndex++)
{
DHT_Data[iByteIndex] = DHT_ReadData();
}
// Here we calculate the CRC by adding the 1st four bytes, and looking at only the lower 8 bits.
// This value should match the 5th byte sent by the sensor which is the CRC byte!
CRC = DHT_Data[0] + DHT_Data[1] + DHT_Data[2] + DHT_Data[3];
CRC = CRC & 0xFF;
if (CRC != DHT_Data[4] && !bExit)
fprintf(PC, "CRC Error, expected: %Lu, and received: %u\n\r", CRC, DHT_Data[4]);
if (bSensorType == DHT11)
{
Humidity = DHT_Data[0];
Temperature = DHT_Data[2];
//fprintf(PC, "Humidity: %ld%%\n\r", Humidity);
//fprintf(PC, "Temperature: %ldC\n\r", Temperature);
}
else
{
Humidity = Make16(DHT_Data[0], DHT_Data[1]);
Temperature = Make16(DHT_Data[2] & 0x7F, DHT_Data[3]); //Strip off the sign bit first!
//Temperature /= 10;
if (DHT_Data[2] & 0x80)
Temperature *= -1;
//fprintf(PC, "Humidity: %3.1w%%\n\r", Humidity);
//fprintf(PC, "Temperature: %3.1wC\n\r", Temperature);
}
|
This code was written to work with either the DHT-11 or the DHT-22 sensors. You'll note a flag that selects which calculations to use to
compute temp. and humidity.
I use a timer interrupt to detect when the sensor stops responding, and would otherwise hang the system. I'll leave that bit as an exercise!
Good Luck!
John
Last edited by ezflyr on Tue Nov 12, 2013 3:41 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Oct 22, 2013 11:44 am |
|
|
Interesting. Several people talk of the DHT11 as being a plug in replacement for the SHT11. Shows how careful you have to be with web data.....
However some of the threads seem to be using the one wire interface, so it gets even more confusing.
Anyway ezflyr's code should be a good start.
Best Wishes |
|
|
PICdawg
Joined: 13 Dec 2007 Posts: 17
|
|
Posted: Wed Oct 23, 2013 9:16 am |
|
|
Thanks ezflyr! That's exactly what I was looking for! Should have my proto up in a jiffy. I want to look at the data line with a logic analyzer just for giggles and grins and see the actual DHT response times. And yes, I'll be adding a timeout to change the state of bExit to make sure it doesn't hang!!!
Kelly |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Wed Oct 23, 2013 9:20 am |
|
|
Hi Kelly,
There isn't much to the 'time-out' feature. Here is how I did it:
Code: |
#int_rtcc
void Set_Enable()
{
// This function is called every time Timer0 overflows (255->0). Timer0 is setup
// so that this occurs approx. 15 times per second, or once every 66mS.
if (!bExit)
bExit = True;
}
|
Let me know if you have any problems getting things to work!
John |
|
|
PICdawg
Joined: 13 Dec 2007 Posts: 17
|
|
Posted: Sat Oct 26, 2013 2:55 pm |
|
|
ezflyr (John) ..... Code works great! I'm up and running and reading temp and humidity. I changed the 18ms time to 2ms in the start sequence since I'm only ever going to use the DHT22. Output of DHT22 looks great on a logic analyzer...
Question, why do you set timer 0 to 255 to rollover in one tick? Why not just set it to 0 and it times out in 256 ticks? That's the only thing I didn't understand.
Thanks again for the help.
Kelly |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Oct 28, 2013 11:04 am |
|
|
Hi Kelly,
I'm glad you got the code working successfully! I don't have a (good) explanation for the timer thing. It's been a long time since I looked at that
code, but I did look back at several earlier revisions, and the value is being set to '0', and not '255'. I can only surmise that I was testing something
related to the 'timeout' feature in that later version, and never changed it back? In any event, I should have checked the code more carefully before
posting!
In the next couple of days I'm going to post a complete DHT11/DHT22 working program to the Code Library for others to reference!
Thanks,
John |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Fri Jan 30, 2015 3:24 pm |
|
|
Quote: | In the next couple of days I'm going to post a complete DHT11/DHT22 working program to the Code Library for others to reference! |
Any updates ezflyr?
Thanks for the code snippet! _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Fri Jan 30, 2015 4:33 pm |
|
|
Hi,
Oops!! I finished that code a long time ago, but I guess I neglected to post the 'driver' and test program. I'm traveling now, but I can post it on Monday when I get back to my office!
Thanks,
John |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Nov 08, 2016 1:26 am |
|
|
Have you _looked_ in the code library?..... |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Tue Nov 08, 2016 4:31 am |
|
|
Ttelmah wrote: | Have you _looked_ in the code library?..... |
Yes, I couldn't find ezflyr final driver in the code library.
Other one works, but he has a better approach like detection of what DHT is connected to.
I'm just asking if it could make it official for the community. _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
|