zoukankan      html  css  js  c++  java
  • Arduino从DHT11读取温湿度数据并显示在1602LCD

    硬件清单

    Arduino NANO
    1602LCD + PCF8574T模块
    YL-47 DHT11模块

    连线

    1. 连接LCD: PCF8574T模块4pin(Gnd, Vcc, SDA i2c数据, SCL i2c时钟) 连接至Arduino接口 Gnd -> Gnd, Vcc -> Vcc, SDA -> A4, SDL -> A5
    2. 连接YL-47 DHT11: Gnd -> Gnd, Vcc -> Vcc, Data-> D4

    Library

    除了1602需要的库以外, 需要安装两个自带的库:  DHT Sensor Library by Adafruit, Adafruit Unified Sensor

    测试代码

    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    #include <DHT.h>
    
    #define DHTPIN 4
    #define DHTTYPE DHT11
    
    // I2C地址, 一般为0x3F, 0x20或0x27
    LiquidCrystal_I2C lcd(0x27,16,2);
    // 初始化DHT
    DHT dht(DHTPIN, DHTTYPE);
    
    void setup() {
      lcd.init();
      lcd.backlight(); // 打开背光
      Serial.begin(9600);
      dht.begin();
      lcd.setCursor(0,0); // line 0, pos 0
      lcd.print("Good Day!");
      lcd.setCursor(0,1); // line 1, pos 0
      lcd.print("H:     % T:");
      delay(1000);
    }
    
    void loop() {
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      // Read temperature as Celsius (the default)
      float t = dht.readTemperature();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f = dht.readTemperature(true);
    
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
      }
      // Compute heat index in Fahrenheit (the default)
      float hif = dht.computeHeatIndex(f, h);
      // Compute heat index in Celsius (isFahreheit = false)
      float hic = dht.computeHeatIndex(t, h, false);
    
      Serial.print("Humidity: ");
      Serial.print(h);
      Serial.print(" %	");
      Serial.print("Temperature: ");
      Serial.print(t);
      Serial.print(" *C ");
      Serial.print(f);
      Serial.print(" *F	");
      Serial.print("Heat index: ");
      Serial.print(hic);
      Serial.print(" *C ");
      Serial.print(hif);
      Serial.println(" *F");
    
      lcd.setCursor(2,1); // line 1, pos 0
      lcd.print(h);
      lcd.setCursor(11,1); // line 1, pos 0
      lcd.print(t);
    
      delay(1000);
    }

    代码说明

    1. DHT11启动到读取数据需要等待1~2秒
    2. 温湿度的精度都为1, 没有小数部分
    3. DHT库里面带了计算热指数的方法 computeHeatIndex(), 用于生成综合温湿度计算得到的热指数值

    改进拼接字符串

    改进后的代码, 注意: arduino里的sprintf只能格式化整数, 不能格式化浮点

    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    #include <DHT.h>
    #include <DS3231.h>
    
    #define DHTPIN 4
    #define DHTTYPE DHT11
    
    // I2C地址, 一般为0x3F, 0x20或0x27
    LiquidCrystal_I2C lcd(0x27,16,2);
    DHT dht(DHTPIN, DHTTYPE);
    DS3231 Clock;
    bool century=false;
    bool h12;
    bool PM;
    
    void setup() {
      lcd.init();
      //lcd.backlight(); // 打开背光
      Serial.begin(9600);
      dht.begin();
      lcd.setCursor(0,0); // line 0, pos 0
      lcd.print("Good Day Jessie~~");
      lcd.setCursor(0,1); // line 1, pos 0
      lcd.print("H:  % T:   T:");
      delay(1000);
    }
    
    void loop() {
      char str[17];
      sprintf(
        str,
        "%02d-%02d %02d:%02d:%02d  ",
        Clock.getMonth(century),
        Clock.getDate(),
        Clock.getHour(h12, PM),
        Clock.getMinute(),
        Clock.getSecond());
    
      lcd.setCursor(0,0); // line 0, pos 0
      lcd.print(str);
      
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      // Read temperature as Celsius (the default)
      float t = dht.readTemperature();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f = dht.readTemperature(true);
    
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
      }
      // Compute heat index in Fahrenheit (the default)
      float hif = dht.computeHeatIndex(f, h);
      // Compute heat index in Celsius (isFahreheit = false)
      float hic = dht.computeHeatIndex(t, h, false);
    
      Serial.print("Humidity: ");
      Serial.print(h);
      Serial.print(" %	");
      Serial.print("Temperature: ");
      Serial.print(t);
      Serial.print(" *C ");
      Serial.print(f);
      Serial.print(" *F	");
      Serial.print("Heat index: ");
      Serial.print(hic);
      Serial.print(" *C ");
      Serial.print(hif);
      Serial.println(" *F");
    
      lcd.setCursor(2,1); // line 1, pos 0
      lcd.print((int)h);
      lcd.setCursor(8,1); // line 1, pos 0
      lcd.print((int)t);
      lcd.setCursor(13,1);
      lcd.print((int)(Clock.getTemperature()*10));
    
      delay(1000);
    }
  • 相关阅读:
    微软面试题: LeetCode 907. 子数组的最小值之和 middle 出现次数:1
    微软面试题: LeetCode 5. 最长回文子串 出现次数:1
    微软面试题: LeetCode 120. 三角形最小路径和 出现次数:1
    开源项目推荐:主流RPC开源框架及知识科普
    微软面试题: LeetCode 84. 柱状图中最大的矩形 出现次数:1
    需重点掌握的三大排序: 快速排序 + 归并排序 + 堆排序
    微软面试题:补充题12. 二叉树的下一个节点 出现次数:2
    微软面试题: 剑指 Offer 51. 数组中的逆序对 出现次数:2
    GUI
    数据结构与算法
  • 原文地址:https://www.cnblogs.com/milton/p/8673812.html
Copyright © 2011-2022 走看看