注釈

こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。

参加する理由は?

  • エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。

  • 学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。

  • 独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。

  • 特別割引:最新製品の独占割引をお楽しみください。

  • 祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。

👉 私たちと一緒に探索し、創造する準備はできていますか?[ここ]をクリックして今すぐ参加しましょう!

1.3.3 リレー

はじめに

このプロジェクトでは、リレーの使用方法を学びます。リレーは、自動制御システムで一般的に使用されるコンポーネントの1つです。電圧、電流、温度、圧力などが予め設定された値に達したり、超えたり、低下したりした場合、リレーは回路を接続または切断して、機器を制御および保護します。

必要な部品

このプロジェクトには、次のコンポーネントが必要です。

../_images/1.3.3_relay_list.png

一式を購入するのが便利です、こちらがリンクです:

名前

このキットのアイテム

リンク

Raphael Kit

337

Raphael Kit

以下のリンクから別々に購入することもできます。

コンポーネントの紹介

購入リンク

GPIO拡張ボード

BUY

ブレッドボード

BUY

ジャンパーワイヤー

BUY

抵抗器

BUY

LED

BUY

トランジスタ

BUY

リレー

BUY

ダイオード

BUY

回路図

../_images/1.3.3_relay_schematic.png

実験手順

ステップ1: 回路を組み立てます。

../_images/1.3.3_relay_circuit.png

ステップ2: コードファイルを開きます。

cd ~/raphael-kit/python-pi5

ステップ3: 実行します。

sudo python3 1.3.3_Relay_zero.py

コードが実行されている間、LEDが点灯します。さらに、通常閉じている接点が切断され、通常開いている接点が閉じることによるチクタク音が聞こえます。

コード

注釈

以下のコードを変更、リセット、コピー、実行、停止することができます。ただし、それを行う前に、ソースコードのパスである raphael-kit/python_5 に移動する必要があります。コードを変更した後、その効果を確認するために直接実行できます。

#!/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 をインポートしています。

    #!/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 オブジェクトを初期化しています。

    # Initialize the relay connected to GPIO pin 17, starting in the 'off' state
    relay = OutputDevice(17, initial_value=False)
    
  3. try ブロック内で、無限ループ while True がリレーの状態を繰り返し切り替えます。リレーは、各状態の間に1秒の遅延があり、コンソールにプリントされたステートメントが表示されます。

    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など)をキャッチして、スクリプトを正常に終了する前にリレーをオフにします。

    except KeyboardInterrupt:
      # Handle a keyboard interrupt (Ctrl+C) to exit the loop
      relay.off()  # Ensure the relay is turned off before exiting
      pass