Controlling NeoPixels with the Raspberry Pi A+/B+

Addressable LEDs or NeoPixels are typically used in combination with an Arduino or similar microcontroller, due to the timing critical signal required to control them. An SBC such as the Raspberry Pi is not suited for such realtime GPIO activities, as the Linux operating system runs other tasks in parallel. Or at least that was the case until Jeremy Garff found a way to use the DMA (Direct Memory Access) module to transfer bytes of memory between different parts of the processor, without using the CPU and thus not being interrupted by the Pi’s OS.

This procedure works for all Raspberry Pi models except version 2!

Software

Jeremy Garff has written a library called “rpi_ws281x”, which can be found on his GitHub page: https://github.com/jgarff/rpi_ws281x. It makes use of the Pi’s BCM2835’s PWM module to drive the controllable WS281X LEDs found in NeoPixel strips and rings. The folks at Adafruit have created a Python wrapper for the library along with some Python example scripts, making it look and feel like the Arduino NeoPixels library. So if you’re familiar with NeoPixels on the Arduino, you should be up and running with this version in no time.

To compile and install the library, follow the steps below.

First, install the dependencies required to download and install the library:

Next, download the files and build the library:

Finally, install the Python wrapper:

As you can see, these steps are very straightforward.

Hardware

Screen+Shot+2015-05-20+at+10.29.31Hooking up the NeoPixels to the Raspberry Pi is extremely easy, just make sure the power supply used is properly rated for the number of NeoPixels you intend to use. For testing, I used a 5V/4A power supply to power the Pi and the NeoPixels (12 and 60 LEDs).

Make sure the ground signals of the NeoPixel strip/ring and the Raspberry Pi are connected. If they are not, the LEDs won’t function properly and will light up in unpredictable patterns.

Even though the LED strip/ring operates at 5V and the Pi’s GPIO at 3.3V, it appears that it is possible to drive the LEDs without having to use logic level conversion.

Demo

I tested two components:

  • an Adafruit NeoPixel ring with 12 LEDs
  • an addressable WS2811 60 LED strip from eBay

Both performed as expected using the sample script (strandtest.py), which I edited to configure the correct number of LEDs:

As you can see, it is also possible to edit other parameters to match the LEDs used, but also parameters such as brightness.

photo+2 photo+(7)

Video:

