注釈

こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。

参加する理由は?

  • エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。

  • 学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。

  • 独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。

  • 特別割引:最新製品の独占割引をお楽しみください。

  • 祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。

👉 私たちと一緒に探索し、創造する準備はできていますか?[ここ]をクリックして今すぐ参加しましょう!

2.1.2 マイクロスイッチ

はじめに

このプロジェクトでは、マイクロスイッチの使用方法を学びます。マイクロスイッチは小さくて非常に敏感なスイッチで、最小限の圧力で作動します。信頼性が高く、感度が良いため、マイクロスイッチは安全装置としてよく使用されます。

例えば、何かや誰かが道を塞いでいる時にドアが閉まらないようにするなど、様々な用途で使われます。

必要な部品

このプロジェクトには、次のコンポーネントが必要です。

../_images/2.1.2_micro_switch_list.png

一式を購入するのが便利です、こちらがリンクです:

名前

このキットのアイテム

リンク

Raphael Kit

337

Raphael Kit

以下のリンクから別々に購入することもできます。

コンポーネントの紹介

購入リンク

GPIO拡張ボード

BUY

ブレッドボード

BUY

ジャンパーワイヤー

BUY

抵抗器

BUY

LED

BUY

マイクロスイッチ

-

コンデンサ

BUY

回路図

マイクロスイッチの左側のピンをGPIO17に接続し、 GPIO22とGPIO27にそれぞれLEDを二つ接続します。 そして、マイクロスイッチの動くアームを押して離すと、 二つのLEDが交互に点灯するのが見られます。

../_images/2.1.2_micro_switch_schematic_1.png ../_images/2.1.2_micro_switch_schematic_2.png

実験手順

ステップ 1: 回路を組み立てる。

../_images/2.1.2_micro_switch_circuit.png

ステップ 2: コードのフォルダに移動する。

cd ~/raphael-kit/python-pi5

ステップ 3: 実行する。

sudo python3 2.1.2_MicroSwitch_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 function for delays

# Initialize micro switch on GPIO pin 17 with the pull-up resistor disabled
micro_switch = Button(17, pull_up=False)
# Initialize LED1 connected to GPIO pin 22
led1 = LED(22)
# Initialize LED2 connected to GPIO pin 27
led2 = LED(27)

try:
    # Continuously check the state of the micro switch and control LEDs accordingly
    while True:
        if micro_switch.is_pressed:  # If the micro switch is pressed
            print('LED1 ON')  # Print a message to the console
            led1.on()       # Turn on LED1
            led2.off()      # Turn off LED2
        else:  # If the micro switch is not pressed
            print('    LED2 ON')  # Print a message to the console
            led1.off()      # Turn off LED1
            led2.on()       # Turn on LED2

        sleep(0.5)  # Pause for 0.5 seconds before checking the switch again

except KeyboardInterrupt:
    # Handle KeyboardInterrupt (Ctrl+C) to exit the loop gracefully
    pass

コード説明

  1. この行はスクリプトがPython 3で実行されるように設定します。GPIOデバイスを制御するために gpiozero から LEDButton を、遅延のために time から sleep をインポートします。

    #!/usr/bin/env python3
    from gpiozero import LED, Button  # Import LED and Button classes from gpiozero
    from time import sleep  # Import sleep function for delays
    
  2. プルアップ抵抗を無効にしてGPIOピン17に接続されたマイクロスイッチ、そしてGPIOピン22と27に接続された二つのLEDを初期化します。

    # Initialize micro switch on GPIO pin 17 with the pull-up resistor disabled
    micro_switch = Button(17, pull_up=False)
    # Initialize LED1 connected to GPIO pin 22
    led1 = LED(22)
    # Initialize LED2 connected to GPIO pin 27
    led2 = LED(27)
    
  3. メインループでは、マイクロスイッチの状態をチェックします。押されていれば、LED1が点灯し、LED2が消灯します。押されていなければ、LED1が消灯し、LED2が点灯します。このループは0.5秒ごとに繰り返されます。Ctrl+C(KeyboardInterrupt)を捕捉して、スクリプトを優雅に終了させることができます。

    try:
        # Continuously check the state of the micro switch and control LEDs accordingly
        while True:
            if micro_switch.is_pressed:  # If the micro switch is pressed
                print('LED1 ON')  # Print a message to the console
                led1.on()       # Turn on LED1
                led2.off()      # Turn off LED2
            else:  # If the micro switch is not pressed
                print('    LED2 ON')  # Print a message to the console
                led1.off()      # Turn off LED1
                led2.on()       # Turn on LED2
    
            sleep(0.5)  # Pause for 0.5 seconds before checking the switch again
    
    except KeyboardInterrupt:
        # Handle KeyboardInterrupt (Ctrl+C) to exit the loop gracefully
        pass