.. note:: こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。 **参加する理由は?** - **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。 - **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。 - **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。 - **特別割引**:最新製品の独占割引をお楽しみください。 - **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。 👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう! .. _2.1.3_py: 2.1.3 タッチスイッチモジュール ================================= はじめに ------------------- このプロジェクトでは、タッチスイッチモジュールについて学びます。従来のスイッチに比べ、操作が便利で、タッチ感が優れ、制御が精密で、機械的な摩耗も少ないといった利点があります。 必要な部品 ------------------------------ このプロジェクトには、以下のコンポーネントが必要です。 .. image:: ../img/2.1.3component.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:: ../img/2.1.3circuit.png :width: 500 :align: center 実験手順 ------------------------------ **ステップ1:** 回路を組む。 .. image:: ../img/2.1.3fritzing.png :width: 700 :align: center **ステップ2:** ディレクトリを変更。 .. raw:: html .. code-block:: cd ~/raphael-kit/python/ **ステップ3:** 実行。 .. raw:: html .. code-block:: sudo python3 2.1.3_TouchSwitch.py コードが実行されている間、赤いLEDが点灯します。タッチスイッチモジュールをタップすると、黄色いLEDが点灯します。 **コード** .. note:: 下記のコードは **修正/リセット/コピー/実行/停止** が可能です。ただし、事前にソースコードのパス(例: ``raphael-kit/python`` )に移動する必要があります。コードの修正後、そのまま実行して効果を確認できます。 .. raw:: html .. code-block:: python #!/usr/bin/env python3 import RPi.GPIO as GPIO import time # Set #17 as touch switch pin, #22 as led1 pin, #27 as led2 pin touchPin = 17 led1Pin = 22 led2Pin = 27 # Define a setup function for some setup def setup(): # Set the GPIO modes to BCM Numbering GPIO.setmode(GPIO.BCM) # Set touchPin input # Set ledPin output, # and initial level to High(3.3v) GPIO.setup(touchPin, GPIO.IN) GPIO.setup(led1Pin, GPIO.OUT, initial=GPIO.HIGH) GPIO.setup(led2Pin, GPIO.OUT, initial=GPIO.HIGH) # Define a main function for main process def main(): while True: # touch switch high, led1 on if GPIO.input(touchPin) == 1: print ('You touch it!') GPIO.output(led1Pin, GPIO.LOW) GPIO.output(led2Pin, GPIO.HIGH) # touch switch low, led2 on if GPIO.input(touchPin) == 0: GPIO.output(led2Pin, GPIO.LOW) GPIO.output(led1Pin, GPIO.HIGH) time.sleep(0.5) # Define a destroy function for clean up everything after # the script finished def destroy(): # Turn off LED GPIO.output(led1Pin, GPIO.HIGH) GPIO.output(led2Pin, GPIO.HIGH) # Release resource GPIO.cleanup() # If run this script directly, do: if __name__ == '__main__': setup() try: main() # When 'Ctrl+C' is pressed, the program # destroy() will be executed. except KeyboardInterrupt: destroy() **コード説明** .. code-block:: python touchPin = 17 led1Pin = 22 led2Pin = 27 ``touchPin`` 、 ``led1Pin`` 、 ``led2Pin`` はそれぞれGPIO17、GPIO22、GPIO27、つまりBCM17、BCM22、BCM27に接続されています。 .. code-block:: python GPIO.setmode(GPIO.BCM) GPIO.setup(touchPin, GPIO.IN) GPIO.setup(led1Pin, GPIO.OUT, initial=GPIO.HIGH) GPIO.setup(led2Pin, GPIO.OUT, initial=GPIO.HIGH) GPIOモードをBCMナンバリングに設定。 ``led1Pin`` 、 ``led2Pin`` を出力モードにし、初期レベルをHigh(3.3V)に設定します。 .. code-block:: python # touch switch high, led1 on if GPIO.input(touchPin) == 1: print ('You touch it!') GPIO.output(led1Pin, GPIO.LOW) GPIO.output(led2Pin, GPIO.HIGH) # touch switch low, led2 on if GPIO.input(touchPin) == 0: GPIO.output(led2Pin, GPIO.LOW) GPIO.output(led1Pin, GPIO.HIGH) タッチスイッチモジュールをタップすると、 ``touchPin`` がHighになり、led1が点灯し、「You touch it!」と表示されます。 ``touchPin`` がLowの場合、led2が点灯します。 現象の画像 ------------ .. image:: ../img/2.1.3touch_switch_module.JPG :width: 500 :align: center