empowering creative people

Using NeoPixels with Particle

Life’s better in colour right? Then why are you still using boring old regular LEDs in your project, when you could be using addressable RGB LEDs. Now LEDS that have Red, Green, and Blue integrated in a single package are nothing new, however these analogue LEDs require a control wire for each colour, and if you’re using more than one, it starts taking up a lot of pins, 3 for every LED! Fortunately, in recent years, digitally addressable LEDs have started to make their way onto the market, and the technology has gotten better and better. One of the most widely used products is the WS2812 line.

WS2812

Adafruit board NeoPixelThe WS2812 is one of the most popular digitally addressable RGB LEDs and is used in thousands of products today, as well as DIY and maker electronics. They allow you to have as many LEDs connected in series as you want, all controlled by a single wire from a microcontroller. That’s right, you can control an entire string of these, all from a single pin! The standard WS2812 has 6 pins, only one of which is used for data, however the alternate WS2812B is used more commonly and has only 4 pins in a 5050 SMD package. They both use the same communication protocol, however the timings are slightly different. Today we’ll be using examples with the WS2812B version, however the examples are fairly easily adaptable to the WS2812.

How Do They Work?

Each LED package looks the same as a standard 5050 package LED, however packed in there is a tiny circuit with the controller chip, and an individual LED for each colour. The WS2812B chip takes care of the LED driving, and we just send it data to tell it what the LED colour should be. Each LED receives its data, then passes the next packet of data along to the next one. The actual timing and communication protocol is slightly weird and getting a bit complicated; into the micro-second world, however fortunately for use, wonderful people have created libraries to make getting started with these super easy. For more information about the timing required and the intricate workings of the WS2812/B, check out this fantastic write up on the reverse engineering of both the WS2812B and WS2812.

Annimated NeoPixelWhat is a NeoPixel?

Now you might have heard the term NeoPixel before and wondered what it is. NeoPixel is Adafruit’s brand name for their products using WS2812, WS2811, and SK6812. Fortunately for us, Adafruit have a habit of releasing incredibly solid libraries for all of their products, and the NeoPixel line is no exception, and today, we’re going to look at how use NeoPixel products with our Particle boards.

Before you dive into this tutorial, if you’re not familiar with using the Particle IDE or the Photon, check out our other Particle tutorials.

The Goal

In this tutorial, we’ll be using the Adafruit Particle libraries to get started with NeoPixels using a Particle Photon. For the Photon we’ll be using the 24x NeoPixel Photon kit, however, the code will work on an Electron as well, you will just need to use separate NeoPixels. The examples we’ll be going through should work on any NeoPixel product, you’ll just need to change the definitions at the top of the code (we’ll go through that later).

The Gear

To complete this tutorial you’ll require the following products:

-Particle Photon (or Electron)

- Particle NeoPixel Ring Kit

**Note that you will need to solder  to the NeoPixel ring**

Plug your Photon into the Ring Kit, or connect up your other NeoPixels, bear in mind that some NeoPixels require 5V to run, other 3.3V, so if you try using 5V NeoPixels using a Particle product, they may not working because the Photon is only outputting a 3.3V digital signal.

If you're unsure about using 3.3V and 5V devices together safely, check out our Logic Level Converters for Particle tutorial. It will show you how to setup a logic level converter with your Particle device. To connect NeoPixels that aren't on a shield board like the NeoPixel Ring Kit, simply connect the power wires to 5V (or 3.3V), ground, and the data wire to your Particle/logic level converter pin.

The Code

Let’s get started! Create a new app in the Particle IDE and add the NeoPixel library to it. If you view the library, you can check out some of the examples which demonstrate the use of the NeoPixels, however, let’s take a look at some of the basic functionality.

Copy the code below into your IDE and this will be the framework to test out the NeoPixel functionality:

#include "application.h"
#include "neopixel/neopixel.h"

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 24
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {
strip.begin();
strip.show();
}

void loop() {
}

It has the library inclusions, and begin/show functions to initialise the NeoPixels. You’ll also notice the 3 definitions at the top called PIXEL_COUNT, PIXEL_PIN, and PIXEL_TYPE, these are important as the define how many NeoPixels are connected in series per pin, how pin they’re connected to, and what type of NeoPixel chips they are. They’re setup as shown for use with the Photon Ring Kit, however you will need to change these to reflect how you’re NeoPixel setup is configured. Finally we’re creating an object called strip with the properties we defined above.

To turn an LED on, we use the function:

#include "application.h"
#include "neopixel/neopixel.h"

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 24
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {
strip.begin();
strip.show();
}

void loop() {
strip.setPixelColor(0, 50,50,50);
strip.show();
delay(500);
}

This sets the colour of LED ‘i’. Something to keep in mind is that this doesn’t actually display the colour on the LED, it just sets it, to update the LEDs to display that colour you’ll need to use:

strip.show();

So that’s the basics of turning individual LEDs, almost everything else is done in standard code, so let’s take a look at a simple animation using our NeoPixels:

#include "application.h"
#include "neopixel/neopixel.h"

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 24
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B

#define PEACH 200,50,5
#define CYAN 10,150,70
#define PURPLE 180,3,180
#define BLUE 5,5,190
#define WHITE 150,150,150
#define GREEN 10,180,10

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int waitTime = 25;
int i;
void spin(int R, int G, int B);

void setup() {
  strip.begin();
  strip.show();
} void loop() { spin (PEACH); spin (CYAN); spin (PURPLE); spin (BLUE); spin (WHITE); spin (GREEN); } void spin(int R, int G, int B) { for(i=0; i < PIXEL_COUNT; i++) { strip.setPixelColor(i, R,G,B); strip.show();
delay(waitTime); } for(i=0; i < PIXEL_COUNT; i++) { strip.setPixelColor(i, 0,0,0); strip.show();
delay(waitTime); } }

Here we’re creating a function which turns the LEDs on in sequence, then turning them off again. In the main loop we’re just passing a colour to the function in its R/G/B components. Super simple, and all we’re using is the setPixelColor() and show() functions. There are a couple of other functions available for increased functionality, for more info check out the Adafruit NeoPixel page.

What Now?

Now that we’ve learnt how to control NeoPixels and looked at how easy they are to scale, there’s an almost limitless potential to the projects that you can create with them. Here’s a few key pointers when using NeoPixels:

  • They are timing specific so you need to use a real-time processor like Particle, Arduino etc. Software simulated environments like the Raspberry Pi will not work due to the extra time spent in processing.
  • Each NeoPixel has a maximum current draw of 60mA so you’ll need to make sure that you use an external power supply for any large numbers. Even the 24 NeoPixel’s we’re using here will draw roughly 1.5 Amps at max brightness.
  • Each NeoPixel uses a few bytes of Ram, so make sure you’re using a beefy chip with plenty.
  • When chaining large strings of LEDs, the voltage drop across the copper strips may start affecting colours so consider using multiple strings and voltage points for larger runs.

Now take these fantastic devices and create amazing things!

 

Life’s better in colour right? Then why are you still using boring old regular LEDs in your project, when you could be ...

Have a question? Ask the Author of this guide today!