Raspberry Pi Controlled NeoPixel Christmas Tree Lights

1.3K52

Intro: Raspberry Pi Controlled NeoPixel Christmas Tree Lights

Decided to change up the Christmas Tree this year with some customisable lights. This idea come from the difference in opinion between my wife and I regarding light colours on our tree. She is a fan of single colours (or just white) to go with a theme where I like the traditional red, green and blue. So now we get the best of both worlds, and the kids love picking a new colour every night.

This instructable does not touch on home network security, ensure you use passwords and your network is appropriately protected from external intrusion.

Credit to all the great open source contributions which I have linked throughout the instructable, they made this incredibly easy for me to complete in a short amount of time without any formal IT background.

If you come up with any new modes for the lights please add them in the comments, I will be working on a twinkle function next.

STEP 1: Set Up Raspberry Pi

Conduct the following in order to set up your Pi, install Node-RED and the required packages.

  • Set up your Pi, if you are running a headless setup make sure you enable SSH and/or VNC.
  • Access you your pi via your preferred means (SSH is the easiest at this point on a headless set up)
  • Update your Pi
  • Install Node-RED with the following command:
				bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
  • Set Node-RED to auto start on boot up:
sudo systemctl enable nodered.service
  • Install Unicorn HAT drivers (allows the Pi to talk to our NeoPixels) with the following command:
curl -sS get.pimoroni.com/unicornhat | bash
  • Once Unicorn HAT is installed it will change your audio setting and require a restart, do this.

Note: If any of the above commands do not run due to permissions, add "sudo" to the front of the command and enter your password if prompted.

STEP 2: Set Up Node-RED

Now that your Pi is set up and we should have a working Node-RED Server, we can navigate to our server via the Pi it is installed on (http://localhost:1880) or any computer on your local network. When browsing from another machine you should use the hostname or IP-address of the Pi: http://<hostname>:1880. You can find the IP address by running hostname -I on the Pi.

We now need to add some modules to allow us to construct our flow, interact with it and talk to our LEDs. The two we are going to add are:

if you prefer to add these via the command line simply follow the links to find the command, just make sure you are in the correct directory so Node-RED can find them. alternatively, simply add them via the UI on the web page you just navigated to:

  • click the "Menu" button ( three horizontal bars) in the top right corner
  • select "Manage palette"
  • select the "Install" tab
  • enter 'node-red-dashboard" into the search field
  • select "install" on what should be the top option
  • once complete, enter "node-red-node-pi-neopixel"
  • select "install" on what should be the only available option
  • select "Close"

Node-RED should now have all the required modules to run our LEDs and User Interface. We can now add the flow which defines the inputs and the outputs between the use, the Pi and the LEDs.

  • click the "Menu" button ( three horizontal bars) in the top right corner
  • select "Import"
  • Past the following into the box which should now be open:
[{"id":"85fce9c6579baaa0","type":"tab","label":"Christmas Lights Simple","disabled":false,"info":"","env":[]},{"id":"cc28a93a30fc342a","type":"ui_colour_picker","z":"85fce9c6579baaa0","name":"Color Picker","label":"COLOR PICKER","group":"d2ace9cc.9f3998","format":"rgb","outformat":"string","showSwatch":true,"showPicker":true,"showValue":true,"showHue":false,"showAlpha":false,"showLightness":true,"square":"false","order":1,"width":0,"height":0,"passthru":true,"topic":"","x":370,"y":360,"wires":[["024561a721d6f79c"]]},{"id":"024561a721d6f79c","type":"function","z":"85fce9c6579baaa0","name":"Set Color","func":"var count = global.get('count')||0;\nmsg.payload = msg.payload.replace(/[rgb()\\s]/g,\"\");\nif(count===0){\n    msg.payload = msg.payload;\n}\nelse{\n    msg.payload = (count-1) + \",\" + msg.payload;\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":360,"wires":[["c0297b03d780b3ae"]]},{"id":"693df877b38a2cf1","type":"ui_button","z":"85fce9c6579baaa0","name":"Clear","group":"d2ace9cc.9f3998","order":6,"width":0,"height":0,"passthru":false,"label":"CLEAR","tooltip":"","color":"","bgcolor":"","icon":"","payload":"0,0,0","payloadType":"str","topic":"","x":190,"y":460,"wires":[["a96f4433de81777e","663a12cf6cede0ef"]]},{"id":"206d8115b7db9943","type":"ui_dropdown","z":"85fce9c6579baaa0","name":"Select Strip Color","label":"SELECT STRIP COLOR","tooltip":"","place":"SELECT STRIP COLOR","group":"d2ace9cc.9f3998","order":4,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"RED","value":"255,0,0","type":"str"},{"label":"GREEN","value":"0,255,0","type":"str"},{"label":"BLUE","value":"0,0,255","type":"str"},{"label":"WHITE","value":"255,255,255","type":"str"},{"label":"PURPLE","value":"255,0,255","type":"str"},{"label":"CYAN","value":"0,255,255","type":"str"},{"label":"YELLOW","value":"255,255,0","type":"str"}],"payload":"","topic":"","topicType":"str","className":"","x":370,"y":420,"wires":[["c0297b03d780b3ae"]]},{"id":"9d8f5d23eeba3096","type":"delay","z":"85fce9c6579baaa0","name":"","pauseType":"rate","timeout":"1","timeoutUnits":"seconds","rate":"4","nbRateUnits":"0.0101","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":710,"y":560,"wires":[["c0297b03d780b3ae"]]},{"id":"c0297b03d780b3ae","type":"rpi-neopixels","z":"85fce9c6579baaa0","name":"WS2812B LED STRIP","pixels":"250","bgnd":"","fgnd":"","wipe":"0","mode":"pixels","rgb":"grb","brightness":"40","gamma":true,"x":960,"y":400,"wires":[]},{"id":"40080ab07f9975a1","type":"ui_button","z":"85fce9c6579baaa0","name":"Christmas","group":"d2ace9cc.9f3998","order":5,"width":0,"height":0,"passthru":true,"label":"Christmas","tooltip":"","color":"","bgcolor":"","icon":"","payload":"true","payloadType":"bool","topic":"","x":200,"y":620,"wires":[["5db1f6bc1f363b40"]]},{"id":"34273e36c70ad99f","type":"debug","z":"85fce9c6579baaa0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"reset","targetType":"msg","x":750,"y":700,"wires":[]},{"id":"5db1f6bc1f363b40","type":"function","z":"85fce9c6579baaa0","name":"Christmas","func":"var numberOfLEDs = 250;\n\nvar j = 0\n\nwhile (j<numberOfLEDs+1){\n        var setColor = j+','+ 255 +','+ 0 +','+ 0;\n        node.send({payload:setColor});\n        j++;\n        var setColor = j+','+ 0 +','+ 255 +','+ 0;\n        node.send({payload:setColor});\n        j++;\n        var setColor = j+','+ 0 +','+ 0 +','+ 255;\n        node.send({payload:setColor});\n        j++;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":620,"wires":[["c0297b03d780b3ae"]]},{"id":"a96f4433de81777e","type":"change","z":"85fce9c6579baaa0","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":520,"wires":[["9d8f5d23eeba3096","34273e36c70ad99f"]]},{"id":"663a12cf6cede0ef","type":"delay","z":"85fce9c6579baaa0","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":530,"y":480,"wires":[["c0297b03d780b3ae"]]},{"id":"d2ace9cc.9f3998","type":"ui_group","name":"Christmas tree","tab":"14b7bf29.77ead1","order":1,"disp":true,"width":"6","collapse":false},{"id":"14b7bf29.77ead1","type":"ui_tab","name":"Lights","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
  • select "Import"
  • you should now see the flow shown in the picture
  • select "Deploy" in the top right corner
  • open a new tab and navigate to the UI "http://<hostname>:1880/ui" (replace <hostname> with you Pi's IP address) you should now see the image of the user interface and interact with it.

Note: This code is set for the light intensity to be at 40%, this limits power drain for the PSU I am using and is a nice level for the LED's that I am using. If you want to adjust this just double click on the rpi-neopixels node at the end of the flow and you will see the box to adjust.

STEP 3: Wiring

Ensure you Pi is tuned off prior to connecting any wires! Wiring to the Pi is very straight forward, you only need to connect the ground and the signal cable from the LEDs. make sure you connect the correct cables though, my set were coloured:

  • Vin - Red
  • Ground - White - Pin 6 on the Pi
  • Signal - Green - Pin 12 (GPIO 18) on the Pi (this is why your audio needs to be HDMI only on the Pi as audio via GPIO uses the same pin)

Once you have connected the Pi you can connect your PSU to the LED strip. depending how long your LED strip is you may want to wire in the PSU at multiple point along the strip (hide it at the back of the tree).

Please make sure all exposed wires are insulated with electrical tape at a minimum and place everything in some kind of container to stop the kids getting into it.

Once you are all wired up you can go ahead and supply power to the Pi and the lights. Once the Pi has booted up, navigate to "http://<hostname>:1880/ui" (replace <hostname> with you Pi's IP address) from you device of choice. once there simply press any where on the colour wheel or hit the Christmas button and your lights should come alive!

I have saved a shortcut to the UI to both my wife's and my phone home screens for easy access.

Comments