找回密码
 立即注册

QQ登录

只需一步,快速开始

基础模块资料库

基于Arduino使用LCD1602显示屏



7.jpg
8.jpg

引脚说明

GND           <------>         地线
VCC 电源    <------>      (5V or3.3v 电源不同显示效果有点差别)
SDA I2C      <------>       数据线
SCL I2C       <------>       时钟线

接线方法

LCD1602 i2c模块      <------>      Ardunio Uno
GND                        <------>      GND接地线
VCC                         <------>      5V 接电源
SDA                         <------>       A4
SCL                          <------>       A5

程序实现

需要用到LCD1602I2C的库,下载地址是 https://github.com/marcoschwartz/LiquidCrystal_I2C
把下载的库放到Arduino的库里
文件夹说明:
LiquidCrystal_I2C-master     ——LCD1602 I2C库(库需要复制在arduino的库目录里)

  1. //LingShun lab
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h> //引用I2C库
  4. //设置LCD1602设备地址,这里的地址是0x3F,一般是0x20,或者0x27,具体看模块手册
  5. LiquidCrystal_I2C lcd(0x3F,16,2);
  6. void setup()
  7. {
  8. lcd.init(); // 初始化LCD
  9. lcd.backlight(); //设置LCD背景等亮
  10. }
  11. void loop()
  12. {
  13. lcd.setCursor(0,0); //设置显示指针
  14. lcd.print("LCD1602 iic Test"); //输出字符到LCD1602上
  15. lcd.setCursor(0,1);
  16. lcd.print(" by L.L.");
  17. delay(1000);
  18. }
复制代码

显示结果:

9.jpg
问题解疑

是一个模块还是两个模块?

这模块是通过LCD1602屏 和 LCD1602 I2C 模块 焊接结合的,可以直接买焊接好的,也可以分开买,不过就需要点动手能力。

无法正常显示?

刚上电的时候,老是显示一个个方块,如图
10.jpg
这情况一般是地址错误,一般情况下地址是0x20,0x27 如果2个都不对,就需要读取地址。

寻找设备地址的代码

  1. #include <Wire.h>
  2. void setup(){
  3. Wire.begin();
  4. Serial.begin(9600);
  5. Serial.println("\nI2C Scanner");
  6. }
  7. void loop(){
  8. byte error, address;
  9. int nDevices;
  10. Serial.println("Scanning...");
  11. nDevices = 0;
  12. for (address = 1; address < 127; address++ ){
  13. // The i2c_scanner uses the return value of
  14. // the Write.endTransmisstion to see if
  15. // a device did acknowledge to the address.
  16. Wire.beginTransmission(address);
  17. error = Wire.endTransmission();
  18. if (error == 0){
  19. Serial.print("I2C device found at address 0x");
  20. if (address < 16)
  21. Serial.print("0");
  22. Serial.print(address, HEX);
  23. Serial.println(" !");
  24. nDevices++;
  25. }else if (error == 4){
  26. Serial.print("Unknow error at address 0x");
  27. if (address < 16)
  28. Serial.print("0");
  29. Serial.println(address, HEX);
  30. }
  31. }
  32. if (nDevices == 0)
  33. Serial.println("No I2C devices found\n");
  34. else
  35. Serial.println("done\n");
  36. delay(5000); // wait 5 seconds for next scan
  37. }
复制代码
把模块按接线方法接好,上传这段代码后,打开端口监视器,就能找到在I2C上的设备地址
分享至 : QQ空间
收藏

0 个回复

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