LCD 1602 显示屏

LCD显示屏与Arduino连接可以有两种方式:

  1. 直接与Arduino相连
  2. 通过转接板利用I2C的方式与Arduino相连

1. 直接与Arduino相连

直接与Arduino相连的好处是不用现另外购买转接板,但这样造成的后果就是要大量占用Arduino的IO口。如果你的项目外接的传感器不多,那还好,但如果你需要外接很多个传感器或者其他配件,那你的IO口就会告急了~

所需材料

  • 1x Arduino UNO
  • 1x LCD 16x2
  • 1x 10KΩ旋转变阻器
  • 1x 面包板

接线示意图


LCD 1602 显示屏

LCD直接与Arduino相连的接线图

LCD 1602 显示屏


LCD Arduino
RS -> 12
E -> 11
D4 -> 5
D5 -> 4
D6 -> 3
D7 -> 2
VCC -> 5V
R/W -> GND
GND -> GND
LCD 旋转变阻器
VCC -> 左边引脚
Vo -> 中间引脚
R/W -> 右边引脚
GND -> 右边引脚

加载库文件

  • 在Arduino IDE 1.6.2 或者以上版本中, 项目->加载库->管理库中搜索LiquidCrystal,然后安装即可。

示例代码

// include the library code: 
#include  《LiquidCrystal.h》

// initialize the library with the numbers of the interface pins 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 

void setup() {
  // set up the LCD's number of columns and rows:  
  lcd.begin(16, 2); 
  // Print a message to the LCD. 
  lcd.print("hello, world!");
} 

void loop() { 
  // set the cursor to column 0, line 1 
  // (note: line 1 is the second row, since counting begins with 0): 
  lcd.setCursor(0, 1); 
  // print the number of seconds since reset: 
  lcd.print(millis()/1000);
}
参考文献

https://www.arduino.cc/en/Reference/LiquidCrystal


2.通过PCF8574T转接板与Arduino相连

通过此种方式,可以大大节省Arduino的IO口,前提是你还得购买一块PCF8574T转接板。

LCD 1602 显示屏
LCD显示屏与转接板

所需要材料

  • 1x Arduino UNO
  • 1x LCD 16x2
  • 1x PCF8574T转接板
  • 电烙铁、焊锡、松香等

接线

首先,把转接板焊接到LCD显示屏上(方向如上图)

PCF8574T Arduino
GND -> GND
VCC -> 5V
SDA -> A4
SCL -> A5

如果你的A4,A5口已经被占用,那么你还可以接到Arduino最上面的两个没有标文字的IO口,即D0-D13那一排最上面的那两个口

  • SCL -> 最上面的口
  • SDA -> 第二个口

扫描I2C地址

将以下代码拷贝到Arduino IDE,并执行。然后选择工具->串口监视器,把右下角的波特率改为115200,即可读出I2C地址。

// I2C Scanner
#include 《Wire.h》
void setup() { 
    Serial.begin (115200); // 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() {}
LCD 1602 显示屏
串口监视器中显示的地址:0x3F

加载库文件

这里下载最新的New LiquidCrystal,手动添加到你的 Arduino IDE中。(ps:记得修改你的I2C地址,把LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7);的0x3F改为你的真实地址)

示例代码

/* Demonstration sketch for PCF8574T I2C LCD Backpack Uses library 
from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads 
GNU General Public License, version 3 (GPL-3.0) */ 

#include  《Wire.h》
#include  《LCD.h》 
#include  《LiquidCrystal_I2C.h》

LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack  

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

void loop() { 
  lcd.home (); // set cursor to 0,0  
  lcd.print(" tronixlabs.com"); 
  lcd.setCursor (0,1); // go to start of 2nd line  
  lcd.print(millis()); 
  delay(1000); 
  lcd.setBacklight(LOW); // Backlight off delay(250);    
  lcd.setBacklight(HIGH); // Backlight on delay(1000);  
}
参考文献

http://www.instructables.com/id/Using-PCF8574-backpacks-with-LCD-modules-and-Ardui/?ALLSTEPS

经验教训

  1. 之前没有查看I2C的地址,傻傻的复制别人的代码过来直接用,结果人家是0x27,试了好久都不行,后来才看到一篇文章说要先确定I2C的地址,才得以解决。

链接:https://www.jianshu.com/p/eee98fb5e68f 


- 本文内容来自网络,如有侵权,请联系本站处理。

2022-05   阅读(423)   评论(0)
 标签: maker LCD

涨知识
PWM

PWM,英文名Pulse Width Modulation,是脉冲宽度调制缩写,它是通过调节占空比的变化来调节信号、能量等的变化。

评论: