【原文】http://blog.csdn.net/micaroo/article/details/7239294
这两天开始一一测试之前买过的一些传感器,首先挑选的是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。结果: