iam郎 发表于 2015-3-13 15:25:59

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

// 这个例子用来驱动各款DHT温湿度传感器
// Written by ladyada, public domain

#include "DHT.h"
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

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

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

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

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
Serial.println("8x8 LED Matrix Test");
dht.begin();
matrix.begin(0x70);
}
static const uint8_t PROGMEM
smile_bmp[] =
{
B00111100,
B01000010,
B10100101,
B10000001,
B10100101,
B10011001,
B01000010,
B00111100 }
,
neutral_bmp[] =
{
B00111100,
B01000010,
B10100101,
B10000001,
B10111101,
B10000001,
B01000010,
B00111100 }
,
frown_bmp[] =
{
B00111100,
B01000010,
B10100101,
B10000001,
B10011001,
B10100101,
B01000010,
B00111100 };
void loop() {
// 读取温度或者湿度大约需要250毫秒时间
// 传感器读取数据也可能超过2秒(它是一个反应速度很慢的传感器)
float h = dht.readHumidity();
float t = dht.readTemperature();
//char
// 检查返回的数据是否是幼小的,如果是非数字数据,那么就说明出错了。
if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
} else {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
}
matrix.setTextWrap(false);// we dont want text to wrap so it scrolls nicely
matrix.setTextSize(1);
matrix.setTextColor(LED_RED);
matrix.setRotation(3);
for (int8_t x=7; x>=-72; x--) {
    matrix.clear();
    matrix.setCursor(x,0);
    matrix.print("Temp:");
    matrix.print(t);
    matrix.writeDisplay();
   delay(100);
}
}


iam郎 发表于 2015-3-13 15:27:16

结合温度传感器和iic的解法就好,图片不知道为啥上传不了,很奇怪

iam郎 发表于 2015-3-13 15:28:50


iam郎 发表于 2015-3-13 15:29:14

ITEAD创易工作室 发表于 2015-3-13 15:48:23

很好,支持!不知道你的点阵驱动板是怎样的?如有详细接线图更好。:D

673165401 发表于 2015-3-15 19:11:54

:D挺好玩的8X8有点小哦

673165401 发表于 2015-3-15 19:13:54

貌似只会显示 温度湿度没有啊

iam郎 发表于 2015-3-16 09:22:19

673165401 发表于 2015-3-15 19:13 static/image/common/back.gif
貌似只会显示 温度湿度没有啊

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

iam郎 发表于 2015-3-16 10:44:25

// 这个例子用来驱动各款DHT温湿度传感器
// Written by ladyada, public domain

#include "DHT.h"
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

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

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

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

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
Serial.println("8x8 LED Matrix Test");
dht.begin();
matrix.begin(0x70);
}
static const uint8_t PROGMEM
smile_bmp[] =
{
B00111100,
B01000010,
B10100101,
B10000001,
B10100101,
B10011001,
B01000010,
B00111100 }
,
neutral_bmp[] =
{
B00111100,
B01000010,
B10100101,
B10000001,
B10111101,
B10000001,
B01000010,
B00111100 }
,
frown_bmp[] =
{
B00111100,
B01000010,
B10100101,
B10000001,
B10011001,
B10100101,
B01000010,
B00111100 };
void loop() {
// 读取温度或者湿度大约需要250毫秒时间
// 传感器读取数据也可能超过2秒(它是一个反应速度很慢的传感器)
float h = dht.readHumidity();
float t = dht.readTemperature();
//char
// 检查返回的数据是否是幼小的,如果是非数字数据,那么就说明出错了。
if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
} else {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
}
matrix.setTextWrap(false);// we dont want text to wrap so it scrolls nicely
matrix.setTextSize(1);
matrix.setTextColor(LED_RED);
matrix.setRotation(3);
for (int8_t x=7; x>=-118; x--) {
    matrix.clear();
    matrix.setCursor(x,0);
    matrix.print("T:");
    matrix.print(t);
    matrix.print(".C H:");
    matrix.print(h);
    matrix.print(" %");
   
    matrix.writeDisplay();
    delay(80);
}
}
这个是完整的显示温度湿度到iic点阵的代码
页: [1]
查看完整版本: 【ocrobot教程】利用dht11温度传感器读取温度并在iic8*8点阵...