# MicroPython繼電器應用

繼電器是以小電流訊號驅動大電流開關的重要元件，有了它，我們就可以在透過micro:bit的一些簡單的電路及程式，去切換電扇、電燈等等這一類使用110V電源的電器，在家電自動化的專題中是不可或缺的元件喔。這次我們使用的繼電器是下面這個：

{% embed url="<https://www.playrobot.com/relay/537-dc-5v-relay-module-arduino.html>" %}

在針腳的部份有3個pin，分別是正負電源以及S訊號線，其中正負電源至少要供給5V的電力才夠，直接使用micro:bit上的3.3V並推不動這個電磁裝置，所以在這個練習中還需要有額外的電源供應裝置才行。另外3個用螺絲起子才能連接的接點就是用來當做是電器的開關。

使用micro:bit驅動這個繼電器請把S訊號連接到micro:bit上的任一個接腳，以下的程式是把它連接到pin2，另外，別忘了它的負極除了連接到電源的負極之外，也要再連一條線到micro:bit的GND上才行。

```python
from microbit import *

while True:
    pin2.write_digital(1)
    sleep(1000)
    pin2.write_digital(0)
    sleep(1000)
```

這個程式讓繼電器以1秒為間隔連續進行開和關的操作，正常的情況下，你將會聽到繼電器模組運作的聲音，也會看到繼電器模組上的燈號不斷地閃爍。


---

# 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-qi-yong.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.
