這兩天開始一一測試之前買過的一些傳感器,首先挑選的是DHT11,這個傳感器用於粗略估計溫溼度。
硬件連接很簡單,只需要將DHT11傳感器和數字針腳4相連,這裏我用到了傳感器擴展板,直接連在擴展板上。材料都是用的奧鬆機器人基地的。
第一件麻煩事兒就是DHT11的庫文件,中文材料是木有滴,我到了官網,終於把一個可以用的庫文件找出來了。這個庫文件還可以測DHT22。如下兩個文件,放在DHT文件夾中,然後放到ardunio的庫文件夾。
dht.cpp
- //
- // FILE: dht.cpp
- // VERSION: 0.1.01
- // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
- //
- // DATASHEET:
- //
- // HISTORY:
- // 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)
- // 0.1.0 by Rob Tillaart (01/04/2011)
- // inspired by DHT11 library
- //
- #include "dht.h"
- #define TIMEOUT 10000
- /////////////////////////////////////////////////////
- //
- // PUBLIC
- //
- // return values:
- // 0 : OK
- // -1 : checksum error
- // -2 : timeout
- int dht::read11(uint8_t pin)
- {
- // READ VALUES
- int rv = read(pin);
- if (rv != 0) return rv;
- // CONVERT AND STORE
- humidity = bits[0]; // bit[1] == 0;
- temperature = bits[2]; // bits[3] == 0;
- // TEST CHECKSUM
- uint8_t sum = bits[0] + bits[2]; // bits[1] && bits[3] both 0
- if (bits[4] != sum) return -1;
- return 0;
- }
- // return values:
- // 0 : OK
- // -1 : checksum error
- // -2 : timeout
- int dht::read22(uint8_t pin)
- {
- // READ VALUES
- int rv = read(pin);
- if (rv != 0) return rv;
- // CONVERT AND STORE
- humidity = word(bits[0], bits[1]) * 0.1;
- int sign = 1;
- if (bits[2] & 0x80) // negative temperature
- {
- bits[2] = bits[2] & 0x7F;
- sign = -1;
- }
- temperature = sign * word(bits[2], bits[3]) * 0.1;
- // TEST CHECKSUM
- uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
- if (bits[4] != sum) return -1;
- return 0;
- }
- /////////////////////////////////////////////////////
- //
- // PRIVATE
- //
- // return values:
- // 0 : OK
- // -2 : timeout
- int dht::read(uint8_t pin)
- {
- // INIT BUFFERVAR TO RECEIVE DATA
- uint8_t cnt = 7;
- uint8_t idx = 0;
- // EMPTY BUFFER
- for (int i=0; i< 5; i++) bits[i] = 0;
- // REQUEST SAMPLE
- pinMode(pin, OUTPUT);
- digitalWrite(pin, LOW);
- delay(20);
- digitalWrite(pin, HIGH);
- delayMicroseconds(40);
- pinMode(pin, INPUT);
- // GET ACKNOWLEDGE or TIMEOUT
- unsigned int loopCnt = TIMEOUT;
- while(digitalRead(pin) == LOW)
- if (loopCnt-- == 0) return -2;
- loopCnt = TIMEOUT;
- while(digitalRead(pin) == HIGH)
- if (loopCnt-- == 0) return -2;
- // READ THE OUTPUT - 40 BITS => 5 BYTES
- for (int i=0; i<40; i++)
- {
- loopCnt = TIMEOUT;
- while(digitalRead(pin) == LOW)
- if (loopCnt-- == 0) return -2;
- unsigned long t = micros();
- loopCnt = TIMEOUT;
- while(digitalRead(pin) == HIGH)
- if (loopCnt-- == 0) return -2;
- if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
- if (cnt == 0) // next byte?
- {
- cnt = 7;
- idx++;
- }
- else cnt--;
- }
- return 0;
- }
- //
- // END OF FILE
- //
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;
- #define DHT11_PIN 4//put the sensor in the digital pin 4
- void setup()
- {
- Serial.begin(115200);
- 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);
- switch (chk)
- {
- case 0: Serial.print("OK,\t"); break;
- case -1: Serial.print("Checksum error,\t"); break;
- case -2: Serial.print("Time out error,\t"); break;
- default: Serial.print("Unknown error,\t"); break;
- }
- // DISPLAT DATA
- Serial.print(DHT.humidity,1);
- Serial.print(",\t");
- Serial.println(DHT.temperature,1);
- delay(1000);
- }
- //
- // END OF FILE
- //
如果在控制檯,出現了time out error,那麼就是沒讀到數據,可能是引腳接錯了。記得,我現在接的是數字引腳4。結果: