NeoPixels, DotStars and Raspberry Pi

We took the family away to Camp Bestival this summer (as we do most years) and as we have three young children we took a festival trolley with us to help move the youngest child and associated paraphernalia around with us. Each year we try to decorate the trolley a little bit to brighten it up and this year I thought I would use a 1 m strip of 144 NeoPixels I had at work which were sat around not being used!

Having used these in class before I knew that they were sometimes a tricky beast to get working with a Raspberry Pi, but also having recently completed a chapter in my book using a similar DotStar strip to create a LED matrix across the front of a laptop bag; I felt sure that I could get a portable solution up and running for the NeoPixels and a Raspberry Pi. Now this solution is perhaps more involved than how you could get things going in the lab; but to get it working for our festival trolley the whole solution needed to be portable and powered from battery packs.

I decided to make use of an AdaFruit Perma-Proto Pi Hat I had lying around, as this meant the associated electronics I would require for this build could be easily added and removed from the Raspberry Pi for future projects. I also decide that I would build my Hat so that it could be used for both portable NeoPixel and DotStar projects. Here is a Fritzing diagram of the completed Perma-Proto Hat and connection to our portable USB power pack and NeoPixel strip.


Now I appreciate that this is quite a bit to take in in one go, so let’s break it down step by step…

Table of Contents

Raspberry Pi and Perma-Proto Setup

First of all we need to get our Raspberry Pi software up and running. As this is going to be run headless there is no point using the full Raspbian operating system, so head over here and grab yourself the latest version of Raspbian Lite.

I would recommend you use Etcher to copy the downloaded image onto your SD Card; it just makes everyone’s life easier! 😉

Raspbian Tweaks

So now that we have our SD Card we need to do the following:

  1. Enable SSH
  2. Add our WiFi network
  3. Change the Hostname and password
  4. Install our required software

For items 1 and 2 just follow the links and then head back over here…

Change the Hostname and password

I always like to change the hostname of my Raspberry Pi projects as this makes it easier to connect to it over WiFi without having to discover the IP address of your Pi. Changing the password will get rid of the nag screen when you log in and is really a sensible thing to do!

Connect to your Raspberry Pi, either by SSH or attaching a keyboard and monitor to it and type:

sudo raspi-config

From the menu which opens, select option 1 “Change user password” and enter your password twice. Back on the raspi-config menu, select option 2 “Network options” and from the next menu select option N1 “Hostname”. Pay attention to the restrictions for your new hostname and enter your chosen name. I am going to call this one “pixelpi”.

We now need to enable the SPI interface so that we can use DotStar LED strips with our Pi. Select option 5 “Interfacing options” from the menu and from the next menu select option P4 “SPI”, answering “Yes” to enable it.

Press tab twice to highlight the “Finish” option and press “Enter”. Answer “Yes” to reboot your Pi now.

When your Raspberry Pi reboots you should be able to connect to it via SSH by using your chosen hostname appended with “.local” for example:

ssh pi@pixelpi.local

Install our required software

We are now going to update our software and install the additional packages we will need. First check everything is up to date by issuing this command:

sudo apt-get update && sudo apt-get dist-upgrade -y

This might take some time, so go make a cup of tea!

For our shutdown button we will need the GPIOZero python library and we will also need a couple of other bits for the DotStar and NeoPixel software, so enter this command:

sudo apt-get install python3-gpiozero build-essential python-dev git scons swig -y

We are now going to add the libraries for the NeoPixels and DotStars. We will install the NeoPixel library first by running these commands:

cd ~
git clone https://github.com/jgarff/rpi_ws281x.git
cd rpi_ws281x
scons

After running the scons command you should see that the library has successfully compiled. We can now install the python library by issuing these two commands:

cd python
sudo python setup.py install

Now we are going to get the DotStar library by entering these commands:

cd ~
git clone https://github.com/adafruit/Adafruit_DotStar_Pi

We should now have all the software we need for our LEDs!

Perma-Proto Setup

Before we begin adding any components to the Perma-Proto board, solder the female GPIO header block provided to the underside of the board. This will allow us to attach and detach the Hat easily from our Pi.

Shutdown button

As we will be running our Raspberry headless, we need a way to safely shut it down once we have got it all running. For that we are going to wire a tactile button, LED and resistor to our Perma-Proto Pi Hat and the write a short Python program which will initiate a shutdown of the Raspberry Pi when this button is held for three seconds. Finally we will ensure that this Python program starts as soon as the Raspberry Pi is switched on.

The Wiring

I used the following components for this:

  1. 3mm tactile switch from Pimoroni
  2. 330 ohm resistor
  3. Red LED

I then soldered these onto the Perma-Proto Hat as shown below. I used solid-core copper cable to connect the LED to GPIO 20 and the tactile button to GPIO 21. Feed the components through the top of the Hat and solder them from the underneath, snipping any excess cable or wire from the resistor, LED and cable with side-snips once the solder joint is cold.

The Python

Our program will make use of the GPIO Zero library to wait until we press and hold the button for three seconds. After we hold the button for three seconds the LED will flash on and off three times and then initial a safe shutdown sequence of our Pi.

Copy this code into a new file saved as: “/home/pi/shutDownPi.py

Auto-start and testing

We are now going to make our shutDownPi.py file executable and ensure it runs as soon as we start up our Pi. First off make your Python file executable by typing:

sudo chmod +x /home/pi/shutDownPi.py

Now we create a systemd service definition file to auto-start the Python file when our Pi boots up. Type the following to open a new file in the nano text editor:

sudo nano /lib/systemd/system/shutDownPi.service

and now type the following into your nano text editor:

[Unit]
Description=Shutdown Pi Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/home/pi/shutDownPi.py

[Install]
WantedBy=multi-user.target

