.. note:: こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。 **参加する理由は?** - **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。 - **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。 - **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。 - **特別割引**:最新製品の独占割引をお楽しみください。 - **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。 👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう! .. _2.1.2_py_pi5: 2.1.2 マイクロスイッチ ======================== はじめに -------------------- このプロジェクトでは、マイクロスイッチの使用方法を学びます。マイクロスイッチは小さくて非常に敏感なスイッチで、最小限の圧力で作動します。信頼性が高く、感度が良いため、マイクロスイッチは安全装置としてよく使用されます。 例えば、何かや誰かが道を塞いでいる時にドアが閉まらないようにするなど、様々な用途で使われます。 必要な部品 ------------------------------ このプロジェクトには、次のコンポーネントが必要です。 .. image:: ../python_pi5/img/2.1.2_micro_switch_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_micro_switch` - \- * - :ref:`cpn_capacitor` - |link_capacitor_buy| 回路図 ----------------- マイクロスイッチの左側のピンをGPIO17に接続し、 GPIO22とGPIO27にそれぞれLEDを二つ接続します。 そして、マイクロスイッチの動くアームを押して離すと、 二つのLEDが交互に点灯するのが見られます。 .. image:: ../python_pi5/img/2.1.2_micro_switch_schematic_1.png .. image:: ../python_pi5/img/2.1.2_micro_switch_schematic_2.png 実験手順 ----------------------- **ステップ 1:** 回路を組み立てる。 .. image:: ../python_pi5/img/2.1.2_micro_switch_circuit.png **ステップ 2**: コードのフォルダに移動する。 .. raw:: html .. code-block:: cd ~/raphael-kit/python-pi5 **ステップ 3**: 実行する。 .. raw:: html .. code-block:: sudo python3 2.1.2_MicroSwitch_zero.py コードが実行されている間、動くアームを押すと、黄色いLEDが点灯し、 動くアームを離すと、赤いLEDが点灯します。 .. warning:: エラー メッセージ ``RuntimeError: Cannot determine SOC peripheral base address`` が表示された場合は、 :ref:`faq_soc` を参照してください。 **コード** .. note:: 下記のコードを **変更/リセット/コピー/実行/停止** することができます。しかし、その前に ``raphael-kit/python-pi5`` のようなソースコードのパスに移動する必要があります。コードを変更した後、直接実行して効果を見ることができます。 .. raw:: html .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED, Button # Import LED and Button classes from gpiozero from time import sleep # Import sleep function for delays # Initialize micro switch on GPIO pin 17 with the pull-up resistor disabled micro_switch = Button(17, pull_up=False) # Initialize LED1 connected to GPIO pin 22 led1 = LED(22) # Initialize LED2 connected to GPIO pin 27 led2 = LED(27) try: # Continuously check the state of the micro switch and control LEDs accordingly while True: if micro_switch.is_pressed: # If the micro switch is pressed print('LED1 ON') # Print a message to the console led1.on() # Turn on LED1 led2.off() # Turn off LED2 else: # If the micro switch is not pressed print(' LED2 ON') # Print a message to the console led1.off() # Turn off LED1 led2.on() # Turn on LED2 sleep(0.5) # Pause for 0.5 seconds before checking the switch again except KeyboardInterrupt: # Handle KeyboardInterrupt (Ctrl+C) to exit the loop gracefully pass **コード説明** #. この行はスクリプトがPython 3で実行されるように設定します。GPIOデバイスを制御するために ``gpiozero`` から ``LED`` と ``Button`` を、遅延のために ``time`` から ``sleep`` をインポートします。 .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED, Button # Import LED and Button classes from gpiozero from time import sleep # Import sleep function for delays #. プルアップ抵抗を無効にしてGPIOピン17に接続されたマイクロスイッチ、そしてGPIOピン22と27に接続された二つのLEDを初期化します。 .. code-block:: python # Initialize micro switch on GPIO pin 17 with the pull-up resistor disabled micro_switch = Button(17, pull_up=False) # Initialize LED1 connected to GPIO pin 22 led1 = LED(22) # Initialize LED2 connected to GPIO pin 27 led2 = LED(27) #. メインループでは、マイクロスイッチの状態をチェックします。押されていれば、LED1が点灯し、LED2が消灯します。押されていなければ、LED1が消灯し、LED2が点灯します。このループは0.5秒ごとに繰り返されます。Ctrl+C(KeyboardInterrupt)を捕捉して、スクリプトを優雅に終了させることができます。 .. code-block:: python try: # Continuously check the state of the micro switch and control LEDs accordingly while True: if micro_switch.is_pressed: # If the micro switch is pressed print('LED1 ON') # Print a message to the console led1.on() # Turn on LED1 led2.off() # Turn off LED2 else: # If the micro switch is not pressed print(' LED2 ON') # Print a message to the console led1.off() # Turn off LED1 led2.on() # Turn on LED2 sleep(0.5) # Pause for 0.5 seconds before checking the switch again except KeyboardInterrupt: # Handle KeyboardInterrupt (Ctrl+C) to exit the loop gracefully pass