In this guide we walk through how to program your first Neopixel sketch
Create your Circuit
Complete the wiring for your Neopixel outlined in the main Neopixel guide
In Particle Build, create a new project and add the library for Neopixels.
Setting up the Sketch
Every Neopixel sketch will start with the same boilerplate:
First start by including the Neopixel library at the top of your file
#include <neopixel.h>
Hint: This should happen automatically when you add the library. Only do this if it doesn’t get added to your code file.
Next add the definitions to the top of your code file and adjust as needed if you are using a different type of Neopixel or are using a different pin.
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN SPI
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
Pins compatible with the Neopixel
If you are using an older Particle board (the Argon, Boron, Xenon or original Photon), you can wire to most of the Digital Pins, e.g. D2
, D3
, etc. The Photon2 requires you to use the MOSI aka MO
pin of the SPI
or SPI1
interface with the neopixel
In the setup you need to initialize the neopixel with the strip.begin
command, like so
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
Then in your main loop you can write code to control the Neopixels:
void loop() {
uint16_t i;
uint32_t c = strip.Color(255, 255, 255);
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, c );
strip.show();
delay( 100 );
}
delay( 100 );
}
Remember that the strip.show
is really important. It tells your Neopixels to push the updates to each individual pixel
Compiling and sending to the Particle Photon2.
Make sure the Status Bar has a Particle Photon2 connected and the Particle Photon2’s indicator is breathing blue. If not make sure your Photon2 is connected by USB and is getting a WiFi signal.
Press the Lightning bolt on the top left of the window.
You’ll see a message ‘Compiling in the Cloud’ and a few sections later your Photon2 should start flashing magenta.
Wait a few moments, and your Neopixel should begin to light up!
Build your skills - Independent Exercises
Exercise 1
Modify the code to light up pixel by pixel then reverse the sequence turning each pixel off one by one
Exercise 2
Modify the code to light up blue, then red, then green then white in sequence
Exercise 3
Change the code to only light up only one pixel at a time. With each loop move that pixel one position higher. When it reaches the final pixel, restart the sequence at zero i.e. build a single pixel that will continually cycle
Exercise 4
Add three Particle.function to set the Red, Green and Blue components. Allow these values to be set from 0-255 and as they are set to change all of the pixels on the LED strip to that color.