LilyPad Pixel Board Hookup Guide
Using NeoPixel Library and Sample Code
Now it's time to light up our pixels. Adafruit's NeoPixel library is a great way to control these LEDs.
Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.
For Arduino IDE users, click here to download a copy of the NeoPixel library along with some example code SparkFun has created.
Setup
There are a few lines of code required to set up your sketch to use the library.
language:c
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define LED_COUNT 3
// Create an instance of the Adafruit_NeoPixel class called "leds".
// That'll be what we refer to from here on...
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
The first line of code tells Arduino to add the NeoPixel library to the sketch. Before you upload any code, make sure you adjust the PIN and LED_COUNT definitions near the top of the sketch. These tell our program which pin on the LilyPad the first pixel is connected to, and how many pixels are linked together in the pixel chain. In our example hookup PIN is 6 and LED_COUNT is 3.
The Adafruit_NeoPixel
line defines our pixel settings and creates an instance we've named leds
.
In order to start controlling the pixels, we'll also need to put the leds.begin()
function somewhere near the beginning of the setup()
function. See the code below for how all of these things work together.
Setting Individual Pixel Colors
Setting an LED with the Adafruit NeoPixel library is a two step process - first setting the color, then showing that color. To set a pixel's color, we use the leds.setPixelColor(position, color)
command. Then, leds.show()
will display it on the pixels. Upload the example code below and you should see a different color displayed on each of the three pixels we hooked up earlier.
language:c
/******************************************************************************
LilyPad Pixel Board - Set Colors Example
Angela Sheehan
SparkFun Electronics
Adapted from SparkFun's WS2812 Breakout Hookup Guide code examples
This code demonstrates setting colors on individual LilyPad Pixel Boards using
the NeoPixel library.
******************************************************************************/
#include <Adafruit_NeoPixel.h>
#define PIN 6 //Which pin the pixels are connected to
#define LED_COUNT 3 //Number of pixels used
// Create an instance of the Adafruit_NeoPixel class called "leds".
// That'll be what we refer to from here on...
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
leds.begin(); // Start up the LED strip
leds.show(); // LEDs don't actually update until you call this function
}
void loop()
{
leds.setPixelColor(0, 255, 0, 0); // Set the first pixel to RED
leds.setPixelColor(1, 0, 255, 0); // Set the second pixel to GREEN
leds.setPixelColor(2, 0, 0, 255); // Set the third pixel to BLUE
leds.show(); //Display the colors
}
Feel free to experiment with creating different colors using different levels of red, green, and blue on each pixel. Using a color picker in graphic design software or a color picker tool can help you find the RGB values for a particular color.
Setting All Pixels
Now let's try setting all the pixels to the same color. Rather than have a line of code for each pixel, we can use a for()
loop in our code to cycle through and set each pixel's value.
language:c
/******************************************************************************
LilyPad Pixel Board - Set All Pixels
Angela Sheehan
SparkFun Electronics
Adapted from SparkFun's WS2812 Breakout Hookup Guide code examples
This code demonstrates setting all LilyPad Pixel Boards in the project to one
color using the NeoPixel library.
******************************************************************************/
#include <Adafruit_NeoPixel.h>
#define PIN 6 //Which pin the pixels are connected to
#define LED_COUNT 3 //Number of pixels used
// Create an instance of the Adafruit_NeoPixel class called "leds".
// That'll be what we refer to from here on...
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
leds.begin(); // Start up the LED strip.
leds.show(); // LEDs don't actually update until you call this.
}
void loop()
{
//Use a for loop to scroll through each pixel
for(int x=0; x<LED_COUNT; x++)
{
//Set the pixel to YELLOW
leds.setPixelColor(x, 255, 255, 0);
}
leds.show(); //Display the color
}
Setting Pixel Brightness
By now, you may have noticed that these pixels can get pretty blindingly bright! Luckily, the library includes a command to set the pixel chain's brightness.
leds.setBrightness();
can be given a value between 0 and 255.
Setting the brightness will not only help save your eyesight, but can also keep the LEDs in the pixels from using a lot of power, which will prolong your project's battery life. Make sure to call leds.show()
afterward to display the updated brightness setting.
This sets all of the pixels in the chain's brightness, not each pixel individually. If you want to show varying brightnesses for different pixels, try mixing up a darker version of the color using RGB values.
Here's some example code that uses a potentiometer to adjust the brightness levels in real time:
language:c
/******************************************************************************
LilyPad Pixel Board - Set Brightness
Angela Sheehan
SparkFun Electronics
Adapted from SparkFun's WS2812 Breakout Hookup Guide code examples
This code demonstrates changing the brightness of all LilyPad Pixel Boards in
the project with a potentiometer.
******************************************************************************/
#include <Adafruit_NeoPixel.h>
#define PIN 9 //Which pin the pixels are connected to
#define LED_COUNT 3 //Number of pixels used
int potentiometer = A2;
int brightness;
// Create an instance of the Adafruit_NeoPixel class called "leds".
// That'll be what we refer to from here on...
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
// Set the potentiometer as an INPUT:
pinMode(potentiometer, INPUT);
// Serial communication so we can view readings from the potentiometer
Serial.begin(9600);
leds.begin(); // Start up the LEDs
//Since the color of the LEDs won't be changing, only the brightness
// we can use a for loop in setup to set the color once
for(int x=0; x<LED_COUNT; x++)
{
leds.setPixelColor(x, 255, 0, 0); //Set the pixel to RED
}
leds.show(); // LEDs don't actually update until you call this.
}
void loop()
{
// Here we'll use the reading from the potentiometer to set the brightness
brightness = map(analogRead(potentiometer), 0, 1023, 0, 255);
leds.setBrightness(brightness); // Set the brightness of the LEDS
leds.show(); // Display the LEDs
}
Additional Code Examples
For more complicated color patterns, check out the example code included in the library download. Make sure to adjust PIN and LED_COUNT in the sample code to match your project's set up.