---恢復內容開始---
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
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
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.
下面是鏈接方式!: