หน้าแรกHome › Arduino › บทที่ 09Lesson 09
จอแสดงผล LCDLCD Screen
แสดงข้อความบนจอShow text on a screen
บทนี้เราจะได้...In this lesson you will...
- ต่อจอ LCD แบบ I2C ด้วยสายแค่ 4 เส้นWire an I2C LCD with just 4 wires
- ใช้ setCursor() เลือกตำแหน่ง และ print() เขียนข้อความUse setCursor() to pick a spot and print() to write
- แสดงข้อความและตัวเลขที่เปลี่ยนไปเรื่อย ๆShow text and a number that keeps changing
อุปกรณ์ที่ใช้What you need
I2C เป็นวิธีคุยกันผ่านสาย 2 เส้น: SDA (ข้อมูล) และ SCL (จังหวะนาฬิกา) อุปกรณ์หลายตัวต่อสายคู่เดียวกันได้ เพราะแต่ละตัวมีที่อยู่ของตัวเอง เช่น จอนี้คือ 0x27 บน Arduino Uno ขา SDA คือ A4 และ SCL คือ A5I2C lets parts talk over 2 wires: SDA (data) and SCL (clock). Many parts can share the pair because each has its own address; this screen is 0x27. On an Uno, SDA is A4 and SCL is A5.
โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ดLab bench: wire it, then run the code
ลากจากขาหรือช่องหนึ่งไปอีกช่องเพื่อต่อสาย · ชี้ที่ช่องเพื่อดูว่าช่องไหนเชื่อมกัน · แตะที่สายเพื่อลบ · แตะที่ LED เพื่อกลับขาDrag from one pin or hole to another to add a wire · point at a hole to see what's connected · tap a wire to remove it · tap an LED to flip it
ภารกิจMissions
จอ LCD 16×2 มี 16 ขา แต่ถ้าด้านหลังมีแผ่น I2C บัดกรีติดไว้ จะเหลือขาที่ต้องต่อแค่ 4 ขา (GND, VCC, SDA, SCL) เหมือนบนโต๊ะทดลองนี้A 16×2 LCD has 16 pins, but with an I2C board soldered on the back you only wire 4 (GND, VCC, SDA, SCL), just like on this bench.
อ่านโค้ดทีละบรรทัดReading the code, line by line
ช่องโค้ดเริ่มจากโครงเปล่า ปิ๊งจะบอกทีละบรรทัดว่าต้องพิมพ์อะไร ตอนกดรัน บรรทัดที่บอร์ดกำลังทำจะเป็นสีเหลือง คำสั่งสำคัญของบทนี้มีดังนี้The code box starts as an empty sketch, and Ping tells you what to type line by line. While it runs, the line the board is doing turns yellow. The key commands in this lesson:
LiquidCrystal_I2C lcd(0x27, 16, 2)สร้างจอชื่อ lcd ที่อยู่ 0x27 ขนาด 16 ตัวอักษร 2 บรรทัดMake a screen called lcd at address 0x27, 16 characters, 2 lineslcd.init(); lcd.backlight();เริ่มใช้งานจอ แล้วเปิดไฟหลังจอStart the screen, then turn on its backlightlcd.setCursor(0, 1)ย้ายตำแหน่งเขียนไปคอลัมน์ 0 บรรทัด 1 (นับจาก 0 บรรทัดล่างคือ 1)Move to column 0, line 1 (counting from 0, so the bottom line is 1)lcd.print("Hello!")เขียนข้อความลงจอ (ภาษาอังกฤษ ตัวเลข และเครื่องหมาย)Write text on the screen (English letters, digits, symbols)ลองแก้เองดูสิTry changing it
แก้โค้ดในช่องโค้ดของโต๊ะทดลองด้านบนได้เลย แล้วกด ▶ รันโค้ด ดูผล อยากเริ่มใหม่ทั้งหมด กด "ล้างโค้ด"Edit the code in the lab bench's code box above, then press ▶ Run code. Want to start over? Press "Clear code".
- เปลี่ยนข้อความเป็นชื่อเรากับชื่อโรงเรียนShow your name and your school
- ย้ายตัวนับวินาทีไปไว้บรรทัดบนขวาMove the seconds counter to the top right
- ท้าทาย: ทำข้อความเลื่อนจากขวาไปซ้ายด้วย for และ setCursorChallenge: scroll text from right to left with for and setCursor
มีบอร์ด Arduino จริง? กดคัดลอกโค้ด ไปวางในโปรแกรม Arduino IDE (ฟรี) แล้วกดอัปโหลดลงบอร์ดได้เลยHave a real Arduino? Copy the code into the free Arduino IDE and upload it to your board.
ควิซท้ายบทQuick quiz
จอ LCD แบบ I2C ใช้สายกี่เส้น?How many wires does an I2C LCD use?
บน Arduino Uno ขา SDA คือขาไหน?On an Arduino Uno, which pin is SDA?
lcd.setCursor(5, 0) คือตำแหน่งไหน?Where is lcd.setCursor(5, 0)?
จอนี้แสดงภาษาไทยได้ไหม?Can this screen show Thai?
ใบงาน: Worksheet: จอแสดงผล LCDLCD Screen
ออกแบบหน้าจอ 16×2 ของฉัน: เขียนตัวอักษรลงในตารางทีละช่องDesign my 16×2 screen: write one character in each box
ทำครบทุกภารกิจแล้ว?All missions done?
กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.