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.
- Half-size breadboard
- Raspberry Pi 3 – Model B+
- Pi Cobbler Plus or Pi T-Cobbler Plus
- NeoPixel Digital RGB LED Strip
- Female DC Power adapter
- 5v 10A switching power supply
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.
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
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 == "morning" : color = (0, 0, 255) elif sys.argv == "night" : color = (255, 0, 0) elif sys.argv == "off" : color = (0, 0, 0) pixels.fill(color) else: print("valid arguments are morning, night or off")
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