1.直接与Arduino相连
2.通过转接板利用I2C的方式与Arduino相连
1. 直接与Arduino相连
直接与Arduino相连的好处是不用现另外购买转接板,但这样造成的后果就是要大量占用Arduino的IO口。如果你的项目外接的传感器不多,那还好,但如果你需要外接很多个传感器或者其他配件,那你的IO口就会告急了~
所需材料
- 1x Arduino UNO
- 1x LCD 16x2
- 1x 10KΩ旋转变阻器
- 1x 面包板
接线示意图
加载库文件
- 在Arduino IDE 1.6.2 或者以上版本中,
项目
->加载库
->管理库
中搜索LiquidCrystal,然后安装
即可。
示例代码
// include the library code:#include// initialize the library with the numbers of the interface pinsLiquidCrystal 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);}
参考文献
2.通过PCF8574T转接板与Arduino相连
通过此种方式,可以大大节省Arduino的IO口,前提是你还得购买一块PCF8574T转接板。
所需要材料
- 1x Arduino UNO
- 1x LCD 16x2
- 1x PCF8574T转接板
- 电烙铁、焊锡、松香等
接线
首先,把转接板焊接到LCD显示屏上(方向如上图)
- SCL -> 最上面的口
- SDA -> 第二个口
扫描I2C地址
将以下代码拷贝到Arduino IDE,并执行。然后选择工具
->串口监视器
,把右下角的波特率改为115200
,即可读出I2C地址,如下图。
// I2C Scanner// Written by Nick Gammon// Date: 20th April 2011#includevoid 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 setupvoid loop() {}
加载库文件
到下载最新的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#include #include 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 lcd.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.jianshu.com/p/eee98fb5e68f
在这里感谢作者的贡献。