极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 23155|回复: 8

I2C Lcd1602 DHT11温湿度显示

[复制链接]
发表于 2014-9-10 22:43:55 | 显示全部楼层 |阅读模式
材料:I2C LCD 1602
          DHT11
          ARDUINO UNO

ARDUINO 代码
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,16,2);

int DHpin;
byte dat[5];
byte read_data()
{
  byte data;
   for(int i=0;i<8;i++)
     {
        if(digitalRead(8)==LOW)
         {
           while(digitalRead(8)==LOW);
           delayMicroseconds(30);
           if(digitalRead(8)==HIGH)
           data|=(1<<(7-i));
           while(digitalRead(8)==HIGH);
         }
     }
     return data;
}
void start_test()
{
  digitalWrite(8,LOW);
  delay(30);
  digitalWrite(8,HIGH);
  delayMicroseconds(40);
       pinMode(8,INPUT);
       while(digitalRead(8)==HIGH);
       delayMicroseconds(80);
       if(digitalRead(8)==LOW);
       delayMicroseconds(80);
       for(int i=0;i<4;i++)
       dat[i]=read_data();
       pinMode(8,OUTPUT);
       digitalWrite(8,HIGH);
}
void setup()
{
  lcd.init();   // initialize the lcd
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  pinMode(8,OUTPUT);
}
void loop()
{
  start_test();
  lcd.setCursor(0,0);
  lcd.print("Cur hum =");
  lcd.print(dat[0],DEC);
  lcd.print('.');
  lcd.print(dat[1],DEC);
  lcd.print('%');
  lcd.setCursor(0,1);
  lcd.print("Cur tem=");
  lcd.print(dat[2],DEC);
  lcd.print('.');
  lcd.print(dat[3],DEC);
  lcd.print('C');
  delay(5000);
  lcd.clear();
  delay(1000);
}
回复

使用道具 举报

发表于 2014-11-6 17:35:44 | 显示全部楼层
怎么接线呢
回复 支持 反对

使用道具 举报

发表于 2014-11-6 17:36:23 | 显示全部楼层
DHT11接哪个端口
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-10 22:47:12 | 显示全部楼层
lailai518 发表于 2014-11-6 17:36
DHT11接哪个端口

DHT11接 针脚8
回复 支持 反对

使用道具 举报

发表于 2015-4-3 23:05:07 | 显示全部楼层
感谢楼主,有解释就好了
回复 支持 反对

使用道具 举报

发表于 2015-4-5 17:24:50 | 显示全部楼层
我的孤独 发表于 2015-4-3 23:05
感谢楼主,有解释就好了

除非要研究 Arduino 如何和 DHT11 通信,
否则新手建议用 DHT11 的库即可 :
  (1) http://playground.arduino.cc/Main/DHTLib
  (2) https://github.com/kkpoon/DHT11Lib
  (3) https://github.com/adafruit/DHT-sensor-library
回复 支持 反对

使用道具 举报

发表于 2015-4-5 18:33:08 | 显示全部楼层
tsaiwn 发表于 2015-4-5 17:24
除非要研究 Arduino 如何和 DHT11 通信,
否则新手建议用 DHT11 的库即可 :
  (1) http://playground.ar ...

全是英文。。。,不过还是谢谢楼主
回复 支持 反对

使用道具 举报

发表于 2015-4-6 00:41:38 | 显示全部楼层
我的孤独 发表于 2015-4-5 18:33
全是英文。。。,不过还是谢谢楼主

这是官網的 dht11.h
但官網沒做成 .zip , 我做成 .zip 夾在後面
这是 testing Example:
  1. // Example sketch for testing  DHT11 humidity/temperature sensors
  2. // Written by [email protected]
  3. // this program is in public domain.

  4. #include "dht11.h"

  5. const int pin = 5;    // the pin attached  DHT11 sensorDHT

  6. dht11 DHT11;   // the Object for sensor DHT11
  7. int temp; // 温度, 因為 DHT11 只有給 int !
  8. int humi; // 湿度, 因為 DHT11 只有給 int !

  9. void setup() {
  10.   Serial.begin(9600);
  11.   Serial.println("Testing the DHT11 and dht11.h Library !");
  12.   int chk = DHT11.read(pin);
  13.   Serial.print(String("Error code=") + chk);
  14.   Serial.println(String("Humidity=") + DHT11.humidity + ", Temperature=" + DHT11.temperature);
  15. } // setup(

  16. void loop() {
  17.   delay(2000);   // Wait 2  seconds between measurements.
  18.   int chk = DHT11.read(pin);
  19.   if(chk != 0 ) {   // 讀取 DHT11 有 Error
  20.      Serial.println("DHT11 reading Error !");
  21.   }else{     // 把这行注釋掉就可以有錯也照显示 !
  22.      temp = DHT11.temperature;
  23.      humi = DHT11.humidity;
  24.      Serial.print("Humidity: ");
  25.      Serial.print(humi);
  26.      Serial.print(" %\t");
  27.      Serial.println(String("Temperature: ") + temp +" oC");
  28.   }// if..else..
  29. } // loop(
复制代码

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

发表于 2015-4-6 20:52:49 | 显示全部楼层
tsaiwn 发表于 2015-4-6 00:41
这是官網的 dht11.h
但官網沒做成 .zip , 我做成 .zip 夾在後面
这是 testing Example:

哇,谢谢啦
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 22:44 , Processed in 0.037227 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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