注釈
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
参加する理由は?
エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。
独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。
特別割引:最新製品の独占割引をお楽しみください。
祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[ここ]をクリックして今すぐ参加しましょう!
2.1.3 タッチスイッチモジュール¶
はじめに¶
このプロジェクトでは、タッチスイッチモジュールについて学びます。これは従来のスイッチを置き換えるもので、操作が便利で、タッチ感が良く、制御が正確で、機械的な摩耗が最小限に抑えられるという利点があります。
必要な部品¶
このプロジェクトには、次のコンポーネントが必要です。
一式を購入するのが便利です、こちらがリンクです:
名前 |
このキットのアイテム |
リンク |
---|---|---|
Raphael Kit |
337 |
以下のリンクから別々に購入することもできます。
コンポーネントの紹介 |
購入リンク |
---|---|
回路図¶
実験手順¶
ステップ 1:: 回路を組み立てる。
ステップ 2: ディレクトリを変更する。
cd ~/raphael-kit/python-pi5
ステップ 3: 実行する。
sudo python3 2.1.3_TouchSwitch_zero.py
コードが実行されている間、赤いLEDが点灯します。タッチスイッチモジュールをタップすると、黄色いLEDが点灯します。
コード
注釈
下記のコードを 変更/リセット/コピー/実行/停止 することができます。しかし、その前に raphael-kit/python-pi5
のようなソースコードのパスに移動する必要があります。コードを変更した後、直接実行して効果を見ることができます。
#!/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
をインポートします。#!/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を初期化します。
# 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)を捕捉して、スクリプトを優雅に終了させることができます。
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