Daily tips and tricks from the experts at Adafruit!
View this email in your browser

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


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.

Pi Setup


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
  3. Library installation
    1. sudo pip3 install adafruit blinka
    2. sudo pip3 install adafruit-circuitpython-neopixel

Code


Go ahead and cut and paste this into your Raspberry Pi. Name the file Circadian_Pi.py. You can test and see if your hardware is working using these commands:

  • sudo python3 ./Circadian_Pi.py morning
  • sudo python3 ./Circadian_Pi.py night
  • sudo python3 ./Circadian_Pi.py off
import board
import neopixel
import sys

pi_pin = board.D18
numpix = 144
brightness = 1.0
pixels = neopixel.NeoPixel(pi_pin, numpix, brightness=brightness) # Raspberry Pi wiring!

# valid argument check and color assignment
# morning == blue
# night == red
# off == all LEDs off
if len(sys.argv) == 2:
    if sys.argv[1] == "morning" :
        color = (0, 0, 255)
    elif sys.argv[1] == "night" :
        color = (255, 0, 0)
    elif sys.argv[1] == "off" :
        color = (0, 0, 0)

    pixels.fill(color)

else:
    print("valid arguments are morning, night or off")

CRON


We can schedule our light to turn on the in the morning hours, off during most of the day and on again in the evening hours using the CRON system. This is one of the ways where using a Raspberry Pi is really powerful since it knows the correct time and can schedule jobs for us. The cron system has five fields of which we are just going to use the first two (minute and hours) which we want to the light to be turned on.

Use the following command and paste in this example crontab syntax.

  • sudo crontab -e

 

# 5am - 8am blue light mode [stimulate]

0 5 * * * python3 /home/pi/Circadian_Pi.py morning 

0 8 * * * python3 /home/pi/Circadian_Pi.py off 

# 6pm - 10pm red light mode [melatonin]

0 18 * * * python3 /home/pi/Circadian_Pi.py night 

0 22 * * * python3 /home/pi/Circadian_Pi.py off