(SKU:RB-02S014)DHT11溫濕度傳感器

來自ALSROBOT WiKi
2015年5月11日 (一) 21:59Arduino77討論 | 貢獻的版本

跳轉(zhuǎn)至: 導航、 搜索
P-789.jpg

目錄

概述

DHT11數(shù)字溫濕度傳感器是一款含有已校準數(shù)字信號輸出的溫濕度復合傳感器。它應用專用的數(shù)字模塊采集技術(shù)和溫濕度傳感技術(shù),確保產(chǎn)品具有極高的可靠性與卓越的長期穩(wěn)定性。傳感器包括一個電阻式感濕元件和一個NTC測溫元件,并與一個高性能8位單片機相連接。因此該產(chǎn)品具有品質(zhì)卓越、超快響應、抗干擾能力強、性價比極高等優(yōu)點。每個DHT11傳感器都在極為精確的濕度校驗室中進行校準。校準系數(shù)以程序的形式儲存在OTP內(nèi)存中,傳感器內(nèi)部在檢測信號的處理過程中要調(diào)用這些校準系數(shù)。單線制串行接口,使系統(tǒng)集成變得簡易快捷。超小的體積、極低的功耗,信號傳輸距離可達20米以上,使其成為各類應用甚至最為苛刻的應用場合的最佳選則。DHT11數(shù)字溫濕度傳感器模塊為3針PH2.0封裝,連接方便。

規(guī)格參數(shù)

  1. 供電電壓:3V-5.5V
  2. 供電電流:最大2.5mA
  3. 溫度范圍:0-50℃ 誤差±2℃
  4. 濕度范圍:當環(huán)境溫度在 0 ℃時為30~90%RH;當環(huán)境溫度在25℃時為20~90%RH ;當環(huán)境溫度在50℃時為20~80%RH
  5. 響應時間: 1/e(63%) 6-30s
  6. 測量分辨率分別為 8bit(溫度)、8bit(濕度)
  7. 采樣周期間隔不得低于1秒鐘
  8. 模塊尺寸:15mm×34mm

使用方法及例子程序

引腳定義

傳感器引腳的定義是

  • S:輸出信號
  • +:電源(VCC)
  • -:地(GND)
引腳的定義是

連接示意圖

P-33.jpg

示例代碼

使用傳感器連接線將濕度傳感器連接到Arduino傳感器擴展板的模擬口0 上如示意圖所示。然后將代碼編譯后下載到 Arduino里,就可以在串口助手窗口上顯示測得的當前值(注:Arduino串口助手波特率調(diào)到19200 )。
Arduino實驗代碼如下:
#define DHT11_PIN 0      // ADC0 UNO接到模擬口0   mega接PIN37
byte read_dht11_dat()
{
	byte i = 0;
	byte result=0;
	for(i=0; i< 8; i++){
	     while(!(PINC & _BV(DHT11_PIN)));  // wait for 50us
	     delayMicroseconds(30);
	     if(PINC & _BV(DHT11_PIN)) 
	     result |=(1<<(7-i));
             while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
	}
	return result;
}
void setup()
{
	DDRC |= _BV(DHT11_PIN);
	PORTC |= _BV(DHT11_PIN);
	  Serial.begin(19200);
Serial.println("Ready");
	}
	
void loop()
{
	byte dht11_dat[5];
	byte dht11_in;
	byte i;
	// start condition
	// 1. pull-down i/o pin from 18ms
	PORTC &= ~_BV(DHT11_PIN);
	delay(18);
	PORTC |= _BV(DHT11_PIN);
	delayMicroseconds(40);
	DDRC &= ~_BV(DHT11_PIN);
	delayMicroseconds(40);
	dht11_in = PINC & _BV(DHT11_PIN);
	if(dht11_in){
		Serial.println("dht11 start condition 1 not met");
		return;
	}
	delayMicroseconds(80);
	dht11_in = PINC & _BV(DHT11_PIN);
	if(!dht11_in){
		Serial.println("dht11 start condition 2 not met");
		return;
	}
	delayMicroseconds(80);
	// now ready for data reception
	for (i=0; i<5; i++)
		dht11_dat[i] = read_dht11_dat();
	DDRC |= _BV(DHT11_PIN);
	PORTC |= _BV(DHT11_PIN);
  byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
	// check check_sum
	if(dht11_dat[4]!= dht11_check_sum)
	{
		Serial.println("DHT11 checksum error");
	}
	Serial.print("Current humdity = ");
	Serial.print(dht11_dat[0], DEC);
	Serial.print(".");
	Serial.print(dht11_dat[1], DEC);
	Serial.print("%  ");
	Serial.print("temperature = ");
	Serial.print(dht11_dat[2], DEC);
	Serial.print(".");
	Serial.print(dht11_dat[3], DEC);
	Serial.println("C  ");
	delay(2000);
}

