Skip to content

Latest commit

 

History

History
230 lines (182 loc) · 4.78 KB

esp8266-dht11-lcd1602.md

File metadata and controls

230 lines (182 loc) · 4.78 KB

ESP8266测量和显示温度/湿度

Fritzing元件图

接线图如下图所示:

NodeMCU-DHT11-LCD1602


ESP8266和Arduino的引脚对应关系表

// for ESP8266
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
#define RX 3 // D9
#define TX 1 // D10

ESP8266+DHT11+LCD1602

#include "DHT.h"
#include <LiquidCrystal.h>

// for Arduino
// #define DHTPIN 8

// for ESP8266
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
#define RX 3 // D9
#define TX 1 // D10

#define DHTPIN RX
#define DHTTYPE DHT11 

DHT dht(DHTPIN, DHTTYPE);

// LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(D0, D1, D2, D3, D4, D5);

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    lcd.setCursor(0, 0);
    lcd.print("Temp=");
    lcd.print(t);
    lcd.write((byte)223);
    lcd.print("C");
    lcd.setCursor(0,1);
    lcd.print("Humidity=");
    lcd.print(h);
    lcd.print("% ");
    Serial.print("Temp=");
    Serial.print(t);
    Serial.println("");
    delay(2000);
  }
}

代码执行效果如下图所示:

ESP8266-DHT11-LCD1602


LCD1602显示℃

LCD1602内置了一个小字库,如下图所示:

LCD1602-CharLib


°这个符号在上图的字库表中,高四位是二进制1101,低四位是二进制1111,用Windows自带的计算器,点左侧中间的BIN(二进制),输入11011111,即可看到对应的十进制是223,如下图所示。因此,在代码的第43行,输出了这个°:

    lcd.write((byte)223);

Calculator


LCD1602-I2C

使用带有I2C模块的LCD1602,可以仅用4根接线即可正确显示。

首先,在Arduino中搜索/安装LiquidCrystal I2C:

Arduino LiquidCrystal I2C


然后,按照下图来连线:

NodeMCU-DHT11-LCD1602_I2C


在Arduino中,运行

#include "DHT.h"
#include <LiquidCrystal_I2C.h>

// for ESP8266
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
#define RX 3 // D9
#define TX 1 // D10

#define DHTPIN RX
#define DHTTYPE DHT11 

DHT dht(DHTPIN, DHTTYPE);

byte findI2CAddress() {
  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  byte i2cAddr = 0;
  
  Wire.begin();
  for (byte i = 8; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      if (i2cAddr == 0) {
        i2cAddr = i;
      }
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
  return i2cAddr;
}

// 初始化LCD
// LCD1602设备地址,这里的地址是0x3F,也可能是0x20,或者0x27;SCL接D1,SDA接D2。
LiquidCrystal_I2C lcd(findI2CAddress(), D1, D2);

void setup() {
  Serial.begin(9600);
  delay(1000);
  
  lcd.begin(16, 2);
  lcd.backlight();
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
    lcd.setCursor(0, 0);
    lcd.print("Failed!");
  } else {
    lcd.setCursor(0, 0);
    lcd.print("Temp=");
    lcd.print(t);
    lcd.write((byte)223);
    lcd.print("C");
    lcd.setCursor(0,1);
    lcd.print("Humidity=");
    lcd.print(h);
    lcd.print("% ");
    Serial.print("Temp=");
    Serial.print(t);
    Serial.println("");
  }
  delay(2000);
}

执行上述代码,液晶上能够正确显示温度和湿度。


参考资料