# MicroPython SSD1306 oled實作

在前面的單元中我們曾經介紹過一款0.96吋，解析度在128x64的OLED顯示器，當時是在積木開發的環境下使用它的擴充積木來顯示文字，現在，使用同樣的連線方式（把SCL和SCL接在一起，SDA和SDA接在一起，並供電給oled），利用MicroPython也可以在顯示器上顯示出文字和圖形。

不過在內建的MicroPython模組中並沒有直接支援這款顯示器（ssd1306）的程式碼，所幸有熱心人士提供了Python程式可供我們匯入使用，連結如下：

{% embed url="<https://github.com/fizban99/microbit_ssd1306>" %}

在上面的網頁中有詳細的使用說明，對我們來說，只要把這些程式（所有\*.py）通通下載到我們的電腦，解壓縮之後把所有的程式都放在mu\_code資料夾之下，再透過Mu Editor把這些.py的檔案逐一上傳到micro:bit板子上，基本上就可以當作是模組在我們的程式中匯入並使用它的函數了。

先來看看程式：

```python
from ssd1306 import initialize, clear_oled
from ssd1306_text import add_text
from microbit import *

initialize()
clear_oled()
add_text(0, 0, 'NKUST')
add_text(0, 1, 'Richard Ho')
while True:
    add_text(0, 2, '{:>4d}'.format(temperature()))
    sleep(2000)
```

如果只是要顯示文字的話，需要用到的是initialize初始化函數以及clear\_oled，用來清除畫面的clear\_oled函數，要顯示文 字則是使用add\_text函數，它的前2個參數分別是行以及列，第3個函數則是要顯示的文字字串內容。

在上面的程式例子中的第7行先在第0列顯示出學校的簡稱，第8行在第1列顯示出自己的英文名字，第10行則是在第2列顯示出目前的晶片表面溫度。

有了這個顯示器，我們就不再需要使用內建5x5LED來捲動文字了，非常方便喔。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nkust.gitbook.io/micro-bit/micropython-ssd1306-oled-zuo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
