注釈
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
参加する理由は?
エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。
独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。
特別割引:最新製品の独占割引をお楽しみください。
祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[ここ]をクリックして今すぐ参加しましょう!
2.1.5 傾斜スイッチ¶
はじめに¶
これは内部に金属のボールが入ったボール傾斜スイッチです。小さな角度の傾斜を検出するために使用されます。
必要な部品¶
このプロジェクトには、次のコンポーネントが必要です。
一式を購入するのが便利です、こちらがリンクです:
名前 |
このキットのアイテム |
リンク |
---|---|---|
Raphael Kit |
337 |
以下のリンクから別々に購入することもできます。
コンポーネントの紹介 |
購入リンク |
---|---|
- |
回路図¶
実験手順¶
ステップ1: 回路を組み立てます。
ステップ2: ディレクトリを変更します。
cd ~/raphael-kit/python-pi5
ステップ3: 実行します。
sudo python3 2.1.5_Tilt_zero.py
傾けて垂直に置くと、緑のLEDが点灯します。傾けると、画面に「傾けました!」と表示され、赤いLEDが点灯します。再び垂直に置くと、緑のLEDが点灯します。
コード
注釈
以下のコードを 変更/リセット/コピー/実行/停止 できます。ただし、それに先立ち、 raphael-kit/python_5
のようなソースコードのパスに移動する必要があります。コードを変更した後、効果を直接確認するために実行できます。
#!/usr/bin/env python3
from gpiozero import LED, Button
# Initialize the Button for the tilt sensor and LEDs using GPIO Zero
TiltPin = Button(17, pull_up=False) # Tilt sensor connected to GPIO pin 17, pull-up resistor disabled
green_led = LED(27) # Green LED connected to GPIO pin 27
red_led = LED(22) # Red LED connected to GPIO pin 22
def detect():
"""
Detect the tilt sensor state and control the LEDs.
Turns on the red LED and turns off the green LED when tilted.
Turns off the red LED and turns on the green LED when not tilted.
"""
if TiltPin.is_pressed: # Check if the sensor is tilted
print(' *************')
print(' * Tilt! *')
print(' *************')
red_led.on() # Turn on red LED
green_led.off() # Turn off green LED
else: # If the sensor is not tilted
red_led.off() # Turn off red LED
green_led.on() # Turn on green LED
try:
while True:
# Continuously check the tilt sensor state and update LEDs
TiltPin.when_pressed = detect
TiltPin.when_released = detect
except KeyboardInterrupt:
# Handle KeyboardInterrupt (Ctrl+C) to exit the loop gracefully
pass
コードの説明
この行はスクリプトをPython 3で実行するように設定し、
gpiozero
からGPIOデバイスを制御するためにLED
とButton
をインポートします。#!/usr/bin/env python3 from gpiozero import LED, Button
プルアップ抵抗が無効化されたGPIOピン17に接続された傾斜センサーと、GPIOピン27および22に接続された2つのLEDを初期化します。
# Initialize the Button for the tilt sensor and LEDs using GPIO Zero TiltPin = Button(17, pull_up=False) # Tilt sensor connected to GPIO pin 17, pull-up resistor disabled green_led = LED(27) # Green LED connected to GPIO pin 27 red_led = LED(22) # Red LED connected to GPIO pin 22
detect
関数を定義し、傾斜センサーの状態をチェックします。傾けた場合、赤いLEDを点灯し、緑のLEDを消灯します。傾けていない場合、その逆を行います。def detect(): """ Detect the tilt sensor state and control the LEDs. Turns on the red LED and turns off the green LED when tilted. Turns off the red LED and turns on the green LED when not tilted. """ if TiltPin.is_pressed: # Check if the sensor is tilted print(' *************') print(' * Tilt! *') print(' *************') red_led.on() # Turn on red LED green_led.off() # Turn off green LED else: # If the sensor is not tilted red_led.off() # Turn off red LED green_led.on() # Turn on green LED
メインループは傾斜センサーの
when_pressed
とwhen_released
イベントにdetect
関数を割り当てます。try-except
ブロックは優雅な終了のためにキーボード割り込みを処理します。try: while True: # Continuously check the tilt sensor state and update LEDs TiltPin.when_pressed = detect TiltPin.when_released = detect except KeyboardInterrupt: # Handle KeyboardInterrupt (Ctrl+C) to exit the loop gracefully pass