This previous vacation season, I made a decision so as to add some additional pleasure to our home by organising a DIY mild show. I used a Raspberry Pi, a programmable mild string, and Python.
You can arrange your individual mild show for any event, due to the pliability of the WS12911/2 (or NeoPixel) system, by following these instructions.
Prerequisites
You will want:
- 1 – Raspberry Pi with headers and an Ethernet or WiFi connection. I used a Raspberry Pi Zero W with headers.
- 1 – WS12811/2 mild string. I used the Alitove WS2811 Addressable LED Pixel Light 50, however many different varieties can be found. Adafruit manufacturers these as NeoPixel.
- 1 – 5v/10A AC-DC power supply for WS12811 should you use the Alitove. Other lights could include an influence provide.
- 1 – Breadboard
- 2 – Breadboard-to-Pi-header jumper wires. I used blue for the Pi GPIO pin 18 and black for the Pi floor.
- 1 – 74AHCT125 stage converter chip to soundly transmit Pi GPIO wire alerts to 5v/10An influence with out feeding again to the Pi.
- eight – Breadboard-to-breadboard jumper wires or solid-core 24 AWG wires. I used purple/orange for 5v energy, black for floor, and yellow for information.
- 1 – SD card with Raspberry Pi OS put in. I used Raspberry Pi OS Lite and set it up in a headless mode with SSH enabled.
What are WS2811/2 programmable LEDs?
The WS2811/2 class of programmable lights integrates purple, inexperienced, and blue LED lights with a driver chip right into a tiny surface-mounted bundle managed via a single wire.
Each mild might be individually programmed utilizing an RGB set of integers or hex equivalents. These lights might be packaged collectively into matrices, strings, and different type elements, and they are often programmatically accessed utilizing an information construction that is sensible for the shape issue. The mild strings I take advantage of are addressed utilizing an ordinary Python checklist. Adafruit has an awesome tutorial on wiring and controlling your lights.
Control NeoPixel LEDs with Python
Adafruit has created a full suite of Python libraries for many of the components it sells. These are designed to work with CircuitPython, Adafruit’s port of Python designed for low-cost microcontroller boards. You don’t want to put in CircuitPython on the Raspberry Pi OS as a result of the preinstalled Python 2 and Python three are appropriate.
You might want to pip3
to put in libraries for Python three. Install it with:
sudo apt-get set up python3-pip
Then set up the next libraries:
Once these libraries and their dependencies are put in, you’ll be able to write code like the next to program a number of lights wired to your Raspberry Pi utilizing sudo python3
(sudo is required):
import board
import neopixel
num_lights = 50
# program 50 lights with the default brightness 1.zero, and autoWrite true
pixels = neopixel.NeoPixel(board.D18, num_lights)
# mild 20 vibrant inexperienced
pixels[19] = (zero,255,zero)
# mild all pixels purple
pixels.fill((255.zero,zero))
# flip off neopixels
pixels.fill((zero,zero,zero))
Set up your lighting system
- Install the SD card into the Raspberry Pi and safe it, the breadboard, and lights where they need to be (velcro works for the Pi and breadboard).
- Install the 74AHCT125 stage converter chip, mild, energy provide, and Pi in line with this schematic:
- String extra lights to the primary mild utilizing their connectors. Note the full variety of lights.
- Plug the ability provide into the wall.
- Plug the Raspberry Pi energy provide into the wall, and await it besides.
Install the sunshine controller and Flask net software
I wrote a Python software and library to work together with the lights and a Flask net software that runs on the Pi. See my Raspberry Pi Neopixel Controller GitHub repository for extra details about the code.
The lib.neopixc library
The lib.neopixc
library extends the neopixel.NeoPixC
class to work with two 50-light Alitove mild strands related in serial, utilizing a programmable checklist of RGB colours lists. It provides the next features:
set_color
: Takes a brand new checklist of lists of RGB coloursstroll
: Walks via every mild and units them to the colours so asrotate
: Pushes the final coloration within the checklist of lists to the start of the checklist of lists for blinking the lights
If you might have a unique variety of lights, you’ll need to edit this library to alter the self._num_lights
worth. Also, some lights require a unique argument within the order constructor attribute. The Alitove is appropriate with the default order attribute neopixel.GRBW
.
The run_lights.py script
The run_lights.py
script makes use of lib.neopixc
to help a colours file and a state file to dynamically set how the lights behave at any time. The colours file is a JSON array of arrays of RGB (or RGBW) integers that’s fed as the colours to the lib.neopixc
object utilizing its set_colors
methodology. The state file can maintain certainly one of three phrases:
static
: Does not rotate the lights with every iteration of the whereas loopblink
: Rotates the lights with every iteration of the principle whereas loopdown
: Turns all of the lights off
If the state file doesn’t exist, the default state is static
.
The script additionally has HUP and INT sign handlers, which is able to flip off the lights when these alerts are obtained.
Note: Because the GPIO 18 pin requires sudo on the Raspberry Pi to work, the run_lights.py
script have to be run with sudo.
The neopixel_controller software
The neopixel_controller
Flask software, within the neopix_controller listing of the github repository (see under), provides a front-end browser graphical person interface (GUI) to regulate the lights. My raspberry pi connects to my wifi, and is accessible at raspberrypi.native. To entry the GUI in a browser, go to http://raspberrypi.local:5000. Alternatively, you should use ping to search out the IP tackle of raspberrypi.native, and use it because the hostname, which is helpful when you’ve got a number of raspberry pi units related to your wifi.
The present state and three front-end buttons use JavaScript to work together with a set of REST API endpoints offered by the Flask software:
/api/v1/state
: Returns the present state of the shared state file, which defaults tostatic
if the state file doesn’t exist/api/v1/blink
: Sets the state file to blink/api/v1/static
: Sets the state file to static/api/v1/down
: Sets the state file to down
I wrote two scripts and corresponding JSON definition recordsdata that launch run_lights.py
and the Flask software:
launch_christmas.sh
launch_new_years.sh
These might be launched from a command-line session (terminal or SSH) on the Pi after it’s arrange (they don’t require sudo, however use sudo internally):
./launch_christmas.sh
You can flip off the lights and cease run_lights.sh
and the Flask software by utilizing lights_down.sh
.
The code for the library and the flask software are within the Raspberry Pi Neopixel Controller GitHub repository.