Arduino 各種模塊篇 DHT11 溫度濕度 數字模塊 單總線


---恢復內容開始---

DHT11

以前用的是DS18B20也是但總線的,而且每個DS18B20都有一個特定的ROM,所以將許多單總線的溫度計放在一根線上也行。

真是一個非常好的設計方案!

 

如果這里我們要用到單總線的話,還加上溫度,濕度。

在arduino上,我們可以直接使用單總線庫,將非常簡單。

想想以前c51單片機上,連最基本的延時程序都要自己寫,還要寫PWM,等等等等,AVR系列的單片機真是讓想法-到實現快了好多步。

在現在單片機成本越來越便宜的情況下,直接使用AVR這種單片機可以說會成為非常好的選擇方案。

 

現在又加上Arduino簡單的庫,更是更加容易了許多。

好了,現在開始。

————————————————————分割線——————————————————————————————

出廠數據:

實物如圖,PCB尺寸:32*14mm

兩種焊接方式,字朝下更實在,對濕度感應更准確。字朝上好看一點而已

————————————————————分割線——————————————————————————————

有車輪,就不用再重新發明輪子了。

效率比較重要。

以下內容源自:http://blog.csdn.net/micaroo/article/details/7239294

作者micaroo

這兩天開始一一測試之前買過的一些傳感器,首先挑選的是DHT11,這個傳感器用於粗略估計溫濕度。

硬件連接很簡單,只需要將DHT11傳感器和數字針腳4相連,這里我用到了傳感器擴展板,直接連在擴展板上。材料都是用的奧松機器人基地的。

第一件麻煩事兒就是DHT11的庫文件,中文材料是木有滴,我到了官網,終於把一個可以用的庫文件找出來了。這個庫文件還可以測DHT22。如下兩個文件,放在DHT文件夾中,然后放到ardunio的庫文件夾。

dht.cpp

  1 //
  2 //    FILE: dht.cpp
  3 // VERSION: 0.1.01
  4 // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
  5 //
  6 // DATASHEET: 
  7 //
  8 // HISTORY:
  9 // 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)
 10 // 0.1.0 by Rob Tillaart (01/04/2011)
 11 // inspired by DHT11 library
 12 //
 13 
 14 #include "dht.h"
 15 
 16 #define TIMEOUT 10000
 17 
 18 /////////////////////////////////////////////////////
 19 //
 20 // PUBLIC
 21 //
 22 
 23 
 24 // return values:
 25 //  0 : OK
 26 // -1 : checksum error
 27 // -2 : timeout
 28 int dht::read11(uint8_t pin)
 29 {
 30     // READ VALUES
 31     int rv = read(pin);
 32     if (rv != 0) return rv;
 33 
 34     // CONVERT AND STORE
 35     humidity    = bits[0];  // bit[1] == 0;
 36     temperature = bits[2];  // bits[3] == 0;
 37 
 38     // TEST CHECKSUM
 39     uint8_t sum = bits[0] + bits[2]; // bits[1] && bits[3] both 0
 40     if (bits[4] != sum) return -1;
 41 
 42     return 0;
 43 }
 44 
 45 // return values:
 46 //  0 : OK
 47 // -1 : checksum error
 48 // -2 : timeout
 49 int dht::read22(uint8_t pin)
 50 {
 51     // READ VALUES
 52     int rv = read(pin);
 53     if (rv != 0) return rv;
 54 
 55     // CONVERT AND STORE
 56     humidity    = word(bits[0], bits[1]) * 0.1;
 57 
 58     int sign = 1;
 59     if (bits[2] & 0x80) // negative temperature
 60     {
 61         bits[2] = bits[2] & 0x7F;
 62         sign = -1;
 63     }
 64     temperature = sign * word(bits[2], bits[3]) * 0.1;
 65 
 66 
 67     // TEST CHECKSUM
 68     uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
 69     if (bits[4] != sum) return -1;
 70 
 71     return 0;
 72 }
 73 
 74 /////////////////////////////////////////////////////
 75 //
 76 // PRIVATE
 77 //
 78 
 79 // return values:
 80 //  0 : OK
 81 // -2 : timeout
 82 int dht::read(uint8_t pin)
 83 {
 84     // INIT BUFFERVAR TO RECEIVE DATA
 85     uint8_t cnt = 7;
 86     uint8_t idx = 0;
 87 
 88     // EMPTY BUFFER
 89     for (int i=0; i< 5; i++) bits[i] = 0;
 90 
 91     // REQUEST SAMPLE
 92     pinMode(pin, OUTPUT);
 93     digitalWrite(pin, LOW);
 94     delay(20);
 95     digitalWrite(pin, HIGH);
 96     delayMicroseconds(40);
 97     pinMode(pin, INPUT);
 98 
 99     // GET ACKNOWLEDGE or TIMEOUT
100     unsigned int loopCnt = TIMEOUT;
101     while(digitalRead(pin) == LOW)
102         if (loopCnt-- == 0) return -2;
103 
104     loopCnt = TIMEOUT;
105     while(digitalRead(pin) == HIGH)
106         if (loopCnt-- == 0) return -2;
107 
108     // READ THE OUTPUT - 40 BITS => 5 BYTES
109     for (int i=0; i<40; i++)
110     {
111         loopCnt = TIMEOUT;
112         while(digitalRead(pin) == LOW)
113             if (loopCnt-- == 0) return -2;
114 
115         unsigned long t = micros();
116 
117         loopCnt = TIMEOUT;
118         while(digitalRead(pin) == HIGH)
119             if (loopCnt-- == 0) return -2;
120 
121         if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
122         if (cnt == 0)   // next byte?
123         {
124             cnt = 7;   
125             idx++;      
126         }
127         else cnt--;
128     }
129 
130     return 0;
131 }
132 //
133 // END OF FILE
134 //

 

