Link Search Menu Expand Document

Controlling NeoPixels

Table of contents

  1. Overview
  2. Open the BlinkNeoPixel.ino sketch
  3. NeoPixel Function Summary
    1. Functions
    2. Initialize the CP in the setup() function
    3. Change the displayed color
    4. Turn off all NeoPixels

Overview

As indicated in the following diagram, the Circuit Playground Express (CPX) has two fixed-color LEDs and 10 programmable NeoPixels.

LEDs on Adafruit Circuit Playground

A green (fixed-color) LED labeled “On” glows when power is supplied the CPX. A red (fixed-color) LED is connected to digital pin D13. This is the LED referred to by the pre-defined LED_BUILTIN variable in the Arduino IDE, and it is the LED that flashes when the standard blink sketch is running.

Blinking NeoPixels requires a little more code than blinking the LED on D13 because the NeoPixel has much more capability than a fixed-color LED. Each NeoPixel has a driver chip that separately controls three smaller LEDs: one red, one green and one blue. The following photograph is a close-up of a NeoPixel when the NeoPixel is off.

Close-up of NeoPixel on Feather Sense

Each NeoPixel has three LED – red, green and blue – that can be turned on separately. The following sequence of four images show a NeoPixel illuminating the red, green and blue LEDs individually, and all three at the same intensity, which produces a white light at a sufficient distance from the NeoPixel.

Close-up of NeoPixel showing red Close-up of NeoPixel showing green Close-up of NeoPixel showing blue Close-up of NeoPixel showing white

NeoPixel software objects can control individual NeoPixels and NeoPixels in strips, rings, small arrays, large arrays, or bulk spools. Additional information is provided in Adafruit’s NeoPixel Uberguide

Open the BlinkNeoPixel.ino sketch

The BlinkNeoPixel.ino sketch has a simplified code to blink one of the NeoPixels on the CPX.

Download BlinkNeoPixel.ino and upload it to your board. While uploading the code, all of the NeoPixels should glow green and then red. After the upload is complete, the NeoPixel should blink a reddish purple color. You can change the color by adjusting the values of r, g and b in the range 0 to 255 for each.

//  File:  BlinkNeoPixel.ino
//
//  Turn on a NeoPixel on for one second, then off for one second, repeatedly.
//  The NeoPixel is specified by the nPixel variable in the loop() function.
//
//  Reference:  https://learn.adafruit.com/circuit-playground-lesson-number-0/neopixels

#include <Adafruit_CircuitPlayground.h>

// -- The setup function runs only once after power is first applied or reset
void setup() {
  CircuitPlayground.begin();
}

// -- The loop function runs over and over again forever
void loop() {

  int nPixel = 3;   //  Number of NeoPixel to turn on and off
  int iRed = 200;   //  Amount of red to display.    0 <= iRed <= 255
  int iGreen = 0;   //  Amount of green to display.  0 <= iGreen <= 255
  int iBlue = 0;    //  Amount of blue to display.   0 <= iBlue <= 255

  CircuitPlayground.setPixelColor(nPixel, iRed, iGreen, iBlue);   // turn on
  delay(1000);                                                    // wait 
  CircuitPlayground.setPixelColor(nPixel, 0, 0, 0);               // turn off
  delay(1000);                                                    // wait 
}

NeoPixel Function Summary

Functions

CircuitPlayground.begin();
CircuitPlayground.setPixelColor(i, iRed, iGreen, iBlue);
CircuitPlayground.clearPixels();

Initialize the CP in the setup() function

CircuitPlayground.begin();

Change the displayed color

The color of each NeoPixel is defined by the mixture of three colors: Red, Green and Blue. This is referred to as as the RGB color model.

CircuitPlayground.setPixelColor(i, iRed, iGreen, iBlue);

i is the number of the NeoPixel

iRed is the magnitude of the red color, 0 ≤ iRed ≤ 255

iGreen is the magnitude of the red color, 0 ≤ iGreen ≤ 255

iBlue is the magnitude of the red color, 0 ≤ iBlue ≤ 255

You can use constants for the values of i, iRed, iGreen and iBlue. For example

CircuitPlayground.setPixelColor(3, 200, 0, 200);

turns NeoPixel 3 a yellow color. However, we recommend using variables in many situations because that makes your code easier to maintain.

Turn off all NeoPixels

CircuitPlayground.clearPixels();