.. note::
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
**参加する理由は?**
- **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。
- **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。
- **特別割引**:最新製品の独占割引をお楽しみください。
- **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
.. _2.1.1_py_pi5:
2.1.1 ボタン
===============
はじめに
-----------------
このプロジェクトでは、ボタンを使用してLEDをオンまたはオフにする方法を学びます。
必要な部品
------------------------------
このプロジェクトには、次のコンポーネントが必要です。
.. image:: ../python_pi5/img/2.1.1_Button_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_button`
- |link_button_buy|
回路図
---------------------
Raspberry Piの入力として通常開放ボタンを使用し、接続は以下の回路図に示す通りです。ボタンが押されると、GPIO18は高レベル(3.3V)になります。プログラムを介してGPIO18の状態を検出できます。つまり、GPIO18が高レベルになると、ボタンが押されたことを意味します。ボタンが押されたときに対応するコードを実行し、LEDが点灯します。
.. note::
LEDの長いピンが陽極であり、短いピンが陰極です。
.. image:: ../python_pi5/img/2.1.1_Button_schematic_1.png
.. image:: ../python_pi5/img/2.1.1_Button_schematic_2.png
実験手順
---------------------------
**ステップ1**: 回路を組み立てます。
.. image:: ../python_pi5/img/2.1.1_Button_circuit.png
**ステップ2**: コードファイルを開きます。
.. raw:: html
.. code-block::
cd ~/raphael-kit/python-pi5
**ステップ3**: コードを実行します。
.. raw:: html
.. code-block::
sudo python3 2.1.1_Button_zero.py
ボタンを押すとLEDが点灯し、ボタンを離すと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 LED, Button # Import LED and Button classes from gpiozero
from signal import pause # Import pause function from signal module
# Initialize an LED object on GPIO pin 17
led = LED(17)
# Initialize a Button object on GPIO pin 18
button = Button(18)
# Link the button's "when_pressed" event to the LED's on() method
button.when_pressed = led.on
# Link the button's "when_released" event to the LED's off() method
button.when_released = led.off
# Run an event loop that waits for button events and keeps the script running
pause()
**コードの説明**
#. このスクリプトはPython3で書かれており、 ``gpiozero`` ライブラリから ``LED`` および ``Button`` クラス、 ``signal`` モジュールから ``pause`` をインポートしています。
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import LED, Button # Import LED and Button classes from gpiozero
from signal import pause # Import pause function from signal module
#. GPIOピン17に接続されたLEDオブジェクトとGPIOピン18に接続されたボタンオブジェクトを初期化します。
.. code-block:: python
# Initialize an LED object on GPIO pin 17
led = LED(17)
# Initialize a Button object on GPIO pin 18
button = Button(18)
#. ボタンが押されたときにLEDが点灯し、ボタンが離されたときにLEDが消灯するようにイベントハンドラを設定します。
.. code-block:: python
# Link the button's "when_pressed" event to the LED's on() method
button.when_pressed = led.on
# Link the button's "when_released" event to the LED's off() method
button.when_released = led.off
#. ボタンの押下およびリリースイベントを待ち続けるために、スクリプトをイベントループ内で実行し続けます。
.. code-block:: python
# Run an event loop that waits for button events and keeps the script running
pause()