极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13542|回复: 8

【ocrobot教程】利用dht11温度传感器读取温度并在iic8*8点阵...

[复制链接]
发表于 2015-3-13 15:25:59 | 显示全部楼层 |阅读模式
  1. // 这个例子用来驱动各款DHT温湿度传感器
  2. // Written by ladyada, public domain

  3. #include "DHT.h"
  4. #include <Wire.h>
  5. #include "Adafruit_LEDBackpack.h"
  6. #include "Adafruit_GFX.h"

  7. Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
  8. #define DHTPIN 2     // 定义DHT温湿度传感器连接到的引脚

  9. // Uncomment whatever type you're using!
  10. #define DHTTYPE DHT11   // DHT 11
  11. //#define DHTTYPE DHT22   // DHT 22  (AM2302)
  12. //#define DHTTYPE DHT21   // DHT 21 (AM2301)

  13. // 传感器的1号引脚(在最左边)连接到+5V
  14. // 传感器的2号引脚连接到DHTPIN所定义的引脚
  15. // 传感器的4号引脚(在最右边)连接到GND
  16. // 在传感器的2号引脚(data)和1号引脚(vcc)之间连接一个10K电阻

  17. DHT dht(DHTPIN, DHTTYPE);

  18. void setup() {
  19.   Serial.begin(9600);
  20.   Serial.println("DHTxx test!");
  21. Serial.println("8x8 LED Matrix Test");
  22.   dht.begin();
  23.   matrix.begin(0x70);
  24. }
  25. static const uint8_t PROGMEM
  26. smile_bmp[] =
  27. {
  28.   B00111100,
  29.   B01000010,
  30.   B10100101,
  31.   B10000001,
  32.   B10100101,
  33.   B10011001,
  34.   B01000010,
  35.   B00111100 }
  36. ,
  37. neutral_bmp[] =
  38. {
  39.   B00111100,
  40.   B01000010,
  41.   B10100101,
  42.   B10000001,
  43.   B10111101,
  44.   B10000001,
  45.   B01000010,
  46.   B00111100 }
  47. ,
  48. frown_bmp[] =
  49. {
  50.   B00111100,
  51.   B01000010,
  52.   B10100101,
  53.   B10000001,
  54.   B10011001,
  55.   B10100101,
  56.   B01000010,
  57.   B00111100 };
  58. void loop() {
  59.   // 读取温度或者湿度大约需要250毫秒时间
  60.   // 传感器读取数据也可能超过2秒(它是一个反应速度很慢的传感器)
  61.   float h = dht.readHumidity();
  62.   float t = dht.readTemperature();
  63.   //char
  64.   // 检查返回的数据是否是幼小的,如果是非数字数据,那么就说明出错了。
  65.   if (isnan(t) || isnan(h)) {
  66.     Serial.println("Failed to read from DHT");
  67.   } else {
  68.     Serial.print("Humidity: ");
  69.     Serial.print(h);
  70.     Serial.print(" %\t");
  71.     Serial.print("Temperature: ");
  72.     Serial.print(t);
  73.     Serial.println(" *C");
  74.   }
  75.   matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
  76.   matrix.setTextSize(1);
  77.   matrix.setTextColor(LED_RED);
  78.   matrix.setRotation(3);
  79.   for (int8_t x=7; x>=-72; x--) {
  80.     matrix.clear();
  81.     matrix.setCursor(x,0);
  82.     matrix.print("Temp:");
  83.     matrix.print(t);
  84.     matrix.writeDisplay();
  85.    delay(100);
  86.   }
  87. }
复制代码


回复

使用道具 举报

 楼主| 发表于 2015-3-13 15:27:16 | 显示全部楼层
结合温度传感器和iic的解法就好,图片不知道为啥上传不了,很奇怪
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-13 15:28:50 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-13 15:29:14 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2015-3-13 15:48:23 | 显示全部楼层
很好,支持!不知道你的点阵驱动板是怎样的?如有详细接线图更好。
回复 支持 反对

使用道具 举报

发表于 2015-3-15 19:11:54 | 显示全部楼层
挺好玩的  8X8有点小哦
回复 支持 反对

