Maker.io main logo

How to Control NeoPixels with a Particle Photon

2019-06-11 | By Maker.io Staff

Programmers Photon

NeoPixels are LEDs with inbuilt serial controls that make controlling many LEDs in a chain very easy. While each LED is addressed, the addressing is all done in software. NeoPixels only require 3 wires: Power, Ground, and Data. In this How-To, we will learn how to control NeoPixels with a Particle Photon!

BOM

Including the NeoPixel Library

While a NeoPixel could be interacted with using fully-custom C code, it would not be the most elegant method, since some kind souls have already created a NeoPixel library for us to use. In a new project on the online Particle Build IDE, click the Libraries option in the menu bar on the left side of the window and search for NeoPixel. Select the NeoPixel library and include it in the new project.

How to Control NeoPixels with a Particle Photon 

Create a NeoPixel Object

Using NeoPixels when the library is all done (with the use of a class called Adafruit_NeoPixel.Initializing the object) requires several parameters that relate to the NeoPixel itself: the number of Neopixels in the chain, the pin that the NeoPixel is connected to, and the type of NeoPixel. While the first two parameters are self-explanatory, the third, the NeoPixel Type, is not (but you can leave this parameter blank if using standard Adafruit NeoPixels).

While these values can be directly passed into the class constructor function, it is better to use some defined variables that allow for easy adjusting later. The setup example below is for use with a NeoPixel Ring, which is compatible with the Particle Photon.

Copy Code
#define PIXEL_COUNT 24						// 24 Pixels on our ring
#define PIXEL_PIN D6						// Ring uses D6 as default pin
#define PIXEL_TYPE WS2812B					// Ring uses WS2812 Pixels
Adafruit_NeoPixel ring(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);	// Create out “ring” object

Once the object has been initialized, the function .begin() needs to be called before it can be accessed and used. It is best to put this instruction inside the setup() routine, as it only needs to be called once.

Copy Code
void setup() {
  ring.begin();
}

Updating the Display of the Neopixel

Before we look at any functions that change the color of the LEDs, show() needs to be understood first. During code execution, the LED color states are held inside the NeoPixel object and can be changed at any time, but the physical NeoPixel is not updated until the .show() function is called. This allows for updates to the NeoPixel to occur after the colors are changed to their desired values, and then all the LEDs are updated in one operation. For example, in the main loop, a change color function can be called to adjust a number of LEDs, but those visible LEDs won’t change color until the show() function is called.

Copy Code
ring.show()		// Update the physical NeoPixel with the new assigned colors 

Setting the Color and Brightness

There are many set color functions, but let’s examine the most important one: setPixelColor(). This function takes 4 required arguments and one optional argument, which are pixel number, red, green, blue, and white, respectively (the white parameter is optional because not all NeoPixels have an inbuilt white LED). The colors have values between 0 and 255, which results in the standard 16 million color possibilities. The pixel number is the nth pixel in the chain, so, for example, if this number was set to 5, then the 5th pixel in the NeoPixel chain will be changed.

Copy Code
ring.setPixelColor(num, red, green, blue);	// Generic function for changing color
ring.setPixelColor(3, 255, 255, 255);		// Change pixel 3 to white (all three colors max)

I often find NeoPixels too bright for my liking, so I usually turn their brightness down to as low as 5% (otherwise headaches can set in quickly). The function setBrightness() is used to change the brightness of the NeoPixel chain, but note that this affects all NeoPixels (and not just one NeoPixel)! The value passed to this function determines the brightness, with values between 0 and 255 being valid.

Simple LED Example

This simple example will make a NeoPixel display a single LED that appears to run around a NeoPixel Ring!

Copy Code
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
 
#define PIXEL_COUNT 24						// 24 Pixels on our ring
#define PIXEL_PIN D6						// Ring uses D6 as default pin
#define PIXEL_TYPE WS2812B					// Ring uses WS2812 Pixels
 
Adafruit_NeoPixel ring(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);	// Create out “ring” object	
 
int pixelCounter = 0;                       // Used for choosing a specific LED
 
void setup() 
{
    // Start the NeoPixel object
    ring.begin();       
    ring.setBrightness(5);                      	// Turn the brightness way down!!!
}
 
 
void loop() 
{
    for(int i = 0; i < PIXEL_COUNT; i ++)
    {
        if(i != pixelCounter)
            // Clear the LED
            ring.setPixelColor(i, 0, 0, 0);         
        else
            // Set LED to white
            ring.setPixelColor(i, 255, 255, 255);  
    }
    
    pixelCounter ++;
    
    // Reset pixelCounter on values greater than total LEDs
    if(pixelCounter >= PIXEL_COUNT)
        pixelCounter =  0;
    
    // Update the NeoPixel
    ring.show();        
}
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum