For those late nights in my office when I’m staring at SQL/Python scripts all day, walking back to the bedroom at night is a task done completely in the dark. Since we recently encountered a 3.5’ snake in the house, I’m a little hesitant to continue walking around blindly.
This was a quick weekend project that not only adds a boost to our quality of life score, but these lights can be customized based on a holiday or event. I’m thinking about doing a movie theater chasing lights theme automatically for Family Movie & Video Game Night™. It’ll add an extra special flair for the kids when it’s time to grab popcorn and snacks. [edit: added and updated below]
The build only took about an hour from start to finish. I soldered a Raspberry Pi Pico W to a string of LEDs on leftover aluminum channels from the Tron Lights project. The only difference is I’m not using WLED to control them. Other than grabbing the current network time from the built-in Wi-Fi, it’s a set-it-and-forget-it with no third party app or data harvesting company in the middle.
I did, however, install an open source desktop app called Thonny that takes care of the more tedious aspects of dealing with an RP2040. I’d highly recommend you do the same.
It’s recommended to power the LEDs directly to a 5v power supply, but if you’re not going full white (255,255,255) brightness for a long distance, there won’t be any drops in current that cause your lights to dim or lose color. Also, make sure you solder your LEDs to the following pinout if you’re using the same led_pin as the script below:
GP0 (Pi Pin 1) > LED Data Pin
VBUS (Pi Pin 40) > LED Power
GND (Pi Pin 38) > LED Ground
Note: The neopixel library is included by default in the Pico firmware, but not the 2040 firmware.
main.py
import network
import ntptime
import utime
from machine import Pin, Timer
import neopixel
ssid ='WIFI' password ='PASSWORD' NUM_LEDS =230 led_pin = Pin(0, Pin.OUT)
strip = neopixel.NeoPixel(led_pin, NUM_LEDS)
seconds_delay =600defset_led_color(strip, r, g, b):
for i in range(NUM_LEDS):
strip[i] = (r, g, b)
strip.write()
defconnect_to_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
print('Connecting to network...', end='')
whilenot wlan.isconnected():
print('.', end='')
utime.sleep(1)
print('\nConnected to', ssid)
print('IP address:', wlan.ifconfig()[0])
defget_ntp_time():
try:
ntptime.settime()
current_time = utime.time()
current_time +=-7*3600 adjusted_time = utime.localtime(current_time)
print("Current time:", adjusted_time)
return adjusted_time
exceptExceptionas e:
print('Failed to get NTP time:', e)
returnNonedefwheel(pos):
if pos <85:
return (pos *3, 255- pos *3, 0)
elif pos <170:
pos -=85return (255- pos *3, 0, pos *3)
else:
pos -=170return (0, pos *3, 255- pos *3)
defwave_effect(strip, wait_ms=50, duration_s=600):
start_time = utime.time()
while utime.time() - start_time < duration_s:
for j in range(256):
for i in range(NUM_LEDS):
strip[i] = wheel((i + j) &255)
strip.write()
utime.sleep_ms(wait_ms) # Speeddefmain():
connect_to_wifi(ssid, password)
whileTrue:
current_time = get_ntp_time()
if current_time isNone:
utime.sleep(60) # Retry in a minute if NTP time fetch failedcontinue current_hour = current_time[3]
current_minute = current_time[4]
current_day = current_time[6]
# Saturday nights between 18:00 and 21:00if current_day ==5and18<= current_hour <21:
wave_effect(strip)
elif current_hour >=19or current_hour <=7:
set_led_color(strip, 7, 7, 7) # White at very low brightness utime.sleep(seconds_delay)
else:
set_led_color(strip, 0, 0, 0)
utime.sleep(seconds_delay)
if __name__ =='__main__':
main()