2015年11月28日 星期六

Arduino的教學09-使用I2C 2X16 lcd


一、學習目標:學習使用I2C 2X16 lcd
i2c為一種通訊界面,2x16代表lcd為二行,每行可以顯示16個字母。

二、接線圖


SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
GND – 接 GND
VCC – 接 +5V

三、參考資料:
http://coopermaa2nd.blogspot.tw/2012/09/i2c-16x2-lcd.html

四、程式碼
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  5.  
  6. void setup()
  7. {
  8. lcd.init(); // initialize the lcd
  9. // Print a message to the LCD.
  10. lcd.backlight();
  11. lcd.setCursor(1, 0);//第1列,空1格開始
  12. lcd.print("Hello, world!");
  13. lcd.setCursor(2, 1);//第2列,空2格開始
  14. lcd.print("second line");
  15. }
  16.  
  17. void loop()
  18. {
  19. }
練習一:如何每秒測量一次,利出
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. // set the LCD address to 0x27 for a 16 chars and 2 line display
  4. LiquidCrystal_I2C lcd(0x27,16,2);
  5. int no=0;//計數器
  6. void setup()
  7. {
  8. lcd.init(); // initialize the lcd
  9. // Print a message to the LCD.
  10. lcd.backlight();
  11. lcd.setCursor(1, 0);//第1列,空1格開始
  12. lcd.print("Hello, world!");
  13. lcd.setCursor(2, 1);//第2列,空2格開始
  14. lcd.print("sceond line");
  15. delay(3000);//等3秒
  16. }
  17. void loop(){
  18. no++;
  19. //資料顯示在LCD
  20. lcd.setCursor(0, 0);
  21. lcd.print("No.");
  22. lcd.print(no);
  23. delay(500); //每500ms更新一次
  24. lcd.clear();
  25. }
五、作業:
如何將 HC-SR04 超聲波模組測量結果,以lcd顯示。

  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. LiquidCrystal_I2C lcd(0x27,16,2);
  4.  
  5.  
  6. int number=0;
  7. #define TrigPIN 13
  8. #define EchoPIN 12
  9.  
  10. void setup() {
  11. pinMode(TrigPIN, OUTPUT);
  12. pinMode(EchoPIN, INPUT);
  13. lcd.init(); // initialize the lcd
  14. // Print a message to the LCD.
  15. lcd.backlight();
  16. lcd.setCursor(1, 0);//第1列,空1格開始
  17. lcd.print("Hello,HC_SR04!");
  18.  
  19. //序列埠除錯用
  20. Serial.begin(9600);
  21. Serial.println("Hello, HC_SR04!");
  22. delay(1000);
  23. }
  24. void loop() {
  25. float duration, distance;//時間、距離
  26. digitalWrite(TrigPIN, HIGH);
  27. delayMicroseconds(1000);
  28. digitalWrite(TrigPIN, LOW);
  29. duration = pulseIn (EchoPIN, HIGH);//pulseIn ( ) :讀取一個針腳的脈衝時間(HIGH或LOW)
  30. distance = (duration/2)/29;
  31.  
  32. number = number +1;
  33.  
  34. lcd.setCursor (0,0);
  35. lcd.print("No.");
  36. lcd.print (number);
  37. lcd.setCursor(0, 1);
  38. lcd.print ("Dis.");
  39. lcd.print(distance);
  40. lcd.print ("cm");
  41. Serial.print("No.");
  42. Serial.print (number);
  43. Serial.print (" Dis. ");
  44. Serial.print(distance);
  45. Serial.println (" cm");//換行
  46. delay(1000);
  47. lcd.clear();
  48. }
  49.  
畫愛心的方法
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  5. #if defined(ARDUINO) && ARDUINO >= 100
  6. #define printByte(args) write(args);
  7. #else
  8. #define printByte(args) print(args,BYTE);
  9. #endif
  10. uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
  11.  
  12.  
  13.  
  14. void setup() {
  15.  
  16. Serial.begin(9600);
  17. lcd.init(); // initialize the lcd
  18.  
  19. // Print a message to the LCD.
  20. lcd.backlight();
  21.  
  22.  
  23. lcd.createChar(0, heart);
  24. }
  25.  
  26. void loop() {
  27.  
  28. lcd.printByte(0);
  29.  
  30. delay(1000);
  31. lcd.clear();
  32.  
  33. }