LoginSignup
1
0

More than 5 years have passed since last update.

02: Raspberry PiでスイッチのOnで何かを動かす

Last updated at Posted at 2018-04-09

はじめに

こちらは
Arduino と Raspberry Pi の違いのまとめ
の二つ目の個別記事で、Raspberry Pi で push switch の入力を検知する。

目的

push switchで何か機能を動作させるときの方法である。Arduinoのswitch制御の例は省く。ArduinoのIEDのexampleを見れば十分だろう。

hardware

  • LED は、GPIO25とGNDに接続する
  • Push Switch は、GPIO24と抵抗でpull downしてGNDに接続する

にそれぞれ接続したとき。Push Switch は、抵抗で pull down した。しかし、Raspberry Pi では、pull downもpull upもソフトウェアで設定することができるため、実際の抵抗を使う必要性はない。
ちなみに Raspberry PiのGNDのピンはたくさんアサインされている。GND のピン数は、Arduino以上に多い。

スイッチの検出のポイントはこちらである。見れば分かるのでこちらでおしまい。


        if GPIO.input(PIN_SW1) == GPIO.HIGH:

software

とりあえずswitch動作でLEDを光らせるもの。

02_LED_SW.py
import RPi.GPIO as GPIO
from time import sleep

PIN_LED1 = 25
PIN_SW1 = 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_LED1, GPIO.OUT)
GPIO.setup(PIN_SW1, GPIO.IN)

try:
    while True:
        if GPIO.input(PIN_SW1) == GPIO.HIGH:
            GPIO.output(PIN_LED1,GPIO.HIGH)
        else:
            GPIO.output(PIN_LED1,GPIO.LOW)
        sleep(0.01)

except KeyboardInterrupt:
        pass

GPIO.cleanup()

親記事

Arduino と Raspberry Pi の違い

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0