1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

モーションを検知したら LED を光らせる

Last updated at Posted at 2022-01-02

モーションセンサーで動きを検知したら LED を 5 秒間光らせます。

用意するもの:

  • モーションセンサー HC-SR501
  • LED
  • 1kΩ の抵抗

配線:

GPIO 20 <-> 1kΩ <-> LED <-> GND
モーションセンサー (HC-SR501) は写真左から Raspberry Pi の GND (オリーブ色?), GPIO 21 (赤), 5V (青) へ接続

IMG_0469.jpg

以下のようなプログラムで動くはずです。

import RPi.GPIO as GPIO, time, datetime

# GPIO Set Up
GPIO.setmode(GPIO.BCM)

motionPin = 21
GPIO.setup(motionPin, GPIO.IN)

ledPin = 20
GPIO.setup(ledPin, GPIO.OUT)

interval = 5

while True:
    try:
        # if a motion is detected, the LED is ON for 5 seconds.
        if GPIO.input(motionPin) == GPIO.HIGH:
            dt = datetime.datetime.now()
            print("Motion detected: " + dt.now())
            GPIO.output(ledPin, GPIO.HIGH)
            time.sleep(interval)
            GPIO.output(ledPin, GPIO.LOW)
        else:
            time.sleep(interval)

    except KeyboardInterrupt:
        break


# GPIO Clean Up
GPIO.cleanup()
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?