Now save and exit nano by typing Ctrl + o, followed by Enter and then Ctrl + x. Now we change the file permissions, reload the systemd daemon and activate our service by typing:

sudo chmod 644 /lib/systemd/system/shutDownPi.service
sudo systemctl daemon-reload
sudo systemctl enable shutDownPi.service

Now we need to check that this works as expected. Type sudo reboot to restart your Pi. When it is restarted, log back on and run the following command to check the status of your systemd service:

sudo systemctl status shutDownPi.service -l

You should see a message saying that the service is started. If not go back and check everything and try again! If all is well, press and hold your button for three seconds and you should see your LED flash three times and then your Pi will shutdown.

Now we have that all working we can move onto the next stage!

Voltage Level-Shifter

Both the NeoPixels and DotStars are rated as 5 V devices, yet the Raspberry Pi GPIO pins run on a 3.3 V logic. When powering your DotStar and NeoPixel devices from mains you can often get away with running them directly from the Raspberry Pi’s 3.3 V GPIO pins, however as we are going to be running the LEDs from a portable battery pack I have found it is better and more reliable to make use of a voltage level-shifter. This small chip will take an input at one voltage and step it up or down to match the voltage with which you power the chip. We will be making use of the AdaFruit 74AHCT125 – Quad Level-Shifter (3V to 5V) for this project.

For this section we will need:

Connect the IC Socket

First we solder the IC socket to the Perma-Proto board as shown below. Note the IC socket linked above has 16 pins yet we only need 14 for our 74AHCT125. This is fine, we just need to ensure we insert the chip the correct way once we have finished soldering; pay attention to half-moon indent on the top of the chip!

Connect our GND lines to the IC socket

Now you need to connect the following lines from your IC socket to the two GND lines of your Perma-Proto Hat. Use side snips to trim any excess cable from the underside when finished.

Connect our data lines to the IC socket

Now you need to connect the three data lines from your Pi GPIO pins into your IC socket. The NeoPixels use GPIO 18 by default and the DotStars use the MOSI and CLK pins. Each data line will need it’s logic stepping up from 3.3 V to 5 V. I have shown the routes of the cables to try and be as clear on the diagram as possible. You will want to route your cables in the neatest way you can around your hat!

Connecting our NeoPixels and DotStars

Now we have our GPIO data lines form the Pi going into the chip we need to connect them back to our NeoPixel or DotStar strip. To make this a reusable setup we are going to make use of a terminal block to enable us to easily attach and detach our NeoPixel and DotStar LED strips to our Hat.

For this section we will need:

Attach the terminal block

Solder your terminal block to your Perma-Proto Hat as shown in the diagram and then connect each pin to the relevant line of the IC chip or GND line as shown:

You may find that you need to route your cables around a little differently than the diagram to get everything to fit. Here is a picture of my finished Hat, ready to be added to my Pi and have the LED strip connected.

Create your USB power line for the LED Strip

The length of cable you will need for your power connection to your LED strip will depend on how you are going to install it. We will connect the red cable to one end of the strip and the black GND cable to the other end. Cut your lengths of the 18 AWG cables accordingly and then solder them into the DIY USB socket as shown below:

You can now glue your DIY USB socket together. I used plenty of hot-glue and wrapped it in electrical tape to be sure! Now attach the cables to the either end of your NeoPixel or DotStar LED strip as shown below. you may need to desolder the supplied cables from each end of your LED strip to do this! Note that the red VCC line goes into the input end of the strip and the black GND line comes out of the output end.

On your input end of your LED strip also solder cables to wire back to your terminal block on your Hat. You will need a VCC and GND line for both types of LED strip. The NeoPixel only requires a single data line, whilst the DotStar requires two.

NeoPixel Wiring:

DotStar Wiring:

Connect your LED strip to the Hat

Now you can connect your LED strip to your terminal block on your hat and power it up with your USB battery pack. I have a nice 10140 MAh battery pack with a 2 A and 1 A output. I am using the 2 A output for the LED strips and the 1 A output to power the Pi.

Testing our LED Strips

New that we have finished our electronics and connected our LED strip to our new hat we need to test them! With your LED strip powered from the 2 A line of your USB battery block and your Pi powered from the 1 A output, SSH into your Pi to begin testing the software libraries we downloaded right at the start.

DotStar Library

To test a DotStar LED strip follow these steps:

  1. Move into your DotStar folder:
     cd ~/Adafruit_Dotstar_Pi
  2. Open the strandtest.py file for editing:
     nano strandtest.py
  3. Find the numpixels constant and edit it to match the number of LEDs on your strip
  4. Find the strip declaration and edit it to match this:
    strip = Adafruit_DotStar(numpixels, 12000000)
  5. Save and close Nano
  6. Test your DotStar strip works:
    python strandtest.py

If your LED strip does not show the colours in the correct order (red then green then blue), then go back and edit your strip declaration to match this:

strip = Adafruit_DotStar(numpixels, 12000000, order='bgr')

You may need a different order depending upon your strip.

You are now ready to create your own DotStar python creations! Use the systemd definition example for the shutdown button to make your python program run at boot up.

NeoPixel Library

To test a NeoPixel LED strip follow these steps:

  1. Move into your NeoPixel Python examples folder:
    cd ~/rpi_ws281x/python/examples/
  2. Open the strandtest.py file for editing:
    nano strandtest.py
  3. Find the LED_COUNT constant and edit it to match the number of LEDs on your strip
  4. Save and close Nano
  5. Test your NeoPixel strip works:
    sudo python ./strandtest.py

You can now create your own NeoPixel python creation! Use the systemd definition example for the shutdown button to make your python program run at boot up.

2 thoughts on “NeoPixels, DotStars and Raspberry Pi

Leave a Reply

Your email address will not be published. Required fields are marked *