.. note::
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
**参加する理由は?**
- **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。
- **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。
- **特別割引**:最新製品の独占割引をお楽しみください。
- **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
.. _4.1.6_py_pi5:
4.1.3 磁気誘導アラームシステム
============================================
はじめに
-----------------
貴重な花瓶を手に入れたとき、誰かがそれを動かしたらすぐにアラームを聞くことができる磁気誘導アラームシステムを作ることができます。
必要な部品
------------------------------
このプロジェクトには、次のコンポーネントが必要です。
.. image:: ../python_pi5/img/4.1.6_magneticalarmsystem_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_buzzer`
- |link_passive_buzzer_buy|
* - :ref:`cpn_transistor`
- |link_transistor_buy|
* - :ref:`cpn_reed_switch`
- |link_reed_switch_buy|
回路図
----------------------
============ ======== ======== ===
T-Board Name physical wiringPi BCM
GPIO17 Pin 11 0 17
GPIO27 Pin 13 2 27
============ ======== ======== ===
.. image:: ../python_pi5/img/4.1.6_magneticalarmsystem_schematic.png
:align: center
実験手順
------------------------------
**ステップ 1:** 回路を組み立てます。
.. image:: ../python_pi5/img/4.1.6_magneticalarmsystem_circuit.png
:width: 800
:align: center
**ステップ 2:** コードのフォルダに入ります。
.. raw:: html
.. code-block::
cd ~/raphael-kit/python-pi5
**ステップ 3:** 実行します。
.. raw:: html
.. code-block::
sudo python3 4.1.6_MagneticAlarmSystem_zero.py
リードスイッチが磁石の影響を受ける場合(例えば、リードスイッチを台座に置き、磁石を花瓶に置く)、物体は安全です。このとき、リードスイッチは閉じた状態にあり、ブザーは鳴りません。
磁石を取り除く(例えば、花瓶が盗まれる)と、リードスイッチは磁気の影響を受けず、スイッチが開き、ブザーがアラームを鳴らします。
.. 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 Buzzer, Button
import time
# Initialize the buzzer on GPIO pin 27
buzzer = Buzzer(27)
# Initialize the reed switch on GPIO pin 17 with pull-up resistor enabled
reed_switch = Button(17, pull_up=True)
try:
while True:
# Check if the reed switch is pressed
if reed_switch.is_pressed:
# Turn off the buzzer if reed switch is pressed
buzzer.off()
else:
# If reed switch is not pressed, beep the buzzer
buzzer.on()
time.sleep(0.1) # Buzzer on for 0.1 seconds
buzzer.off()
time.sleep(0.1) # Buzzer off for 0.1 seconds
except KeyboardInterrupt:
# Turn off the buzzer when the program is interrupted (e.g., keyboard interrupt)
buzzer.off()
pass
**コード説明**
#. 必要なクラス ``Buzzer`` と ``Button`` を ``gpiozero`` ライブラリから、そしてPythonの標準ライブラリから ``time`` モジュールをインポートします。
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import Buzzer, Button
import time
#. ``Buzzer`` オブジェクトはGPIOピン27にリンクされ、 ``pull_up=True`` 引数を持つ ``Button`` (リードスイッチとして機能)はGPIOピン17に接続され、内部プルアップ抵抗が有効になります。
.. code-block:: python
# Initialize the buzzer on GPIO pin 27
buzzer = Buzzer(27)
# Initialize the reed switch on GPIO pin 17 with pull-up resistor enabled
reed_switch = Button(17, pull_up=True)
#. ``try`` ブロックには無限ループ( ``while True`` )が含まれ、リードスイッチの状態をチェックします。押されている場合( ``is_pressed`` )、ブザーはオフになります。そうでない場合、ブザーはビープ音を出します(0.1秒オン、0.1秒オフ)。
.. code-block:: python
try:
while True:
# Check if the reed switch is pressed
if reed_switch.is_pressed:
# Turn off the buzzer if reed switch is pressed
buzzer.off()
else:
# If reed switch is not pressed, beep the buzzer
buzzer.on()
time.sleep(0.1) # Buzzer on for 0.1 seconds
buzzer.off()
time.sleep(0.1) # Buzzer off for 0.1 seconds
#. ``except`` ブロックは、ターミナルでCtrl+CのようなKeyboardInterruptを捕捉し、安全にブザーをオフにします。
.. code-block:: python
except KeyboardInterrupt:
# Turn off the buzzer when the program is interrupted (e.g., keyboard interrupt)
buzzer.off()
pass