CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

DHT22 code desired (RH and Temp sensor)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PICdawg



Joined: 13 Dec 2007
Posts: 17

View user's profile Send private message

DHT22 code desired (RH and Temp sensor)
PostPosted: Mon Oct 21, 2013 8:41 am     Reply with quote

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: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 6:15 am     Reply with quote

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: 19500

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 7:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 8:58 am     Reply with quote

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! Very Happy 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

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 9:26 am     Reply with quote

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: 19500

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 11:44 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 23, 2013 9:16 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 23, 2013 9:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 26, 2013 2:55 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 28, 2013 11:04 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 30, 2015 3:24 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 30, 2015 4:33 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 07, 2016 8:59 pm     Reply with quote

Still waiting Wink
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Ttelmah



Joined: 11 Mar 2010
Posts: 19500

View user's profile Send private message

PostPosted: Tue Nov 08, 2016 1:26 am     Reply with quote

Have you _looked_ in the code library?.....
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Tue Nov 08, 2016 4:31 am     Reply with quote

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!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group