.. note::
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
**参加する理由は?**
- **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。
- **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。
- **特別割引**:最新製品の独占割引をお楽しみください。
- **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
.. _2.1.3_py_pi5:
2.1.3 タッチスイッチモジュール
=================================
はじめに
-------------------
このプロジェクトでは、タッチスイッチモジュールについて学びます。これは従来のスイッチを置き換えるもので、操作が便利で、タッチ感が良く、制御が正確で、機械的な摩耗が最小限に抑えられるという利点があります。
必要な部品
------------------------------
このプロジェクトには、次のコンポーネントが必要です。
.. image:: ../python_pi5/img/2.1.3_touch_switch_list.png
:width: 700
: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_touch_switch`
- |link_touch_buy|
回路図
-----------------
.. image:: ../python_pi5/img/2.1.3_touch_switch_schematic.png
:width: 500
:align: center
実験手順
------------------------------
**ステップ 1:** 回路を組み立てる。
.. image:: ../python_pi5/img/2.1.3_touch_switch_circuit.png
:width: 700
:align: center
**ステップ 2:** ディレクトリを変更する。
.. raw:: html
.. code-block::
cd ~/raphael-kit/python-pi5
**ステップ 3:** 実行する。
.. raw:: html
.. code-block::
sudo python3 2.1.3_TouchSwitch_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 for delay
# Initialize touch sensor (Button) on GPIO pin 17, pull-up resistor disabled
touch_sensor = Button(17, pull_up=False) # Suitable for sensors that pull the pin low when pressed
# Initialize LED1 and LED2 connected to GPIO pins 22 and 27 respectively
led1 = LED(22) # LED1 connected to GPIO pin 22
led2 = LED(27) # LED2 connected to GPIO pin 27
try:
# Continuously monitor the state of the touch sensor and control LEDs accordingly
while True:
if touch_sensor.is_pressed: # Check if the touch sensor is pressed
print('You touch it!') # Output message indicating sensor activation
led1.off() # Turn off LED1
led2.on() # Turn on LED2
else: # If the sensor is not pressed
led1.on() # Turn on LED1
led2.off() # Turn off LED2
sleep(0.5) # Pause for 0.5 seconds before rechecking the sensor state
except KeyboardInterrupt:
# Handle a keyboard interrupt (Ctrl+C) for a clean exit from the loop
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 for delay
#. プルアップ抵抗を無効にしてGPIOピン17にタッチセンサー(Buttonとして)を初期化し、GPIOピン22と27に二つのLEDを初期化します。
.. code-block:: python
# Initialize touch sensor (Button) on GPIO pin 17, pull-up resistor disabled
touch_sensor = Button(17, pull_up=False) # Suitable for sensors that pull the pin low when pressed
# Initialize LED1 and LED2 connected to GPIO pins 22 and 27 respectively
led1 = LED(22) # LED1 connected to GPIO pin 22
led2 = LED(27) # LED2 connected to GPIO pin 27
#. メインループでは、タッチセンサーの状態をチェックします。触れられたとき、LED2が点灯しLED1が消灯します;触れられていないとき、LED1が点灯しLED2が消灯します。このループは0.5秒ごとに繰り返されます。Ctrl+C(KeyboardInterrupt)を捕捉して、スクリプトを優雅に終了させることができます。
.. code-block:: python
try:
# Continuously monitor the state of the touch sensor and control LEDs accordingly
while True:
if touch_sensor.is_pressed: # Check if the touch sensor is pressed
print('You touch it!') # Output message indicating sensor activation
led1.off() # Turn off LED1
led2.on() # Turn on LED2
else: # If the sensor is not pressed
led1.on() # Turn on LED1
led2.off() # Turn off LED2
sleep(0.5) # Pause for 0.5 seconds before rechecking the sensor state
except KeyboardInterrupt:
# Handle a keyboard interrupt (Ctrl+C) for a clean exit from the loop
pass