Give Your Raspberry Pi a NeoPixel

Everyone loves Raspberry Pi and everyone loves NeoPixels, so let’s get a NeoPixel directly under your Raspberry Pi’s control!

20140902_144939

The Pi can set the NeoPixel to any RGB color and you can create scripts to update it up to 100 times per second.

For a few cents worth of parts (actually just one part!) and a single IO pin, you can give your Pi…

  • an animated multicolor status light
  • an i-style breathing power indicator
  • an adorable single pixel Ambilight back-light behind your tiny screen
  • a sunrise-synchronized night light

The possibilities are endless! Click on for a demo video and full instructions…

Demo Video

Make it Blink!

To get this working, all you need is…

  • a Raspberry Pi
  • a NeoPixel
  • some wire

I got my NeoPixel by snipping it off the end of a long string of NeoPixels, but you can use any kind of WS2812B NeoPixel including the new handy single pixel size.

Connect the NeoPixel to the PI like this…

neopixel rasppi connections

The NeoPixel is getting power from the Pi’s 3V3 pin and data from Pi’s P1-08 pin. The ground connection from the NeoPixel can connect to any GND pin on the Pi, I just used P1-06 because it was close. Note that you will leave the NeoPixel’s data out (DO) pin unconnected.

Once you have everything hooked up, you will need some software to drive it. I’ve written a little command-line utility called neouart that lets you set the color of the connected NeoPixel and even animate it.

To get the software, go to a command prompt on your Pi and enter the command (note that the -O needs to be capital)…

[code lang=”bash” light=”true”]sudo wget -O /usr/local/sbin/neouart https://github.com/bigjosh/NeoUart/releases/download/2/neouart
[/code]

…which will download the program file to your machine. Next, enter this command…

[code lang=”bash” light=”true”]sudo chmod a+x+u+s /usr/local/sbin/neouart
[/code]

…which will make the file executable and allow it to run with the privileges it needs to talk directly to the hardware.

Pixel On, Pixel Off

Now let’s make the pixel blink! Enter the command…

[code lang=”bash” light=”true”]neouart ffffff[/code]

…and the pixel should turn on. Next enter…

[code lang=”bash” light=”true”]neouart 000000[/code]

…and it should turn off. You can even enter…

[code lang=”bash” light=”true”]neouart 64ffffff 000000[/code]

…and it should turn on for 1 second and turn off.

To get the help page for the neouart command, we can enter…

[code lang=”bash” light=”true”]neouart –help[/code]

…which gives us…

NeoUART (c)2014, josh. See http://josh.com for more info.
Drives a NeoPixel connected to Raspberry Pi pin P1-08

Usage: neouart [-i] [-sn] [-q] [pixelspec ...]
-i WS2811 mode where colors are sent in RGB order (default GRB)
-s smoothly trnasition between colors over n hundreths of a second.
-q quiet mode.

Accepts pixelspecs in the format [DD]RRGGBB where...
[DD] is an optional duration in 1/100s of seconds, and
RRGGBB are each the 2 digit hex brightness level for
red, green, and blue respecively.

All pixelspec values are hex numbers in the range 00-ff.

Examples of pixelspecs:
000000=Black, 05FFFF=white for .05s, 800080=50% blue for 1.28s

You can specify one or more pixelspecs on the command line, or run the
program without parameters and it will read and execute pixelspecs from
stdin.

So, in our blink example above, we specified two pixelspecs – namely 64FFFFFF and 000000. (pixelspec is just a made up word for a number that tells the neouart command what color to display and optionally how long to display it.)

All the values we will be using for neouart are two digit hexadecimal numbers. In this system, 00 is the lowest value and FF is the highest. 80 is in the middle.

20140902_151727

Let’s break down the first pixelspec of 3CFFFFFF into 2 digit hex numbers. The first is 64 which gives the delay value. 64 in hex is 100 in decimal., so the delay here will be 1 second (we specify delays in 1/100s of seconds). The color part is FFFFFF, where the first two digits are the red brightness, the second two are green brightness, and the last two are the blue brightness. FF is the highest value we can specify for a color, so we have full brightness red+green+blue=  full bright white.

HTML also used 6 digit hex RGB numbers for representing a color, so you can use all the handy HTML color pickers to find pretty colors for your NeoPixel. Just leave off HTML’s starting “#” character.

