“(SKU:RB-02S014)DHT11溫濕度傳感器”的版本間的差異

來(lái)自ALSROBOT WiKi
跳轉(zhuǎn)至: 導(dǎo)航、 搜索
?產(chǎn)品相關(guān)推薦
?產(chǎn)品相關(guān)推薦
第200行: 第200行:
 
[[文件:12|500px|縮略圖|居中]]
 
[[文件:12|500px|縮略圖|居中]]
 
==產(chǎn)品相關(guān)推薦==
 
==產(chǎn)品相關(guān)推薦==
?
購(gòu)買(mǎi)地址:[http://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-3667083713.12.MH45fF&id=7701003813 DHT11溫濕度傳感器]
+
購(gòu)買(mǎi)地址:[http://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-3667083713.12.MH45fF&id=7701003813 DHT11溫濕度傳感器]<br/>
 +
技術(shù)社區(qū):[http://www.makerspace.cn/portal.php 哈爾濱奧松機(jī)器人科技有限公司技術(shù)論壇]

2015年5月11日 (一) 22:03的版本

P-789.jpg

目錄

概述

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

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

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

使用方法及例子程序

引腳定義

傳感器引腳的定義是

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

連接示意圖

P-33.jpg

示例代碼

使用傳感器連接線將濕度傳感器連接到Arduino傳感器擴(kuò)展板的模擬口0 上如示意圖所示。然后將代碼編譯后下載到 Arduino里,就可以在串口助手窗口上顯示測(cè)得的當(dāng)前值(注:Arduino串口助手波特率調(diào)到19200 )。
Arduino實(shí)驗(yàn)代碼如下:
#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中,打開(kāi)串口助手即可看見(jiàn)實(shí)際測(cè)量的溫度與濕度。

應(yīng)用例程

我們使用Arduino控制器來(lái)做個(gè)測(cè)試,要用到硬件設(shè)備如下:

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

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

Arduino實(shí)驗(yàn)代碼如下:
#define DHT11_PIN 0
int  Led=8;//定義數(shù)字口8為L(zhǎng)ED燈
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);//當(dāng)濕度等于25%時(shí)LED燈亮
else
digitalWrite(Led,LOW); //當(dāng)濕度不等于25%時(shí)LED燈滅

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

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

文件:12
500px

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

購(gòu)買(mǎi)地址:DHT11溫濕度傳感器
技術(shù)社區(qū):哈爾濱奧松機(jī)器人科技有限公司技術(shù)論壇