自宅の廊下に面した収納を食料庫にしている。 が、照明が微妙で暗い。

ダイソーで、扉の開閉で点灯する小さなセンサーライト。 ボタン電池3個で、一般的な砲弾型LEDがついて、100円。

機能はバッチリだが、暗いし、ボタン電池3個は面倒。2週間ほどで電池切れ。2週間ごとにボタン電池3個は効率悪すぎ。

ということで、センサーだけ流用してRPi Pico とLED Tape で満足いくものをDIYする。

部品

  • ダイソーの100円 LEDセンサーライト
  • LED Tape 5連 (WS2812B)
  • 単三電池 4個ケース
  • RPi Pico
  • 配線部材

配線

  • GP0 ... センサーのIN
  • GP1 ... NeoPixel 制御
  • VBUS ... 電池+
  • VSYS ... LED Tape NeoPixel 電源+

LED Tape は、WS2812B。5Vが必要なので、電池は1.5x4=6V。

VBUSから電池の+をいれて、VSYSへ。VSYSは、LED Tape の + に配線。

センサーの解析

きっと、PULL UPだろうと予想したら正解。 以下のように制御した。

センサーがくっつく(扉が閉まる)と、LED OFF。

センサーが離れる(扉が開く)と、LED ON。

プログラム

Sensor light (circuitpython) · Gistpython
import time, random
import board, neopixel
from digitalio import DigitalInOut, Direction, Pull

# ボタンの設定(GP1をプルアップ)
button = DigitalInOut(board.GP1)
button.direction = Direction.INPUT
button.pull = Pull.UP

num_leds = 5
led_contorol_pin = board.GP0
pixel = neopixel.NeoPixel(
    led_contorol_pin, num_leds, brightness=0.4, auto_write=False, pixel_order=neopixel.RGB
)
# RGB = "RGB"
# GRB = "GRB"
# RGBW = "RGBW"
# GRBW = "GRBW"

CLEAR = (0, 0, 0)  # clear (or second color)

print("Press the button to light up the LEDs!")
is_black = True
while True:
    # ボタンが離されたかチェック(プルアップなので、押されるとFalse)
    if button.value:
        print("Button release!")
        # ボタンがRELEASE(扉が開いた)ので点灯
        for i in range(num_leds):
            color = (255, 200, 200)  # 明るく点灯
            pixel[i] = color
        pixel.show()
        is_black = False
        
        time.sleep(0.5)
        # ボタンが押されるるまで待機(扉が閉まるまで)
        while button.value:
            time.sleep(0.01)
        time.sleep(0.1)  # デバウンス
    else:
        # 通常の動作
        time.sleep(1)

        if not is_black:
            for i in range(num_leds):
                pixel[i] = CLEAR
            pixel.show()
            is_black = True