Blue light in the morning and red light in the evening can help us adopt a more natural circadian rhythm. Blue light is stimulating and ideal to be exposed to early in the day while the removal of blue light allows melatonin production to ramp up so red light in the evening can help get us ready for bed. The Raspberry Pi is the perfect device to help with the timing and driving of long NeoPixel strips. In this post we will show how just a few wires, some code, and a tiny bit of hardware, we can have a light with some circadian awareness.

Parts

Angled shot of Raspberry Pi 3 - Model B+
The Raspberry Pi 3 Model B is the most popular Raspberry Pi computer made, and the Pi Foundation knows you can always make a good thing better! And what could make the Pi 3...
$35.00
In Stock
Adafruit NeoPixel Digital RGB LED Strip wired to a microcontroller, with all the LEDs in a rainbow
We crammed ALL THE NEOPIXELS into this strip! An unbelievable 144 individually-controllable LED pixels on a flexible PCB. It's completely out of control and ready for you to...
Out of Stock
Angled shot of half-size solderless breadboard with red and black power lines.
This is a cute, half-size breadboard with 400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm x 5.5cm with a standard double-strip in the...
$4.95
In Stock

Wiring

We are limited to just a few pins on the Raspberry Pi to drive NeoPixel strips (D10, D12, D18 and D21). You can see the white wire is our control line (D18) and the black is our shared ground. The other red/black wire of the NeoPixel strips can go right into the marked +/- barrel jack terminal block. That is it. We are wired up. This wiring will work on all releases to date of Raspberry Pi v1, v2, v3 and Zero.

The 10A power supply will connect to the female barrel jack and the female NeoPixel header will connect to our GND and GPIO #18 on the Cobbler Plus breadboard.

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.

# SPDX-FileCopyrightText: 2019 Mikey Sklar for Adafruit Industries
#
# SPDX-License-Identifier: MIT

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)

Configure TimeZone

Run the raspi-config command as shown below to select your Time Zone if it has not already been configured. 

  • Localisation Options
  • Change Timezone
  • Geographic Area
  • Time Zone
sudo raspi-config

Download the Code

The most convenient way to get the program on your Pi will be to use the 'wget' command from a terminal and place the script directly into your home directory.

wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Circadian_Pi_Desk_Light/Circadian_Pi_Desk_Light.py

Running the Code

The desk light program can be launched once with the following command. It will automatically change the colors based on the time values placed in the script such as:

# 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"
sudo python3 ./Circadian_Pi_Desk_Light.py

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