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
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.
下面是链接方式!: