Pi Config

Make sure your Pi is running the latest version of Raspbian and has the following Adafruit blinka and neopixel libraries installed.

  1. Prerequisite Pi Setup
  2. Updates for an existing Pi
    1. sudo apt-get update
    2. sudo apt-get upgrade

Library installation

  1. sudo pip3 install adafruit-blinka
  2. sudo pip3 install adafruit-circuitpython-neopixel

Code

This script is intended to turn on blue light in the morning for a four hour window, then turn the lights off for most of the day. In the evening, it will turn on red lights for a four hour window and then the LEDs will be turned off for the night. You can customize the timing, color and duration by modifying this script.

import datetime
import time
import board
import neopixel

pi_pin = board.D18
numpix = 144
brightness = 1.0
pixels = neopixel.NeoPixel(pi_pin, numpix, brightness=brightness)

# morning BLUE light hours
# BLUE light is stimulating
start_morning = "06:00:00"
end_morning = "10:00:00"

# evening RED light hours
# RED light is calming allows melatonin production to increase
start_night = "18:00:00"
end_night = "22:00:00"

color_change = False

while True:
    date_string = datetime.datetime.now().strftime("%H:%M:%S" )

    if date_string == start_morning:
        color = (0, 0, 255)
        color_change = True

    elif date_string == end_morning:
        color = (0, 0, 0)
        color_change = True

    elif date_string == start_night:
        color = (255, 0, 0)
        color_change = True

    elif date_string == end_night:
        color = (0, 0, 0)
        color_change = True

    else:
        time.sleep(1)

    # update neopixel strip with new colors
    if color_change:
        pixels.fill(color)
        color_change = False
        time.sleep(1)

This guide was first published on Feb 18, 2019. It was last updated on Feb 18, 2019.

This page (CircuitPython Code) was last updated on Mar 16, 2021.

Text editor powered by tinymce.