.. note::
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
**参加する理由は?**
- **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。
- **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。
- **特別割引**:最新製品の独占割引をお楽しみください。
- **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
.. _1.3.3_py_pi5:
1.3.3 リレー
===============
はじめに
------------
このプロジェクトでは、リレーの使用方法を学びます。リレーは、自動制御システムで一般的に使用されるコンポーネントの1つです。電圧、電流、温度、圧力などが予め設定された値に達したり、超えたり、低下したりした場合、リレーは回路を接続または切断して、機器を制御および保護します。
必要な部品
------------------------------
このプロジェクトには、次のコンポーネントが必要です。
.. image:: ../python_pi5/img/1.3.3_relay_list.png
一式を購入するのが便利です、こちらがリンクです:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - 名前
- このキットのアイテム
- リンク
* - Raphael Kit
- 337
- |link_Raphael_kit|
以下のリンクから別々に購入することもできます。
.. list-table::
:widths: 30 20
:header-rows: 1
* - コンポーネントの紹介
- 購入リンク
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_led`
- |link_led_buy|
* - :ref:`cpn_transistor`
- |link_transistor_buy|
* - :ref:`cpn_relay`
- |link_relay_buy|
* - :ref:`cpn_diode`
- |link_diode_buy|
回路図
-----------------
.. image:: ../python_pi5/img/1.3.3_relay_schematic.png
実験手順
-----------------------
**ステップ1:** 回路を組み立てます。
.. image:: ../python_pi5/img/1.3.3_relay_circuit.png
**ステップ2:** コードファイルを開きます。
.. raw:: html
.. code-block::
cd ~/raphael-kit/python-pi5
**ステップ3:** 実行します。
.. raw:: html
.. code-block::
sudo python3 1.3.3_Relay_zero.py
コードが実行されている間、LEDが点灯します。さらに、通常閉じている接点が切断され、通常開いている接点が閉じることによるチクタク音が聞こえます。
.. warning::
エラー メッセージ ``RuntimeError: Cannot determine SOC peripheral base address`` が表示された場合は、 :ref:`faq_soc` を参照してください。
**コード**
.. note::
以下のコードを変更、リセット、コピー、実行、停止することができます。ただし、それを行う前に、ソースコードのパスである ``raphael-kit/python_5`` に移動する必要があります。コードを変更した後、その効果を確認するために直接実行できます。
.. raw:: html
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import OutputDevice # Import the class for controlling GPIO pins
from time import sleep # Import the sleep function for delay
# Initialize the relay connected to GPIO pin 17, starting in the 'off' state
relay = OutputDevice(17, initial_value=False)
try:
# Loop to continuously toggle the relay's state every second
while True:
print('Relay open...') # Inform that the relay is being activated
relay.on() # Turn on the relay (assuming active low configuration)
sleep(1) # Maintain the relay in the on state for 1 second
print('...Relay close') # Inform that the relay is being deactivated
relay.off() # Turn off the relay
sleep(1) # Maintain the relay in the off state for 1 second
except KeyboardInterrupt:
# Handle a keyboard interrupt (Ctrl+C) to exit the loop
relay.off() # Ensure the relay is turned off before exiting
pass
**コードの説明**
1. このセクションでは、必要なライブラリをインポートしています。GPIOピンを制御するために ``gpiozero`` から ``OutputDevice`` を、時間制御のために ``time`` から ``sleep`` をインポートしています。
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import OutputDevice # Import the class for controlling GPIO pins
from time import sleep # Import the sleep function for delay
2. GPIOピン17に接続されたリレーを制御するための ``OutputDevice`` オブジェクトを初期化しています。
.. code-block:: python
# Initialize the relay connected to GPIO pin 17, starting in the 'off' state
relay = OutputDevice(17, initial_value=False)
3. ``try`` ブロック内で、無限ループ ``while True`` がリレーの状態を繰り返し切り替えます。リレーは、各状態の間に1秒の遅延があり、コンソールにプリントされたステートメントが表示されます。
.. code-block:: python
try:
# Loop to continuously toggle the relay's state every second
while True:
print('Relay open...') # Inform that the relay is being activated
relay.on() # Turn on the relay (assuming active low configuration)
sleep(1) # Maintain the relay in the on state for 1 second
print('...Relay close') # Inform that the relay is being deactivated
relay.off() # Turn off the relay
sleep(1) # Maintain the relay in the off state for 1 second
4. KeyboardInterrupt(Ctrl+Cなど)をキャッチして、スクリプトを正常に終了する前にリレーをオフにします。
.. code-block:: python
except KeyboardInterrupt:
# Handle a keyboard interrupt (Ctrl+C) to exit the loop
relay.off() # Ensure the relay is turned off before exiting
pass