The neouart command will leave the NeoPixel turned on to whatever the last color you specify, so the second pixlelspec in our blink example turns the NeoPixel off using the color 000000. This pixel spec has no delay- just a color. The color 000000 means zero brightness for red, green, and blue – which is black.

Patriotic Pixel!

Try entering this command…

[code lang=”bash” light=”true”]neouart 64ff0000 64ffffff 640000ff 000000[/code]

..and you should see the NeoPixel flash 1 second of each red, white, and blue and then turn off.

So now you should be able to make your NeoPixel do anything you can type on the command line, but what if you want to make color patterns that are too long for a command line, or you want to pipe live instructions into neouart from another running program?

Smooth Fades!

You can also have neouart smoothly fade between consecutive colors with the -s parameter.

Try entering this command…

[code lang=”bash” light=”true”]neouart -s50 64ff0000 64ffffff 640000ff 000000[/code]

…and you should see the NeoPixel fade from red to white to blue and then turn off. The transition between colors should take a half a second (1/2=50/100ths).

Pixel Panic Beacon!

If you run neouart without any parameters, then it will start reading from stdin. This is a common technique in linux. One way to use this feature is to pipe a file of pixelspecs into neouart. Try entering this command…

[code lang=”bash” light=”true”]wget https://raw.githubusercontent.com/bigjosh/NeoUart/master/demos/sos.nua%5B/code%5D

…to download the sos.nua file, and then enter…

[code lang=”bash” light=”true”]neouart <sos.nua[/code]

…to play it on your NeoPixel. You should see the <a href="http://en.wikipedia.org/wiki/SOS">universal distress call</a> displayed in red on your NeoPixel at a 20 words per minute. (At that rate, each dot is 60ms long which is 6/100’s of a second. Dashes are 3 times as long as dots.)
<h2>Real-time Pixel Load Indicator One Liner!</h2>
Finally, for extra bonus round, try entering this command…

[code lang="bash" light="true"]top -bd.2|awk ‘/Cpu/ {i=($8*255)/100; printf "%2.02x%2.02×00\n",255-i,i;fflush()}’|neouart[/code]

Your NeoPixel is now a real-time CPU load monitor! It will change from green to yellow to red as your CPU goes from idle to busy. It should be green right now (assuming your Pi isn’t too jammed). You can give your CPU some work by entering this command…

[code lang=”bash” light=”true”]cat /dev/urandom >nul[/code]

…and it should make your green pixel turn positively red. Press control-C the break out of the busy loop and let the pixel settle back down to green. When you are done, you can enter…

[code lang=”bash” light=”true”]kill %%[/code]

..to kill the pixel-load-monitor background process that we started above.

That’s it! You can find the source code for neouart and lots more fun nua demo files to try (macbreath.nua and dogshow.nua) on GitHub.

FAQ

Q: Why power the NeoPixel off the Pi’s 3.3 volt pin rather than one of the 5 volt pins?
A: A couple of reasons. Firstly, the WS2812B draws less current at lower voltages. At 5 volts, full brightness white can draw as much as 60ma. This slightly above the maximum 50ma current draw that is spec’ed for the RaspPi power pins. Next, the WS2812B specs require that the high voltage on the data pin be at least 0.7 * Vcc to be detected as a high. If we power the NeoPixel with 5 volts, then the minimum “high” voltage comes out to 3.5 volts. The output on a RaspPi data pin like the one we are using is only ~3.3 volts, so if we power our NeoPixel at 5 volts, our 3.3 volt high data signals might not be high enough.

That said, I have never met a RaspPi that had trouble with 60ma on a 5 volt power pin, and I have never met a NeoPixel that didn’t see 3.3 volts as a high even when running at 5 volts power (although other people have), so if you like to live life on the edge and want your pixel to be slightly brighter and slightly bluer, I’d say you can at least try connecting to a 5 volt power pin.

Q: Can the Pi’s 3.3 volt pin supply enough current to light the NeoPixel?
A: According to the Pi’s specs, The 3.3 volt power pin can supply up to 50ma and empirically I’ve found that a single NeoPixel running full brightness white (the most power hungry color) off a 3.3 volt supply will draw less than 40ma.