使用道具 举报

发表于 2015-3-15 19:13:54 | 显示全部楼层
貌似只会显示 温度  湿度没有啊
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-16 09:22:19 | 显示全部楼层
673165401 发表于 2015-3-15 19:13
貌似只会显示 温度  湿度没有啊

这个传感器是温度湿度传感器,我只读取了温度数据,打印到i2c上了,你可以读取湿度打印出来,做个简单的修改就好了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-16 10:44:25 | 显示全部楼层
  1. // 这个例子用来驱动各款DHT温湿度传感器
  2. // Written by ladyada, public domain

  3. #include "DHT.h"
  4. #include <Wire.h>
  5. #include "Adafruit_LEDBackpack.h"
  6. #include "Adafruit_GFX.h"

  7. Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
  8. #define DHTPIN 2     // 定义DHT温湿度传感器连接到的引脚

  9. // Uncomment whatever type you're using!
  10. #define DHTTYPE DHT11   // DHT 11
  11. //#define DHTTYPE DHT22   // DHT 22  (AM2302)
  12. //#define DHTTYPE DHT21   // DHT 21 (AM2301)

  13. // 传感器的1号引脚(在最左边)连接到+5V
  14. // 传感器的2号引脚连接到DHTPIN所定义的引脚
  15. // 传感器的4号引脚(在最右边)连接到GND
  16. // 在传感器的2号引脚(data)和1号引脚(vcc)之间连接一个10K电阻

  17. DHT dht(DHTPIN, DHTTYPE);

  18. void setup() {
  19.   Serial.begin(9600);
  20.   Serial.println("DHTxx test!");
  21. Serial.println("8x8 LED Matrix Test");
  22.   dht.begin();
  23.   matrix.begin(0x70);
  24. }
  25. static const uint8_t PROGMEM
  26. smile_bmp[] =
  27. {
  28.   B00111100,
  29.   B01000010,
  30.   B10100101,
  31.   B10000001,
  32.   B10100101,
  33.   B10011001,
  34.   B01000010,
  35.   B00111100 }
  36. ,
  37. neutral_bmp[] =
  38. {
  39.   B00111100,
  40.   B01000010,
  41.   B10100101,
  42.   B10000001,
  43.   B10111101,
  44.   B10000001,
  45.   B01000010,
  46.   B00111100 }
  47. ,
  48. frown_bmp[] =
  49. {
  50.   B00111100,
  51.   B01000010,
  52.   B10100101,
  53.   B10000001,
  54.   B10011001,
  55.   B10100101,
  56.   B01000010,
  57.   B00111100 };
  58. void loop() {
  59.   // 读取温度或者湿度大约需要250毫秒时间
  60.   // 传感器读取数据也可能超过2秒(它是一个反应速度很慢的传感器)
  61.   float h = dht.readHumidity();
  62.   float t = dht.readTemperature();
  63.   //char
  64.   // 检查返回的数据是否是幼小的,如果是非数字数据,那么就说明出错了。
  65.   if (isnan(t) || isnan(h)) {
  66.     Serial.println("Failed to read from DHT");
  67.   } else {
  68.     Serial.print("Humidity: ");
  69.     Serial.print(h);
  70.     Serial.print(" %\t");
  71.     Serial.print("Temperature: ");
  72.     Serial.print(t);
  73.     Serial.println(" *C");
  74.   }
  75.   matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
  76.   matrix.setTextSize(1);
  77.   matrix.setTextColor(LED_RED);
  78.   matrix.setRotation(3);
  79.   for (int8_t x=7; x>=-118; x--) {
  80.     matrix.clear();
  81.     matrix.setCursor(x,0);
  82.     matrix.print("T:");
  83.     matrix.print(t);
  84.     matrix.print(".C H:");
  85.     matrix.print(h);
  86.     matrix.print(" %  ");
  87.    
  88.     matrix.writeDisplay();
  89.     delay(80);
  90.   }
  91. }
复制代码
这个是完整的显示温度湿度到iic点阵的代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 04:11 , Processed in 0.041351 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表