Motion-activated light ・ 人体感应灯
PIR センサーで人を検知し、RGB LED を自動点灯します。
この構成をエディタで開く・編集する →| 部品 | 役割 | ポート | リンク・価格 |
|---|---|---|---|
| M5Stack Core2 | Controller | grove_i2c, grove_analog, grove_uart, mbus | official page — $46.9 |
| M5Stack Unit PIR | PIR motion sensor | grove_i2c | official page — $5.5 |
| Unit RGB (SK6812) | RGB LED | grove_i2c | official page — $3.95 |
M5Stack Core2とPIRユニット、RGBユニットで作る、人感センサー付き自動点灯装置です。PIRセンサーが人の動きを検知すると、RGB LEDが自動で点灯します。暗い場所での足元照明や、玄関のライトとして便利です。
# M5Stack Core2 + PIR Unit + RGB Unit (SK6812)
# Wiring: PIR Unit -> PaHub -> Core2 Port.A (I2C)
# RGB Unit -> PaHub -> Core2 Port.A (I2C)
import time
from machine import Pin, I2C
from m5stack import lcd
# I2C setup for Port.A (pins 21=SDA, 22=SCL)
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
# PIR sensor I2C address (default 0x64)
PIR_ADDR = 0x64
PIR_REG = 0x01 # register to read motion status
# RGB LED I2C address (default 0x38)
RGB_ADDR = 0x38
# Function to set RGB color (R, G, B each 0-255)
def set_rgb(r, g, b):
# SK6812 control via I2C: send 3 bytes (R, G, B)
i2c.writeto(RGB_ADDR, bytes([r, g, b]))
# Turn off LED initially
set_rgb(0, 0, 0)
lcd.clear()
lcd.print('Waiting...', 10, 20)
while True:
# Read PIR motion status
data = i2c.readfrom_mem(PIR_ADDR, PIR_REG, 1)
motion = data[0] & 0x01 # bit 0 indicates motion
if motion:
lcd.clear()
lcd.print('Motion detected!', 10, 20)
set_rgb(255, 255, 255) # white light
time.sleep(5) # keep light on for 5 seconds
set_rgb(0, 0, 0) # turn off
lcd.clear()
lcd.print('Waiting...', 10, 20)
time.sleep(0.5) # check every 0.5 seconds
この作り方はAIが構成から生成したものです(未検証)。通電前に配線とコードをご確認ください。
この構成はAIが実在するM5Stack部品から設計し、ポート互換をサーバで検証したものです。編集・共有できます。
エディタで開く →UnitKit は M5Stack® 製品向けの非公式ツール。作りたいものを書くと、部品リストと配線図が出ます。