Q: Can a NeoPixel really run off 3.3 volts? The strands are marked “+5V”.
A: The WS2812B logic can actually run fine down to less than 2 volts, you just start to loose brightness on the blue LED because it has the highest forward voltage. This means that a full-white NeoPixel running on 3.3 volts looks a little warmer than the same pixel running full-white off 5 volts, but color perception is a complicated thing so I wouldn’t worry too much about this.

Q: How do I know if I need the -i parameter?
A: Some NeoPixels are based on the WS2811 and some on the WS2812 controller. The biggest difference is that the 11’s want the colors in RGB order, whereas the 12’s want GRB. If you are not sure which kind you’ve got, try turning it on red using FF0000; if it turns green then you need the -i.

Q: How the heck does this work?
A: Tune in next time for a full explanation!

Q: Where can I buy individual NeoPixels?

A: Send a SASE to:

josh 
POB 362
NY, NY 10268

…with the words “NeoPixels for Pi’s” written on the back of the envelope and you’ll get a couple of NeoPixels in the mail a week or two later.

UPDATES:

9/7/14 – Added video and release 2.0 that supports smooth fades and WS2811-based pixels.

9/16/2014 – 100 in hex is actually 0x64, not 0x3C… Thanks Joe!

24 comments

  1. Sandra

    Hi Josh,

    I’m an absolute newbie to RPi and python. Managed to get your stuff working from terminal and it’s amazing :) Is there any chance to incorporate it into python code? E.g. making the led change color depending on what button is pressed?

    Thanks a lot

    • bigjosh2

      Probably the easiest way would be to run the neouart command from a shell inside python. For example, here is python code to turn on the pixel…

      from subprocess import call
      call(["neouart" , "ffffff" ])

      • Sandra

        Yeah, it works fine! Is there a chance to plug in more than one though? I’m using the RGB neopixels, but for some reason when I solder them, only the first one lights up and the rest remains with no change, so I think I will just have to use them all separately (?)

        Thank you very much,
        Sandra

  2. bigjosh2

    This technique of using the UART will only work reliably with a single pixel. I plan on doing a future post showing how to use the Pi’s GPIO pins and DMA to drive lots and lots of pixels…

  3. john m n

    Hello, i have tried everything you told me to do here, but i have a problem. i get the error “Failed to open /dev/mem: permission denied” What can i do to give neouart access to /dev/mem?

    best regards, John M

  4. Sammy Herring

    Hi,
    I’m using a raspberry pi 2, but the neopixel only shows green, regardless of what you enter, even if you try to turn it off.

    Could you offer any support please?

  5. sammy3155

    Hi,

    Im using a Raspberry Pi 2, however, for some reason I am only getting the neopixel to light up green when wired in. Even when I try to turn off the neopixel, it does not do so.

    I would really appreciate any support,

    Thanks in advance,

    Sam

        • bigjosh2

          It looks like they have significantly changed the hardware with the RaspPi2. Unfortunately, the data sheets for the new BCM2836 processor not available yet, so it would take a lot of work to try and figure out what is wrong. I’ll keep an eye out and will update `neouart` as soon as information is available.

          • wtb

            Did you ever get around to attempt to fix NeoUart for the modem 2 Rpi?
            I can also confirm that it works very well on my RPi B+, but when I tried it on my RPi2 it simply does not work. It would be fantastic if you could find a fix for that.

          • Sammy Herring

            Hi Josh, Thanks for the fix, but I can’t seem to get the fix to work… I’ve installed the latest version but the NeoPixels are not showing any sign of working when I apply the latest program on the RPi 2. Any ideas? Thanks in advance, Sam

  6. Bartosz N.

    Hello,
    I’m trying connect led strip to Raspberry Pi. For know I can drive only one led. How can I drive other leds (not only first) on led strip? Can I drive diodes indyvidually?

    • bigjosh2

      The code present here explicitly will only drive a single pixel. Driving more than one on a PI is much harder. I hope to eventually to write an article describing how you can do it.

    • bigjosh2

      Surprisingly no – at least not using this UART technique. There are only 2 UARTs on the Pi (UART0 and UART1), and for some reason they are both connected to the same pin (P1-08) for transmit.

  7. andrewr_

    Is it possible to use a raspberry pi to control large amounts of these pixels to make an led message board?

Leave a Reply