dht.h

// 
//    FILE: dht.h
// VERSION: 0.1.01
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//
//     URL: http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
// see dht.cpp file
// 

#ifndef dht_h
#define dht_h

#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif

#define DHT_LIB_VERSION "0.1.01"

class dht
{
public:
    int read11(uint8_t pin);
    int read22(uint8_t pin);
    double humidity;
    double temperature;

private:
    uint8_t bits[5];  // buffer to receive data
    int read(uint8_t pin);
};
#endif
//
// END OF FILE
//

 

庫文件搞定之后,可以開始寫ardunio程序了。這里因為只有DHT11,所以程序就不去測試22了。引入dht的庫,然后編寫如下代碼:

// 
//   FILE:  dht_test.pde
// PURPOSE: DHT library test sketch for Arduino
//

#include <dht.h>

dht DHT;
//這里是用class類dht 創建一個名為DHT的對象(實例)。其繼承含有dht的屬性與方法(面向對象開發,程序都是這么回事)上面的代碼有class dht{}
#define DHT11_PIN 4//put the sensor in the digital pin 4 void setup() { Serial.begin(9600); Serial.println("DHT TEST PROGRAM "); Serial.print("LIBRARY VERSION: "); Serial.println(DHT_LIB_VERSION); Serial.println(); Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)"); } void loop() { // READ DATA Serial.print("DHT11, \t"); int chk = DHT.read11(DHT11_PIN);    //這里是利用庫里面的一個函數read11這個函數。意思是每次輸出溫度、濕度的時候,都要運行。 switch (chk) { case 0: Serial.print("OK,\t"); break;      //如果能讀到數據,那么就輸出OK,加一個tab縮進 case -1: Serial.print("Checksum error,\t"); break;  //下面這幾個是解答錯誤的 case -2: Serial.print("Time out error,\t"); break; default: Serial.print("Unknown error,\t"); break;    //default是啥也沒有的時候,輸出Unknown error C語言都一個樣。簡單明了 } // DISPLAT DATA Serial.print(DHT.humidity,1); //利用創建的實例的方法,輸出,這里1代表輸出1位精確小數,默認不寫為2位 Serial.print(",\t");      //輸出一個tab 縮進 Serial.println(DHT.temperature,1);  //輸出對象的溫度 並換行 delay(1000); // 一秒一刷新新的溫度,適度 } // // END OF FILE //

 

 

如果在控制台,出現了time out error,那么就是沒讀到數據,可能是引腳接錯了。記得,我現在接的是數字引腳4。結果:

——————————————————————————————————————————————————————

---恢復內容結束---

 這里有Arduino Playground的資料,關於這個庫的。http://playground.arduino.cc/Main/DHT11Lib

這里面有DHT 11 成形的庫文檔。

ladyada的關於這個的學習資料:http://learn.adafruit.com/dht tutorial 超級推薦

還有這個模塊的pdf datasheet  http://www.micro4you.com/files/sensor/DHT11.pdf 

——————

ladyada 上教怎么使用這個DHT傳感器的,

DHT11 DHT22 區別不是很大,可以看這里看區別。http://learn.adafruit.com/dht

Luckily it is trivial to connect to these sensors, they have fairly long 0.1"-pitch pins so you can plug them into any breadboard, perfboard or similar.
dhtbreadboard.jpg

Likewise, it is fairly easy to connect up to the DHT sensors. They have four pins

  • VCC (3 to 5V power)
  • Data out
  • Not connected
  • Ground

Simply ignore pin 3, its not used. You will want to place a 10K resistor between VCC and the data pin, to act as a medium-strength pull up on the data line. The Arduino has built in pullups you can turn on but they're very weak, about 100K

需要在數據接口上連接一個上拉電阻。大約10k,一般是4.7k。

關於上拉電阻,下拉電阻的內容:http://www.dianyuan.com/article/213934

 

This diagram shows how we will connect for the testing sketch. Connect data to pin 2, you can change it later to any pin.

下面是鏈接方式!:

dhtwiring.gif
 
 
載入上面的代碼后,
一開始的空氣濕度為38%左右,向DHT11呵氣后,濕度變大,到40%多,如圖:
現在等待2分鍾后,我室內的濕度又回到41%。溫度在20攝氏度左右,很適合人類生存。呵呵~
不過這個東西的精度不是很高。簡單的用用還好~
 
有用到一些面向對象的知識不是特別難以置信,哈哈~~
看我寫的代碼注釋,將有助於你更好的了解這個東西工作原理。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM