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摄氏度左右,很适合人类生存。呵呵~
不过这个东西的精度不是很高。简单的用用还好~
 
有用到一些面向对象的知识不是特别难以置信,哈哈~~
看我写的代码注释,将有助于你更好的了解这个东西工作原理。
posted @ 2013-04-06 16:09  spaceship9  阅读(4072)  评论(0编辑  收藏  举报