程序效果

編譯代碼后下載到Arduino中,打開串口助手即可看見實際測量的溫度與濕度。

應用例程

我們使用Arduino控制器來做個測試,要用到硬件設備如下:

  1. Arduino控制器×1
  2. Arduino 傳感器擴展板×1
  3. DHT11溫濕度傳感器模塊×1
  4. 蜂鳴器發(fā)聲模塊×1和 LED發(fā)光模塊×1
  5. 通用3P傳感器連接線×3
  6. USB數(shù)據(jù)通信線×1

如圖所示,使用傳感器連接線將濕度傳感器連接到Arduino傳感器擴展板的模擬口0上。如需檢測到達某一溫度和濕度時報警可以把本公司的蜂鳴器模塊和LED發(fā)光模塊連接到數(shù)字口上。先把DHT11庫文件解壓縮到你的Arduino安裝目錄下的hardware\libraries里面(如需DHT11庫文件請聯(lián)系我們的技術(shù)客服)。然后將代碼編譯后下載到Arduino里,就可以在串口助手窗口上顯示測得的當前值(注:Arduino串口助手波特率調(diào)到19200)。

Arduino實驗代碼如下:
#define DHT11_PIN 0
int  Led=8;//定義數(shù)字口8為LED燈
int  Buzzer=7;//定義數(shù)字口7為蜂鳴器    
byte read_dht11_dat()
{
	byte i = 0;
	byte result=0;
	for(i=0; i< 8; i++)
{
		while(!(PINC & _BV(DHT11_PIN)));  // wait for 50us
		delayMicroseconds(30);
		if(PINC & _BV(DHT11_PIN)) 
			result |=(1<<(7-i));
          while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
	 }
	return result;
}

void setup()
{
	DDRC |= _BV(DHT11_PIN);
	PORTC |= _BV(DHT11_PIN);
  pinMode(Led,OUTPUT);//定義數(shù)字口Led為輸出模式
  pinMode(Buzzer,OUTPUT); //定義數(shù)字口Buzzer為輸出模式
	Serial.begin(19200);
Serial.println("Ready");
}
	
void loop()
{
	byte dht11_dat[5];
	byte dht11_in;
	byte i;
	// start condition
	// 1. pull-down i/o pin from 18ms
	PORTC &= ~_BV(DHT11_PIN);
	delay(18);
	PORTC |= _BV(DHT11_PIN);
	delayMicroseconds(40);
	DDRC &= ~_BV(DHT11_PIN);
	delayMicroseconds(40);
	dht11_in = PINC & _BV(DHT11_PIN);
	if(dht11_in){
		Serial.println("dht11 start condition 1 not met");
		return;
	}
	delayMicroseconds(80);
	dht11_in = PINC & _BV(DHT11_PIN);
	if(!dht11_in)
{
			Serial.println("dht11 start condition 2 not met");
			return;
	      }
	delayMicroseconds(80);
	// now ready for data reception
	for (i=0; i<5; i++)
	dht11_dat[i] = read_dht11_dat();
	DDRC |= _BV(DHT11_PIN);
	PORTC |= _BV(DHT11_PIN);
   byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
	// check check_sum
	if(dht11_dat[4]!= dht11_check_sum)
	{
		Serial.println("DHT11 checksum error");
	}
	Serial.print("Current humdity = ");
	Serial.print(dht11_dat[0], DEC);
	Serial.print(".");
	Serial.print(dht11_dat[1], DEC);
	Serial.print("%  ");
	Serial.print("temperature = ");
	Serial.print(dht11_dat[2], DEC);
	Serial.print(".");
	Serial.print(dht11_dat[3], DEC);
	Serial.println("C  ");
  if(dht11_dat[0]==25) 判斷濕度是否為25%
digitalWrite(Led,HIGH);//當濕度等于25%時LED燈亮
else
digitalWrite(Led,LOW); //當濕度不等于25%時LED燈滅

if(dht11_dat[0]==28) 判斷溫度是否為28度
digitalWrite(Buzzer,LOW); //當溫度等于28度時蜂鳴器響
else
digitalWrite(Buzzer,HIGH); //當溫度不等于28度時蜂鳴器不響
	delay(2000);
}

此代碼功能是檢測當前環(huán)境下的濕度和溫度值。設定當濕度等于25%時LED燈亮,當溫度等于28度時蜂鳴器響。如下圖所示,串口助手窗口左側(cè)一列顯示為當前濕度值右側(cè)一列為當前溫度值。

文件:12
500px

產(chǎn)品相關(guān)推薦

購買地址:DHT11溫濕度傳感器