.. note:: こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。 **参加する理由は?** - **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。 - **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。 - **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。 - **特別割引**:最新製品の独占割引をお楽しみください。 - **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。 👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう! .. _4.1.8_py_pi5: 4.1.5 ようこそ ===================================== はじめに ------------- このプロジェクトでは、歩行者の動きを検出するためにPIRを使用し、 センサー付きコンビニエンスストアの扉の動作を模倣するためにサーボ、LED、ブザーを使用します。歩行者がPIRの感知範囲内に現れると、インジケーターライトが点灯し、扉が開き、ブザーが開店ベルを演奏します。 必要な部品 ------------------------------ このプロジェクトには、次のコンポーネントが必要です。 .. image:: ../python_pi5/img/4.1.8_welcome_list.png :width: 800 :align: center 一式を購入するのが便利です、こちらがリンクです: .. 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_pir` - \- * - :ref:`cpn_servo` - |link_servo_buy| * - :ref:`cpn_buzzer` - |link_passive_buzzer_buy| * - :ref:`cpn_transistor` - |link_transistor_buy| 回路図 ------------------- ============ ======== ======== === T-Board Name physical wiringPi BCM GPIO18 Pin 12 1 18 GPIO17 Pin 11 0 17 GPIO27 Pin 13 2 27 GPIO22 Pin 15 3 22 ============ ======== ======== === .. image:: ../python_pi5/img/4.1.8_welcome_schematic.png :align: center 実験手順 ------------------------- **ステップ 1:** 回路を組み立てます。 .. image:: ../python_pi5/img/4.1.8_welcome_circuit.png :align: center **ステップ 2:** ディレクトリを変更します。 .. raw:: html .. code-block:: cd ~/raphael-kit/python-pi5 **ステップ 3:** 実行します。 .. raw:: html .. code-block:: sudo python3 3.1.2_Welcome_zero.py コードが実行されると、PIRセンサーが通行人を検出した場合、扉は自動的に開き(サーボで模倣)、インジケーターライトが点灯し、開店ベルの音楽が再生されます。ドアベルの音楽が再生された後、システムは自動的に扉を閉じ、インジケーターライトを消灯し、次回の通行人を待機します。 PIRモジュールには2つのポテンショメータがあります。1つは感度を調整するためのもので、もう1つは検出距離を調整するためのものです。PIRモジュールをより良く動作させるには、これらのポテンショメータを両方とも反時計回りにまわす必要があります。 .. image:: ../python_pi5/img/4.1.8_PIR_TTE.png :width: 400 :align: center .. 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, MotionSensor, Servo, TonalBuzzer import time # GPIO pin setup for LED, motion sensor (PIR), and buzzer ledPin = LED(6) pirPin = MotionSensor(21) buzPin = TonalBuzzer(27) # Servo motor pulse width correction factor and calculation myCorrection = 0.45 maxPW = (2.0 + myCorrection) / 1000 # Maximum pulse width minPW = (1.0 - myCorrection) / 1000 # Minimum pulse width # Initialize servo with custom pulse widths servoPin = Servo(25, min_pulse_width=minPW, max_pulse_width=maxPW) # Musical tune for buzzer, with notes and durations tune = [('C#4', 0.2), ('D4', 0.2), (None, 0.2), ('Eb4', 0.2), ('E4', 0.2), (None, 0.6), ('F#4', 0.2), ('G4', 0.2), (None, 0.6), ('Eb4', 0.2), ('E4', 0.2), (None, 0.2), ('F#4', 0.2), ('G4', 0.2), (None, 0.2), ('C4', 0.2), ('B4', 0.2), (None, 0.2), ('F#4', 0.2), ('G4', 0.2), (None, 0.2), ('B4', 0.2), ('Bb4', 0.5), (None, 0.6), ('A4', 0.2), ('G4', 0.2), ('E4', 0.2), ('D4', 0.2), ('E4', 0.2)] def setAngle(angle): """ Move the servo to a specified angle. :param angle: Angle in degrees (0-180). """ value = float(angle / 180) # Convert angle to servo value servoPin.value = value # Set servo position time.sleep(0.001) # Short delay for servo movement def doorbell(): """ Play a musical tune using the buzzer. """ for note, duration in tune: buzPin.play(note) # Play the note time.sleep(float(duration)) # Duration of the note buzPin.stop() # Stop buzzer after playing the tune def closedoor(): # Turn off LED and move servo to close door ledPin.off() for i in range(180, -1, -1): setAngle(i) # Move servo from 180 to 0 degrees time.sleep(0.001) # Short delay for smooth movement time.sleep(1) # Wait after closing door def opendoor(): # Turn on LED, open door (move servo), play tune, close door ledPin.on() for i in range(0, 181): setAngle(i) # Move servo from 0 to 180 degrees time.sleep(0.001) # Short delay for smooth movement time.sleep(1) # Wait before playing the tune doorbell() # Play the doorbell tune closedoor() # Close the door after the tune def loop(): # Main loop to check for motion and operate door while True: if pirPin.motion_detected: opendoor() # Open door if motion detected time.sleep(0.1) # Short delay in loop try: loop() except KeyboardInterrupt: # Clean up GPIO on user interrupt (e.g., Ctrl+C) buzPin.stop() ledPin.off() **代码解释** #. 脚本は必要なモジュールをインポートすることから始まります。 ``gpiozero`` ライブラリは、LED、モーションセンサー、サーボモーター、音楽ブザーとのインターフェースを提供するために使用されます。 ``time`` モジュールはタイミング関連の機能を処理するために使用されます。 .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED, MotionSensor, Servo, TonalBuzzer import time #. LED、PIRモーションセンサー、音楽ブザーをそれぞれのGPIOピンに初期化します。 .. code-block:: python # GPIO pin setup for LED, motion sensor (PIR), and buzzer ledPin = LED(6) pirPin = MotionSensor(21) buzPin = TonalBuzzer(27) #. サーボモーターの最大および最小パルス幅を計算し、微調整のための補正ファクターを組み込みます。 .. code-block:: python # Servo motor pulse width correction factor and calculation myCorrection = 0.45 maxPW = (2.0 + myCorrection) / 1000 # Maximum pulse width minPW = (1.0 - myCorrection) / 1000 # Minimum pulse width #. サーボモーターを正確な位置に配置するためにカスタムパルス幅を使用してGPIOピン25上で初期化します。 .. code-block:: python # Initialize servo with custom pulse widths servoPin = Servo(25, min_pulse_width=minPW, max_pulse_width=maxPW) #. ブザーで演奏するための音楽チューンを、音符(周波数)と持続時間(秒)のシーケンスとして定義します。 .. code-block:: python # Musical tune for buzzer, with notes and durations tune = [('C#4', 0.2), ('D4', 0.2), (None, 0.2), ('Eb4', 0.2), ('E4', 0.2), (None, 0.6), ('F#4', 0.2), ('G4', 0.2), (None, 0.6), ('Eb4', 0.2), ('E4', 0.2), (None, 0.2), ('F#4', 0.2), ('G4', 0.2), (None, 0.2), ('C4', 0.2), ('B4', 0.2), (None, 0.2), ('F#4', 0.2), ('G4', 0.2), (None, 0.2), ('B4', 0.2), ('Bb4', 0.5), (None, 0.6), ('A4', 0.2), ('G4', 0.2), ('E4', 0.2), ('D4', 0.2), ('E4', 0.2)] #. 指定された角度にサーボを移動するための関数。角度をサーボの値に変換します。 .. code-block:: python def setAngle(angle): """ Move the servo to a specified angle. :param angle: Angle in degrees (0-180). """ value = float(angle / 180) # Convert angle to servo value servoPin.value = value # Set servo position time.sleep(0.001) # Short delay for servo movement #. ブザーを使用して音楽を演奏するための関数。 ``tune`` リスト内を繰り返し、各音符を指定された持続時間で演奏します。 .. code-block:: python def doorbell(): """ Play a musical tune using the buzzer. """ for note, duration in tune: buzPin.play(note) # Play the note time.sleep(float(duration)) # Duration of the note buzPin.stop() # Stop buzzer after playing the tune #. サーボモーターを使用してドアを開閉するための関数。 ``opendoor`` 関数はLEDを点灯させ、ドアを開け、音楽を演奏し、その後ドアを閉じます。 .. code-block:: python def closedoor(): # Turn off LED and move servo to close door ledPin.off() for i in range(180, -1, -1): setAngle(i) # Move servo from 180 to 0 degrees time.sleep(0.001) # Short delay for smooth movement time.sleep(1) # Wait after closing door def opendoor(): # Turn on LED, open door (move servo), play tune, close door ledPin.on() for i in range(0, 181): setAngle(i) # Move servo from 0 to 180 degrees time.sleep(0.001) # Short delay for smooth movement time.sleep(1) # Wait before playing the tune doorbell() # Play the doorbell tune closedoor() # Close the door after the tune #. モーション検出を常にチェックするメインループ。モーションが検出された場合、 ``opendoor`` 関数がトリガーされます。 .. code-block:: python def loop(): # Main loop to check for motion and operate door while True: if pirPin.motion_detected: opendoor() # Open door if motion detected time.sleep(0.1) # Short delay in loop #. メインループを実行し、スクリプトをキーボードコマンド(Ctrl+C)で停止できるようにし、クリーンな終了のためにブザーとLEDをオフにします。 .. code-block:: python try: loop() except KeyboardInterrupt: # Clean up GPIO on user interrupt (e.g., Ctrl+C) buzPin.stop() ledPin.off()