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 support@ccsinfo.com

DHT11 Relative Humidity and Temperature Sensor

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
sshahryiar



Joined: 05 May 2010
Posts: 92
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

DHT11 Relative Humidity and Temperature Sensor
PostPosted: Fri Jan 17, 2014 8:28 am     Reply with quote

DHT11.h
Code:

#define DHT11_pin PIN_A0

unsigned char values[5];

void DHT11_init();
unsigned char get_byte();
unsigned char get_data();


DHT11.c
Code:

#include "DHT11.h"

void DHT11_init()
{
   output_float(DHT11_pin);
   delay_ms(1000);
}

unsigned char get_byte()
{
   unsigned char s = 0;
   unsigned char value = 0;

   for(s = 0; s < 8; s += 1)
   {
      value <<= 1;
      while(!input(DHT11_pin));
      delay_us(30);

      if(input(DHT11_pin))
      {
          value |= 1;
      }
      while(input(DHT11_pin));
   }
   return value;
}


unsigned char get_data()
{
   short chk = 0;
   unsigned char s = 0;
   unsigned char check_sum = 0;

   output_high(DHT11_pin);
   output_low(DHT11_pin);
   delay_ms(18);
   output_high(DHT11_pin);
   delay_us(26);
   
   chk = input(DHT11_pin);
   if(chk)
   {
      return 1;
   }
   delay_us(80);
   
   chk = input(DHT11_pin);
   if(!chk)
   {
      return 2;
   }
   delay_us(80);

   for(s = 0; s <= 4; s += 1)
   {
       values[s] = get_byte();
   }
 
   output_high(DHT11_pin);
   
   for(s = 0; s < 4; s += 1)
   {
       check_sum += values[s];
   }
   
   if(check_sum != values[4])
   {
      return 3;
   }
   else
   {
      return 0;
   }
}


Example:

Code:

#include <16F84A.h>
#device *= 16
#fuses NOWDT, PUT, PROTECT, XT
#use delay (clock = 4MHz)

#include <lcd420.c>
#include "DHT11.c"

const unsigned char symbols[8]=
{       
    0x07, 0x05, 0x05, 0x07, 0x00, 0x00, 0x00, 0x00
};

void setup();
void lcd_symbols();

void main()
{
   unsigned char state = 0;
   setup();
   
   while(TRUE)
   {
      state = get_data();
     
      switch(state)
      {
            case 1:
            {
            }
            case 2:
            {
               lcd_putc("\fNo Sensor Found!");
               break;
            }
            case 3:
            {
               lcd_putc("\fChecksum Error!");
               break;
            }
            default:
            {
               lcd_gotoxy(1, 1);
               lcd_putc("Tmp/ C: ");
               lcd_gotoxy(5, 1);
               lcd_putc(0);
               lcd_gotoxy(1, 2);
               lcd_putc("R.H/ %: ");
               lcd_gotoxy(9, 1);
               printf(lcd_putc, "%3u.%03u ", values[2], values[3]);
               lcd_gotoxy(9, 2);
               printf(lcd_putc, "%3u.%03u ", values[0], values[1]);
               break;
            }
      }
      delay_ms(1000);
   };
}


void setup()
{
   disable_interrupts(global);
   port_B_pullups(FALSE);
   setup_timer_0(T0_internal | T0_8_bit);
   set_timer0(0);
   DHT11_init();
   lcd_init();
   lcd_putc("\f");
   lcd_symbols();
}

void lcd_symbols() 
{
   unsigned char i = 0;             

   lcd_send_byte(0, 0x40);

   for(i = 0; i < 8; i++)
   {
        lcd_send_byte(1, symbols[i]);
   }
   lcd_send_byte(0, 0x80);
}

_________________
https://www.facebook.com/MicroArena

SShahryiar
Leandro



Joined: 18 Feb 2014
Posts: 12
Location: Brasil

View user's profile Send private message

is possible use multiples dht11?
PostPosted: Wed Mar 18, 2015 5:54 pm     Reply with quote

Hi all!

I need use 4 dht11 in one pic, is possble adapt this code for this?
sshahryiar



Joined: 05 May 2010
Posts: 92
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

multiple DHT11
PostPosted: Mon Mar 23, 2015 11:07 pm     Reply with quote

