View previous topic :: View next topic |
Author |
Message |
sshahryiar
Joined: 05 May 2010 Posts: 94 Location: Dhaka, Bangladesh
|
DHT11 Relative Humidity and Temperature Sensor |
Posted: Fri Jan 17, 2014 8:28 am |
|
|
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
|
is possible use multiples dht11? |
Posted: Wed Mar 18, 2015 5:54 pm |
|
|
Hi all!
I need use 4 dht11 in one pic, is possble adapt this code for this? |
|
|
sshahryiar
Joined: 05 May 2010 Posts: 94 Location: Dhaka, Bangladesh
|
multiple DHT11 |
Posted: Mon Mar 23, 2015 11:07 pm |
|
|
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
|
Re: DHT11 Relative Humidity and Temperature Sensor |
Posted: Sun Apr 19, 2015 10:32 am |
|
|
Hello i can use R232 in dht11 library? |
|
|
sshahryiar
Joined: 05 May 2010 Posts: 94 Location: Dhaka, Bangladesh
|
RS232 |
Posted: Sun Apr 19, 2015 8:38 pm |
|
|
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: 98 Location: Curtea de Arges, Romania
|
|
Posted: Sun Aug 23, 2015 5:30 pm |
|
|
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: 94 Location: Dhaka, Bangladesh
|
DHT22 instead of DHT11 |
Posted: Sun Aug 23, 2015 8:37 pm |
|
|
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
|
Alarm |
Posted: Wed Sep 30, 2015 3:28 am |
|
|
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
|
Re: DHT11 Relative Humidity and Temperature Sensor |
Posted: Tue Jan 26, 2016 12:00 pm |
|
|
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: 94 Location: Dhaka, Bangladesh
|
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
Re: DHT11 Relative Humidity and Temperature Sensor |
Posted: Sun Jun 19, 2016 12:17 am |
|
|
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: 94 Location: Dhaka, Bangladesh
|
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
Re: DHT11 Simulation |
Posted: Sun Jun 19, 2016 11:47 am |
|
|
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
|
Re: is possible use multiples dht11? |
Posted: Thu Aug 11, 2016 12:08 pm |
|
|
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: 94 Location: Dhaka, Bangladesh
|
Multiple DHT Sensor |
Posted: Sat Aug 13, 2016 10:26 pm |
|
|
Yeah I think you can use multiple DHT sensors using a analog MUX like 4051.... _________________ https://www.facebook.com/MicroArena
SShahryiar |
|
|
|