Arduino - LCD1602的I2C通信显示

Arduino通过I2C(PCF8574T)驱动1602LCD

使用带Arduino的LCD显示器

视频:Using LCD Displays with Arduino

使用16位端口扩展器通过I2C / SPI连接图形LCD

I2C - 双线外设接口 - 用于Arduino

介绍

自20世纪70年代末以来,液晶显示器或LCD已经用于电子设备中。LCD显示器具有消耗极少电流的优势,是您Arduino项目的理想选择。

PCF8574T模块

PCF8574T模块4pin(Gnd, Vcc, SDA i2c数据, SCL i2c时钟)和Arduino接口的对应关系: Gnd -> Gnd, Vcc -> Vcc, SDA -> A4, SDL -> A5
在这里插入图片描述

LCD1602引脚布局

在这里插入图片描述

GND - 这是接地引脚。在某些模块上,它标记为VSS。
5 VDC- 这是5伏电源连接。在某些模块上,它标记为VDD。
Bright- 这是亮度控制电压的输入,在0到5伏之间变化以控制显示亮度。在某些模块上,此引脚标记为V0。
RS - 这是寄存器选择引脚。它控制输入数据是显示在LCD上还是用作控制字符。
RW- 将LCD置于读或写模式。在大多数情况下,您将使用读取模式,因此该引脚可以永久连接到地。
EN - 启用引脚。高电平时,它读取应用于数据引脚的数据。低时执行命令或显示数据。
D0 - 数据输入0。
D1 - 数据输入1。
D2 - 数据输入2。
D3 - 数据输入3。
D4 - 数据输入4。
D5 - 数据输入5。
D6- 数据输入6。
D7 - 数据输入7。
A - 与背光LED的阳极(正电压)连接。
K - 与背光LCD的阴极(接地或负电压)连接。

确定I2C地址

并非所有I2C适配器都具有相同的I2C地址,大多数具有地址0x20但有些使用地址0x27或0x3F。您可以通过短接电路板上的一些焊盘来更改适配器的地址。

如果你不知道地址,你需要找到它才能运行草图,我将要告诉你。幸运的是,由于Nick Gammon的出色工作,有一种简单的方法可以做到这一点。

这里写了一个简单的I2C扫描仪草图,他将其放入公共领域。它扫描您的I2C总线并返回它找到的每个I2C设备的地址。我在这里重复了Nick的草图,它也在ZIP文件中,您可以使用本文的所有代码下载。

在运行显示测试前检查是否已经安装了library: LiquidCrystal, LiquidCrystal_I2C

// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011

#include <Wire.h>

void setup() {
  Serial.begin (9600);

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 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 (")");
      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).");
}  // end of setup

void loop() {}

LCD1602显示温度

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack

void setup() { // activate LCD module
    Serial.begin(9600);
  
    lcd.begin (16,2); // for 16 x 2 LCD module
    lcd.setBacklightPin(3,POSITIVE);
    lcd.setBacklight(HIGH);
    pinMode(A0,INPUT);
    
}

void loop() {
    float temp = analogRead(A0);
    int temp2 = map(temp,0,1023,-50,100);
    
    lcd.home (); // LCD1602 (16,2),这里显示屏( 0,0)开始显示 
    lcd.print("Temp is : ");
    lcd.print(temp2);
    Serial.println(temp2);
    lcd.print("℃");
    
    lcd.setCursor (6,1); // go to start of 2nd line 第二行第六列开始显示
    //lcd.print(millis());
    delay(500);
    lcd.print("2018-12-15");
    /*lcd.setBacklight(LOW); // Backlight off 
    delay(1000);
    lcd.setBacklight(HIGH); // Backlight on 
    */
}

猜你喜欢

转载自blog.csdn.net/Naiva/article/details/85015401