You can use a MUX when you need to use multiple DHT11 sensors....
_________________
https://www.facebook.com/MicroArena

SShahryiar
andresgallego0126



Joined: 19 Apr 2015
Posts: 1

View user's profile Send private message

Re: DHT11 Relative Humidity and Temperature Sensor
PostPosted: Sun Apr 19, 2015 10:32 am     Reply with quote

Hello i can use R232 in dht11 library?
sshahryiar



Joined: 05 May 2010
Posts: 92
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

RS232
PostPosted: Sun Apr 19, 2015 8:38 pm     Reply with quote

Quote:
Hello i can use R232 in dht11 library?


RS232 and DHT11 library are not dependent on each other....
_________________
https://www.facebook.com/MicroArena

SShahryiar
wangine



Joined: 07 Jul 2009
Posts: 96
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Sun Aug 23, 2015 5:30 pm     Reply with quote

Hello. Can i use DHT22 instead of DHT11 ? I can't find dht11 in my country, only dht22. Also DHT22 seems to be more accurate. I try to compare datasheets of each but on DHT22 is in chinese. I'm lost here. Thank you.
sshahryiar



Joined: 05 May 2010
Posts: 92
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

DHT22 instead of DHT11
PostPosted: Sun Aug 23, 2015 8:37 pm     Reply with quote

Yeah you can but you'll need to change the values in the delay functions according to DHT22's datasheet.... Study the delays used in this library and compare them with the timings given in DHT11's datasheet.... You can then decide what will be the delays for DHT22....
_________________
https://www.facebook.com/MicroArena

SShahryiar
gianhenrique



Joined: 20 Dec 2010
Posts: 8

View user's profile Send private message Send e-mail MSN Messenger

Alarm
PostPosted: Wed Sep 30, 2015 3:28 am     Reply with quote

Hello guys,

I would like to know, if I want to make a Alarm with some temperature or humidity... How variable can I read for compare with some value, then make a decision?

Have you some examples?

Thanks
exceps



Joined: 26 Jan 2016
Posts: 1
Location: VENEZUELA

View user's profile Send private message Send e-mail

Re: DHT11 Relative Humidity and Temperature Sensor
PostPosted: Tue Jan 26, 2016 12:00 pm     Reply with quote

I used this library but it does not work for me. I was simulating in Proteus
and it tells me it does not find the sensor (sensor not found!) Can you
help me please !!!
sshahryiar



Joined: 05 May 2010
Posts: 92
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

Proteus Sim....
PostPosted: Tue Jan 26, 2016 7:32 pm     Reply with quote

Don't depend on Proteus sim as it sometimes fail terribly.... Test it in real life.... Here's my test video:
https://www.youtube.com/watch?v=uYR1GkMOCGw

By the way where did you get the model for DHT11 in Proteus?
_________________
https://www.facebook.com/MicroArena

SShahryiar
cvargcal



Joined: 17 Feb 2015
Posts: 70

View user's profile Send private message

Re: DHT11 Relative Humidity and Temperature Sensor
PostPosted: Sun Jun 19, 2016 12:17 am     Reply with quote

sshahryiar wrote:
DHT11.h
Code:

#define DHT11_pin PIN_A0

unsigned char values[5];

void DHT11_init();
unsigned char get_byte();
unsigned char get_data();


DHT11.c
Code:

#include "DHT11.h"

void DHT11_init()
{
   output_float(DHT11_pin);
   delay_ms(1000);
}

unsigned char get_byte()
{
   unsigned char s = 0;
   unsigned char value = 0;

   for(s = 0; s < 8; s += 1)
   {
      value <<= 1;
      while(!input(DHT11_pin));
      delay_us(30);

      if(input(DHT11_pin))
      {
          value |= 1;
      }
      while(input(DHT11_pin));
   }
   return value;
}


