04. อาร์ดูโนกับแอลซีดี
การต่อ LCD ดูได้จาก http://www.arduino.cc/en/Reference/LiquidCrystalConstructor
ข้อควรระวัง
1. แรงดัน +5V ที่จ่ายเข้าขา 28 ของ บอรด์ Arduino อย่าให้เกิน
2. ขา 3 ของ LCD สามารถต่อลง GND ได้เลยจะสว่างที่สุด ไม่ต้องใช้ R ปรับค่า แต่ถ้าสว่างมากเกินไปให้ใช้ R 2 ตัวทำเป็นวงจรแบ่งแรงด้น
รูปที่ 1
LCD Library สำหรับ Arduino ดูรายละเอียดได้จาก http://arduino.cc/en/Reference/LiquidCrystal
• LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
• lcd.begin(16, 2); แบบ 16ตัวอักษร 2 บรรทัด
• lcd.print("hello, world!");
• lcd.print(millis()/1000);
• lcd.noBlink();
• lcd.blink();
• lcd.noCursor();
• lcd.cursor();
• lcd.noDisplay();
• lcd.display();
• lcd.setCursor(0, 0); // set the cursor to column 1, line 1
ผลการทำงาน
รูปที่ 2
หมายเหตุ ในรูปที่ 2 นี้ ใช้แหล่งจ่ายภายนอกเลี้ยง LCD ต่างหากเนื่องจาก แหล่งจ่ายจาก USB ไม่พอ ถ้าคอมพิวเตอร์เครื่องใหนพอ ก็ไม่จำเป็นต้องใช้
โปรแกรมที่ 1
พิมพ์คำว่า "Hello,world!" ที่บรรทัดแรก (บรรทัดที่ 0 ) คอลัมน์แรก (คอลัมน์ที่ 0)เป็นต้นไป
// เรียกใช้ไลบรารี LiquidCrystal.h
#include <LiquidCrystal.h>
// กำหนดขาสัญญาณของ Arduino ที่จะต่อกับ LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2); //กำหนดบบ LCD ขนาด 2 บรรทัด บรรทัดละ 16 ตัวอักษร
lcd.print("hello, world!"); //พิมพ์คำว่า "Hello,world!"
}
//โปรแกรมหลักไม่มีการทำงานอะไร
void loop() {
}
โปรแกรมที่ 2
พิมพ์ Hello World และเวลาเป็นวินาที ที่บรรทัดที่ 2 คอลัมน์แรก
ฟังก์ชั่นที่ใช้บอกเวลาในขณะทำงานคือ millis()
รายละเอียด
เมื่อเรียกใช้จะห้ค่าเวลาตั้งแต่บอร์ด Arduino เริ่มทำงาน มีหน่วยเป็นมิลลิวินาที ตั้งแต่ 0 ถึงประมาณ 50 วัน แล้วจะย้อนกลับมาเป็น 0 ใหม่
ดูรายละเอียดเพิ่มเติมได้จาก http://arduino.cc/en/Reference/
#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);
}
โปรแกรมที่ 3 Cursor กระพริบ
// 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() {
// Turn off the blinking cursor:
lcd.noBlink();
delay(3000);
// Turn on the blinking cursor:
lcd.blink();
delay(3000);
}
โปรแกรมที่ 4 ปิด-เปิด Cursor
// 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() {
// Turn off the cursor:
lcd.noCursor();
delay(500);
// Turn on the cursor:
lcd.cursor();
delay(500);
}
โปรแกรมที่ 5 แสดงและไม่แสดงตัวอักษร
// 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() {
// Turn off the display:
lcd.noDisplay();
delay(500);
// Turn on the display:
lcd.display();
delay(500);
}
โปรแกรมที่ 6 แสดงการกำหนดตำแหน่ง Cursor
// include the library code:
#include <LiquidCrystal.h>
// these constants won't change. But you can change the size of
// your LCD using them:
const int numRows = 2;
const int numCols = 16;
// 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(numCols,numRows);
}
void loop() {
// loop from ASCII 'a' to ASCII 'z':
for (int thisLetter = 'a'; thisLetter <= 'z'; thisLetter++) {
// loop over the columns:
for (int thisCol = 0; thisCol < numRows; thisCol++) {
// loop over the rows:
for (int thisRow = 0; thisRow < numCols; thisRow++) {
// set the cursor position:
lcd.setCursor(thisRow,thisCol);
// print the letter:
lcd.print(char(thisLetter));
delay(200);
}
}
}
}
โปรแกรมที่ 7 แสดงการอ่านสถานะสวิทช์แล้วแสดงผลออกทาง LCD
ต่อสวิทช์ sw1 เพิ่มตามรูปที่ 3
รูปที่ 3
#include <LiquidCrystal.h>
const int numRows = 2;
const int numCols = 16;
// 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(numCols,numRows);
pinMode(6,INPUT);
lcd.setCursor(0, 0);
lcd.print("Read switch status");
}
void loop() {
lcd.setCursor(0, 1);
if (digitalRead(6)==HIGH){
lcd.print("Switch Open");
}else{
lcd.print("Switch Close");
}
delay(200);
}
แบบฝึกหัด
ต่อสวิทช์ 8 ตัวเข้ากับ Arduino ตามรูปที่ 4 แล้วเขียนโปรแกรมเพื่ออ่านสถานะของสวิทช์ ให้แสดงสถานะของสวิทช์ทุกตัว บน LCD
รูปที่ 4