36 thoughts on “Controlling NeoPixels with the Raspberry Pi A+/B+”

    1. Hi Erich. I’m not sure why exactly, but I suppose it has to do with the new CPU on the Pi 2 as opposed to the previous models (A, B, A+, B+).

      Frederick

  1. Q:Can you also turn all led red or only the second led blue? if so how would the coding look like

    thanks in advance

    1. Hello,
      I can confirm that this instruction is working with the Raspberry Pi 3. At least it is working on my Pi3 at the moment. I’m running Raspbian Jessy. The only thing I have to do is to run the script from the terminal with ‘sudo’. Without I get an error with denied permission to open /dev/mem/. Don’t know how to fix this at the moment. For that I’m to unexperienced with this distributions.

  2. Hi,
    Does anyone have this working on RPi 2?
    When i run strandtest.py the ring starts to do strange things, for instances, 6 leds are always up. The effects are not perfect. Any ideas?

    Thanks

  3. Hi Fred,
    I just tried working this library in Raspberry Pi Zero. Everything looks pretty straightforward. But I’m getting the following error if I try to run sudo ./test:
    Can’t open device file /tmp/mailbox-11974: No such device or address
    Can’t open device file /tmp/mailbox-11974: No such device or address

    What’s the problem and how do I fix this?

  4. Why have got a wire going to 5 volt pin on the raspberry pi when you have an external power supply. This is not present in the wiring diagram but it is in the video.

    1. It’s not required, the diagram is the proper way to do it but requires two power supplies: micro usb + barrel jack.
      A shortcut I took was to power via the GPIO, using a single power supply. This bypasses some fuses, so make sure you only apply 5V if doing like that.

  5. Hi Frederick, I’m trying to connect a simple neopixel jewel (7 leds) to my raspberry. I’ve achieved on my arduino nano (5V) and on my arduino due (3.3V). But I can’t not achieve on my raspberry pi B+. First attempt I connected like arduino, I mean, raspberry power via usb and jewel to 5V, GND and GPIO18. It didn’t work, leds flashing random. After that I found your video. I’ve connected just like your tutorial but it hasn’t worked. I used a wired diagram from Adafruit tutorial as well (using a diode) but it hasn’t worked 🙁 Always leds blinking. Can you give me some clue? Thanks!

  6. Hey Frederick, I just tried this on my Raspberry Pi (Neopixel 12 LED’s), but the LED’s are blinking randomly. 2 Of them blink with a higher brightness than the others. Do you have any ideas what the problem could be?

    1. Hi Kaj,

      first thing I would think of, is that the GND of the NeoPixels may not be connected to the GND of the Pi. Can you confirm whether or not that’s the case?

      Also, the NeoPixels don’t play nice with the Pi’s audio. You’re not playing any audio I assume?

      1. Hey Frederick,

        Thanks for the reply! I was not playing any audio, but I tried disabling the build-in soundcard by adding “blacklist snd_bcm2835” to the file /etc/modprobe.d/alsa-blacklist.conf and that did the trick.

        1. Hey Kaj,

          thank you so much for this piece of information. I read about the soundcard incompatibility with the workings of the lib before but I forgot. Anyway now it works with my Pi 2. I’m sort of blind now, though …

  7. Hi,

    I was wondering if this would work using airplay music? I’m using my raspberry as an AirPlay receiver which is then connected to my receiver with HDMI.

    If I put this in place, will the lights illuminate when I play music?

    Thanks in advance for the answer :-).

    1. I think that in combination with HDMI audio, it should be fine, though I haven’t tested it. I did encounter problems of NeoPixels and audio interfering with eachother using the 3.5mm stereo jack of the Pi as an output. Worst case, you may need to use a USB sound device to avoid interference when controlling the NeoPixels.

      Hope this helps 🙂

  8. It’s a shame to have to go through the sudo drive through the terminal.
    I wanted to incorporate device and include it in my own programming using various sensors.
    Too bad for me, I’ll have to find another solution to replace these led. 🙂
    Thank you for your work.
    Google translator.

  9. Je suis Français désolé 🙂
    Je disais qu’il est dommage de devoir passer par le terminal plutôt que par l’idle de python 🙂
    Ceci dis, je début aussi en python depuis quelques mois, et j’ai pris pour habitude donc de passer par idle.

    Mais je vais tout de même essayer voir si j’arrive à en faire quelque chose, mais dans un premier temps essayer de trouver pourquoi je n’ai pas le même rendu sur votre vidéo et mon rendu lorsque je lance standtest.py , ça clignote de partout et je crois que je deviens aveugle lol.

    Si vous avez une idée je suis preneur, merci 🙂

  10. RPI 3
    12 LED ring neopixel RGBW w/ integrated drivers from AdaFruit
    using this library

    got it working after forcing audio through 3.5mm jack only (disable HDMI audio) used raspi-config to nix the hdmi audio.

    Now Octopi is running great and im working on color changes for status updates on my prints. So bright and clean, gotta love those LEDS!

  11. woaaahh…works like a charm. Raspberry PI A (the old 26Pin model), 60 LED stripe. Thx a lot for sharing this tutorial.

  12. Trying to run this on a pi zero and it doesn’t seem to be working. I disabled snd_bcm2835, and I have it on the right pin. The wires are all connected properly. Some of the leds near the end light up, and the ones near the tip will flash sometimes, but that’s about it. Tried it with a BJT level shifter, same result.

    1. Got it to work! The trick was adding “blacklist snd_bcm2835” in the /etc/modprobe.d/alsa-blacklist.conf and not using modprobe -r

    1. I was able to use this information to program my Pi3 and Pi0 to control a short NeoPixel strip. How far can the signal be sent? I have a project in mind that will distribute 30 LEDs evenly on a trail of 300 feet. Will the signal strength degrade? Would it need to be shielded? Any additional help would be greatly appreciated.

Leave a Reply

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