unsigned char get_data()
{
   short chk = 0;
   unsigned char s = 0;
   unsigned char check_sum = 0;

   output_high(DHT11_pin);
   output_low(DHT11_pin);
   delay_ms(18);
   output_high(DHT11_pin);
   delay_us(26);
   
   chk = input(DHT11_pin);
   if(chk)
   {
      return 1;
   }
   delay_us(80);
   
   chk = input(DHT11_pin);
   if(!chk)
   {
      return 2;
   }
   delay_us(80);

   for(s = 0; s <= 4; s += 1)
   {
       values[s] = get_byte();
   }
 
   output_high(DHT11_pin);
   
   for(s = 0; s < 4; s += 1)
   {
       check_sum += values[s];
   }
   
   if(check_sum != values[4])
   {
      return 3;
   }
   else
   {
      return 0;
   }
}


Example:

Code:

#include <16F84A.h>
#device *= 16
#fuses NOWDT, PUT, PROTECT, XT
#use delay (clock = 4MHz)

#include <lcd420.c>
#include "DHT11.c"

const unsigned char symbols[8]=
{       
    0x07, 0x05, 0x05, 0x07, 0x00, 0x00, 0x00, 0x00
};

void setup();
void lcd_symbols();

void main()
{
   unsigned char state = 0;
   setup();
   
   while(TRUE)
   {
      state = get_data();
     
      switch(state)
      {
            case 1:
            {
            }
            case 2:
            {
               lcd_putc("\fNo Sensor Found!");
               break;
            }
            case 3:
            {
               lcd_putc("\fChecksum Error!");
               break;
            }
            default:
            {
               lcd_gotoxy(1, 1);
               lcd_putc("Tmp/ C: ");
               lcd_gotoxy(5, 1);
               lcd_putc(0);
               lcd_gotoxy(1, 2);
               lcd_putc("R.H/ %: ");
               lcd_gotoxy(9, 1);
               printf(lcd_putc, "%3u.%03u ", values[2], values[3]);
               lcd_gotoxy(9, 2);
               printf(lcd_putc, "%3u.%03u ", values[0], values[1]);
               break;
            }
      }
      delay_ms(1000);
   };
}


void setup()
{
   disable_interrupts(global);
   port_B_pullups(FALSE);
   setup_timer_0(T0_internal | T0_8_bit);
   set_timer0(0);
   DHT11_init();
   lcd_init();
   lcd_putc("\f");
   lcd_symbols();
}

void lcd_symbols() 
{
   unsigned char i = 0;             

   lcd_send_byte(0, 0x40);

   for(i = 0; i < 8; i++)
   {
        lcd_send_byte(1, symbols[i]);
   }
   lcd_send_byte(0, 0x80);
}


I try simulate this but no work.. alway stay in "no sensor found" This if work?
sshahryiar



Joined: 05 May 2010
Posts: 92
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

DHT11 Simulation
PostPosted: Sun Jun 19, 2016 12:23 am     Reply with quote

It won't work in simulation....
_________________
https://www.facebook.com/MicroArena

SShahryiar
cvargcal



Joined: 17 Feb 2015
Posts: 70

View user's profile Send private message

Re: DHT11 Simulation
PostPosted: Sun Jun 19, 2016 11:47 am     Reply with quote

sshahryiar wrote:
It won't work in simulation....

I use this function:
Code:


set_tris_a(0b11111110);  // With A0

void DHT11()
{
     state = get_data();
     
     switch(state)
      {
            case 1:           
            case 2: fprintf(uart1,"No Sensor Found!"); break;
            case 3: fprintf(uart1,"Checksum Error!");  break;
       
            default:
                  fprintf(uart1,"Tmp/ C: %3u.%03u ", values[2], values[3]);
                  //fprintf(uart1,"R.H/ %: %3u.%03u ", values[0], values[1]);
                 break;       
  }
}


But no work good ... show this "Tmp/ C: 253.127" in real not simulate
LuisdelaTorre



Joined: 11 Aug 2016
Posts: 1
Location: Guadalajara, Jalisco

View user's profile Send private message

Re: is possible use multiples dht11?
PostPosted: Thu Aug 11, 2016 12:08 pm     Reply with quote

Leandro wrote:
Hi all!

I need use 4 dht11 in one pic, is possble adapt this code for this?
sshahryiar



Joined: 05 May 2010
Posts: 92
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

Multiple DHT Sensor
PostPosted: Sat Aug 13, 2016 10:26 pm     Reply with quote

Yeah I think you can use multiple DHT sensors using a analog MUX like 4051....
_________________
https://www.facebook.com/MicroArena

SShahryiar
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
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