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.
Circuit Diagram
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.
CircuitPython Code
Make sure your Pi is running the latest version of Raspbian and has the following Adafruit blinka and neopixel libraries installed.
- Prerequisite Pi Setup
- Updates for an existing Pi
sudo apt-get update
sudo apt-get upgrade
Library installation
sudo pip3 install adafruit-blinka
sudo pip3 install adafruit-circuitpython-neopixel
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)
Run it!
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
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
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.