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
- 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
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.
- 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
-
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
I can see using the Pi Cobbler for complex circuits, and even simple ones if you design a lot of Pi connected circuits and/or simply have it lying around. But why would anyone want to spend seven or eight dollars for a something that’s already tested and running, and requires only two wires?
Chris – You are correct that the cobbler is overkill for this circuit. However, it makes the pin locations easy to convey and allows for easy expandability. If you wanted to add a trimpot to control brightness and some buttons to switch into different modes it would be convenient to hook them up. Those who know they don’t need a cobbler can skip that component.