# MicroPython光感測器模組

在之前使用的光感測器模組，可以輕易地利用MicroPython來偵測環境的亮度。連接的方法也很簡單，請把光感測模組的VCC接到3V3，GND接到micro:bit的GND，A0則是接到任意一個可以讀取類比信號的接腳，在這裡我們選擇pin0，連接完成之後，搭配SSD1306顯示器的程式碼如下所示：

```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, 'Temp.:{} C'.format(temperature()))
    add_text(0, 3, 'Light:{:>4d}'.format(pin1.read_analog()))
    sleep(2000)
```

從上面的程式同學們應該可以看出，真正執行讀取亮度的，就只有第11行format函數中的那個pin1.read\_analog()這個指令而已，讀出來的數值會被放到「Light:{}」字串中，組合之後呈現在oled顯示器上。執行的結果如下所示：

![](/files/-LenM2Ad0Ryk1YZBT1PT)


---

# 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-guang-gan-qi-mo.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.
