MicroPython จอแสดงผล ตอนที่ 2 (i2c OLED)

Sittipong Jansorn
Jul 22, 2017 · 2 min read

จอแสดงผล OLED เป็นจอขนาดเล็กกินพลังงานต่ำ เหมาะเอามาใช้งานสำหรับงานที่ต้องประหยัดพลังาน ก่อนอื่นต้องทำการโหลด ssd1306.py เป็นสคลิป driver สำหรับ OLED SSD1306 สำหรับสคลิป driver ssd1306.py จะใช้การแสดงผลผ่าน frame buffer ของ MicroPython โดยจะใช้รูปแบบตัวอักษรจากระบบเลย ไม่ต้องสร้างัวอักษรขึ้นมาใช้งาน

ทำการ upload ssd1306.py และตรวจสอบ

ampy -p COM11 put ssd1306.py
ampy -p COM11 ls

ssd1306.py

จากรูปเราพร้อมใช้งานสคลิปเรียบร้อยแล้วขั้นตอนก็จะเหมือนกับ i2c lcd

from machine import I2C

import module Pin

from machine import Pin

กำหนดขาที่ใช้เชื่อมต่อกับระบบบัส I2C โดยให้ SCL = GPIO2, SDA = GPIO0

scl = Pin(2)
sda = Pin(0)

สร้าง object I2C โดยใช้ชื่อตัวแปรเป็น i2c

i2c = I2C(scl=scl, sda=sda, freq=100000)

ทำการต่ออปกรณ์เข้ากับบอร์ดและทดสอบว่าใช้งานได้หรือไม โดยจะทำการแสกนอุปกร์ที่อยู่บนบัส

i2c.scan()

จะ return ค่าออกมาเป็น address ของอุปกรณ์ในรูปแบบของ array ในที่นี้ 60 = 0x3C เป็น address ของ OLED

import module SSD1306_I2C ที่อยู่ใน package ssd1306

from ssd1306 import SSD1306_I2C

ssd1306

สราง object SSD1306_I2C โดยใช้ชื่อตัวแปรเป็น oled

oled = SSD1306_I2C(128, 64, i2c)

oled

ตอนนี้ OLED ของเราพร้อมที่จะแสดงผลแล้ว

คำสั่งล้างหน้าจอ

oled.fill(0)

คำสั่งแสดงผลตัวอักษรที่ตำแหน่งจุด pixel X = 35, Y = 5

oled.text(‘ESP8266’, 35, 5)

คำสั่งแสดงผลตัวอักษรที่ตำแหน่งจุด pixel X = 20, Y = 20

oled.text(‘MicroPython’, 20, 20)

คำสั่งแสดงผลตัวอักษรที่ตำแหน่งจุด pixel X = 3, Y = 50

oled.text(‘TEMP: {0:.2f}’.format(25.5), 3, 50)

จะเห็นว่าคำสั่ง oled.text() จะไม่แสดงผลทันทีจนกว่าจะเรียก function ที่ชื่อว่า show()

คำสั่งให้จอแสดงผล

oled.show()

ลองเอาไปประยุกต์ใช้งานในการแสดงผลจาก sensor กันดู

    Sittipong Jansorn

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade