Last Updated: 04/11/2021

Neopixels

Components >> Neopixels

NeoPixels

WS2812 neopixel and Arduino

This tutorial will be based on the WS2812 type flexible neoPixel strip although the code will work with neopixel rings and break out boards.

Connections are easy, D0 goes to the Arduino Digital Pin you wish to use.
GND goes to Arduino GND
5v to Arduino 5v

All examples in this tutorial will use the Adafruit NeoPixel library installable from the Arduino IDE library manager.


Note: There are some NeoPixels that are 12v, for these you will need a seperate 12v power supply. Don't forget to connect the GND to the Arduino GND as well as the power supply GND.

For longer lengths of NeoPixels even the 5v type will need an external 5v supply as the Arduino will not have enough power. Connect in the same way as a 12v set.

 

Since the video was made example 6 has been added, this has millis() timers for each neopixel as well as each neopixel fading in and out independently.

NeoPixel uses

NeoPixels can be used for various lighting effects. They have the ability to change to virtually any colour combination as well as being able to alter the brightness of the lights.

I have used them in buildings on model railways as well as in control panels although their uses are only limited by your imagination.

They have the advantage of only needing a single digital ping to control a huge number of Neopixels, on a Mega in theory you could control about 2500 if you don't have anything else running.

They seem to be quite tough, I have encased them in hot glue on some projects without any problems.


Below is an axample of using NeoPixels for track routing, in this example a GT911 Capacitative touch screen has been mounted over the top of the NeoPixels to enable touch control of the track direction.

DCC Controller using neopixels

Example 1: NeoPixel_test.ino


Click to Download code: NeoPixel_test.ino

Simple sketch to test the NeoPixels are connected correctly

 

 
/* 18/10/2021
 *  Neopixel_test
 * 
 *  Test sketch based on the Adafruit library to make sure basics are working  
 *  sets the colours for a number of neopixels
 *  Sets various colours and brightnesses.
 * 
 */



//Library needs to be included
#include "Adafruit_NeoPixel.h"


// Set the pin that is connected to the D0 on the Neopixel string
#define PIN        8

// change to the number of neopixels in your string
#define NUMPIXELS 16 // 


// see Adafruit strandtest example for more information on possible values.
//sets up an Object called "pixels" that our instructions will be sent to
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


void setup() {
  Serial.begin(9600);
  Serial.println("NeoPixel_test");
  //This line starts by initialising "pixels"
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)

  pixels.clear(); // Set all pixel colors to 'off'
  //now set various values starting at 0 to 15 for a 16 pixel string
  pixels.setPixelColor(0, pixels.Color(0, 50, 0));//green
  pixels.setPixelColor(1, pixels.Color(50, 0, 0)); //red
  pixels.setPixelColor(2, pixels.Color(0, 0, 50)); //Blue
  pixels.setPixelColor(3, pixels.Color(50, 0, 0)); //red
  pixels.setPixelColor(4, pixels.Color(0, 50, 0));//green
  pixels.setPixelColor(5, pixels.Color(0, 0, 50)); //blue
  pixels.setPixelColor(6, pixels.Color(50, 50, 50)); //white
  pixels.setPixelColor(7, pixels.Color(255, 0, 0)); //bright red
  pixels.setPixelColor(8, pixels.Color(0, 255, 0));//bright green
  pixels.setPixelColor(9, pixels.Color(0, 0, 255));//bright blue
  pixels.setPixelColor(10, pixels.Color(255, 255, 255)); //bright white 
  pixels.setPixelColor(11, pixels.Color(50, 50, 0)); //yellow
  pixels.setPixelColor(12, pixels.Color(0, 50, 50));//light blue
  pixels.setPixelColor(13, pixels.Color(50, 0, 50));//purple
  pixels.setPixelColor(14, pixels.Color(50, 25, 25)); //pink
  pixels.setPixelColor(15, pixels.Color(25, 50, 25));//
  //Nothing changes until this line is sent
  pixels.show();   // Send the updated pixel colors to the hardware.
}


void loop() {
  
}

Example 2: NeoPixelMulti.ino

How to attach and control multiple strings of NeoPixels to an Arduino.


Click to Download code: NeoPixelMulti.ino

 

 
/*
  NeoPixelMulti
  simple example of attaching multiple strings of Neoppixels to an Arduino

  This method can be useful for model railways when it's hard to contune a 
  single string of neopixels from one building to another
  
  Pins used
  5 pixels1 
  6 pixels2
  7 pixels3
  
*/

//Library needs to be included
#include "Adafruit_NeoPixel.h"

// Set the pin that is connected to the D0 on the Neopixel string 1
#define neoPin1        5
// change to the number of neopixels in your string
#define NUMPIXELS1 16 // 16 on my test strip
//Create object "pixels1"
Adafruit_NeoPixel pixels1(NUMPIXELS1, neoPin1, NEO_GRB + NEO_KHZ800);

// Set the pin that is connected to the D0 on the Neopixel string 2
#define neoPin2        6
// change to the number of neopixels in your string
#define NUMPIXELS2 16 // 16 on my test strip
//Create object "pixels2"
Adafruit_NeoPixel pixels2(NUMPIXELS2, neoPin2, NEO_GRB + NEO_KHZ800);

// Set the pin that is connected to the D0 on the Neopixel string 3
#define neoPin3        7
// change to the number of neopixels in your string
#define NUMPIXELS3 16 // 16 on my test strip
//Create object "pixels3"
Adafruit_NeoPixel pixels3(NUMPIXELS3, neoPin3, NEO_GRB + NEO_KHZ800);


void setup() {
  Serial.begin(9600);
  Serial.println("NeoPixelMulti...");
  
  //neopixels
  pixels1.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pixels1.clear(); // Set all pixel colors to 'off'
  pixels1.show();
  pixels2.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pixels2.clear(); // Set all pixel colors to 'off'
  pixels2.show();
  pixels3.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pixels3.clear(); // Set all pixel colors to 'off'
  pixels3.show();
  
  
}

void loop() {
  int q;
  for(q=0;q<16;q++){
    pixels1.setPixelColor(q, pixels1.Color(0, 0 , 150)); //pixels1 are blue
    pixels1.show();
    pixels2.setPixelColor(q, pixels2.Color(0, 150 , 0)); //pixels2 are green 
    pixels2.show();
    pixels3.setPixelColor(q, pixels3.Color(150, 0 , 0)); //pixels3 are red 
    pixels3.show();
    delay(100);  
  }
  pixels1.clear(); // Set all pixel colors to 'off'
  pixels1.show();
  pixels2.clear(); // Set all pixel colors to 'off'
  pixels2.show();
  pixels3.clear(); // Set all pixel colors to 'off'
  pixels3.show();
  
}

Example 4: NeoPixel_MultiColourtwinkle.ino

How to use multiple millis() timers to control NeoPixels creating a random multicoloured twinkling lights effect


Click to Download code: NeoPixel_MultiColourtwinkle.ino

 

 
/* 18/10/2021
    Neopixel_twinkle

    This sketch shows how to control  different neopixels using a millis() timer.
    In this example all 16 neopixels will have their own random timers



*/



//Library needs to be included
#include "Adafruit_NeoPixel.h"
// Set the pin that is connected to the D0 on the Neopixel string
#define PIN        8
// change to the number of neopixels in your string
#define NUMPIXELS 16 // 
// see Adafruit strandtest example for more information on possible values.
//sets up an Object called "pixels" that our instructions will be sent to
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

unsigned long myNeoTimers[16];//areray that holds millis() timer info for each neopixel


void setup() {
  Serial.begin(9600);
  Serial.println("NeoPixel_MultiColourtwinkle");
  //This line starts by initialising "pixels"
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)

  pixels.clear(); // Set all pixel colors to 'off'
  //now set various values starting at 0 to 15 for a 16 pixel string
  pixels.setPixelColor(0, pixels.Color(0, 50, 0));//green
  pixels.setPixelColor(1, pixels.Color(50, 0, 0)); //red
  pixels.setPixelColor(2, pixels.Color(0, 0, 50)); //Blue
  pixels.setPixelColor(3, pixels.Color(50, 0, 0)); //red
  pixels.setPixelColor(4, pixels.Color(0, 50, 0));//green
  pixels.setPixelColor(5, pixels.Color(0, 0, 50)); //blue
  pixels.setPixelColor(6, pixels.Color(50, 50, 50)); //white
  pixels.setPixelColor(7, pixels.Color(255, 0, 0)); //bright red
  pixels.setPixelColor(8, pixels.Color(0, 255, 0));//bright green
  pixels.setPixelColor(9, pixels.Color(0, 50, 255));//bright blue
  pixels.setPixelColor(10, pixels.Color(255, 255, 255)); //bright white
  pixels.setPixelColor(11, pixels.Color(50, 50, 0)); //yellow
  pixels.setPixelColor(12, pixels.Color(0, 50, 50));//light blue
  pixels.setPixelColor(13, pixels.Color(50, 0, 50));//purple
  pixels.setPixelColor(14, pixels.Color(50, 25, 25)); //pink
  pixels.setPixelColor(15, pixels.Color(25, 50, 25));//
  //Nothing changes until this line is sent
  pixels.show();   // Send the updated pixel colors to the hardware.
  delay(3000);//wait 3 seconds before starting the show
}


void loop() {
  int q;
  for (q = 0; q < 16; q++) {
    if (millis() > myNeoTimers[q]) {
      myNeoTimers[q] = millis() + random(200, 4000); //pick a random number to extend the timer between 0.2 and 4 seconds
      pixels.setPixelColor(q, pixels.Color(random(0,255), random(0,255), random(0,255))); //random colour and brightness
      pixels.show();
    }
  }
}

Example 5: NeoPixel_twinkle.ino

How to use multiple millis() timers to control NeoPixels creating a random twinkling lights effect


Click to Download code: NeoPixel_twinkle.ino

 

 
/* 18/10/2021
 *  Neopixel_twinkle
 *  
 *  This sketch shows how to control  different neopixels using a millis() timer.
 *  In this example all 16 neopixels will have their own random timers
 * 
 * 
 * 
 */



//Library needs to be included
#include "Adafruit_NeoPixel.h"
// Set the pin that is connected to the D0 on the Neopixel string
#define PIN        8
// change to the number of neopixels in your string
#define NUMPIXELS 16 // 
// see Adafruit strandtest example for more information on possible values.
//sets up an Object called "pixels" that our instructions will be sent to
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

unsigned long myNeoTimers[16];//areray that holds millis() timer info for each neopixel
int myNeoState[16];//holds thee current on/off state

void setup() {
  Serial.begin(9600);
  Serial.println("NeoPixel_twinkle");
  //This line starts by initialising "pixels"
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)

  pixels.clear(); // Set all pixel colors to 'off'
  //now set various values starting at 0 to 15 for a 16 pixel string
  pixels.setPixelColor(0, pixels.Color(0, 50, 0));//green
  pixels.setPixelColor(1, pixels.Color(50, 0, 0)); //red
  pixels.setPixelColor(2, pixels.Color(0, 0, 50)); //Blue
  pixels.setPixelColor(3, pixels.Color(50, 0, 0)); //red
  pixels.setPixelColor(4, pixels.Color(0, 50, 0));//green
  pixels.setPixelColor(5, pixels.Color(0, 0, 50)); //blue
  pixels.setPixelColor(6, pixels.Color(50, 50, 50)); //white
  pixels.setPixelColor(7, pixels.Color(255, 0, 0)); //bright red
  pixels.setPixelColor(8, pixels.Color(0, 255, 0));//bright green
  pixels.setPixelColor(9, pixels.Color(0, 50, 255));//bright blue
  pixels.setPixelColor(10, pixels.Color(255, 255, 255)); //bright white 
  pixels.setPixelColor(11, pixels.Color(50, 50, 0)); //yellow
  pixels.setPixelColor(12, pixels.Color(0, 50, 50));//light blue
  pixels.setPixelColor(13, pixels.Color(50, 0, 50));//purple
  pixels.setPixelColor(14, pixels.Color(50, 25, 25)); //pink
  pixels.setPixelColor(15, pixels.Color(25, 50, 25));//
  //Nothing changes until this line is sent
  pixels.show();   // Send the updated pixel colors to the hardware.
  delay(3000);//wait 3 seconds before starting the show
}


void loop() {
  int randomBrightness;
  int q;
  for(q=0;q<16;q++){
    if(millis() > myNeoTimers[q]){
      if(myNeoState[q] < 1){//if pixel is turned off
        myNeoTimers[q] = millis() + random(200,8000);//pick a random number to extend the timer between 0.2 and 4 seconds  
        myNeoState[q] = 1;
        //randomBrightness = 255;//set brightness
        randomBrightness = random(0,255);
        pixels.setPixelColor(q, pixels.Color(randomBrightness, randomBrightness, randomBrightness)); //various whites
      }else{//if pixel is turned on
        myNeoTimers[q] = millis() + random(100,1000);//pick a random number to extend the timer between 0.1 and 1 seconds for off time  
        myNeoState[q] = 0;
        pixels.setPixelColor(q, pixels.Color(0, 0, 0)); //off
      }
      pixels.show();  
    }
  }
}

Example 6: NeoPixel_MultiColourFadeInOutTwinklev1.ino

This version has seperate timers for each NeoPixel but also has a fade in/out effect, all controlled thorugh millis();

Because the colurs fade in and out a multi domensiaonal array is needed to store the information for each neopixel.


Click to Download code: NeoPixel_MultiColourFadeInOutTwinklev1.ino

 

 
/* 04/11/2021
    NeoPixel_MultiColourFadeInOutTwinklev1

    16 neopixels on individual timers as well as fade in out



*/



//Library needs to be included
#include "Adafruit_NeoPixel.h"
// Set the pin that is connected to the D0 on the Neopixel string
#define PIN        8
// change to the number of neopixels in your string
#define NUMPIXELS 16 // 
// see Adafruit strandtest example for more information on possible values.
//sets up an Object called "pixels" that our instructions will be sent to
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);



void setup() {
  Serial.begin(9600);
  Serial.println("NeoPixel_MultiColourFadeInOutTwinklev1");
  //This line starts by initialising "pixels"
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)

  
  //now set various values starting at 0 to 15 for a 16 pixel string
  //Used as a test to make sure connection is correct.
  pixels.setPixelColor(0, pixels.Color(0, 50, 0));//green
  pixels.setPixelColor(1, pixels.Color(50, 0, 0)); //red
  pixels.setPixelColor(2, pixels.Color(0, 0, 50)); //Blue
  pixels.setPixelColor(3, pixels.Color(50, 0, 0)); //red
  pixels.setPixelColor(4, pixels.Color(0, 50, 0));//green
  pixels.setPixelColor(5, pixels.Color(0, 0, 50)); //blue
  pixels.setPixelColor(6, pixels.Color(50, 50, 50)); //white
  pixels.setPixelColor(7, pixels.Color(255, 0, 0)); //bright red
  pixels.setPixelColor(8, pixels.Color(0, 255, 0));//bright green
  pixels.setPixelColor(9, pixels.Color(0, 50, 255));//bright blue
  pixels.setPixelColor(10, pixels.Color(255, 255, 255)); //bright white
  pixels.setPixelColor(11, pixels.Color(50, 50, 0)); //yellow
  pixels.setPixelColor(12, pixels.Color(0, 50, 50));//light blue
  pixels.setPixelColor(13, pixels.Color(50, 0, 50));//purple
  pixels.setPixelColor(14, pixels.Color(50, 25, 25)); //pink
  pixels.setPixelColor(15, pixels.Color(25, 50, 25));//
  //Nothing changes until this line is sent
  pixels.show();   // Send the updated pixel colors to the hardware.
  delay(3000);//wait 3 seconds before starting the show

  //prepare for system
  pixels.clear(); // Set all pixel colors to 'off'
  pixels.show();
}

unsigned long myNeoTimers[16];//array that holds millis() timer info for each neopixel
//Multi dimensional Array to hold all the data for the system
//see http://www.digitaltown.co.uk/lesson11Arrays.php for tutorial
//[lightState][lightDir][R][G][B],
//[lightState] 0-255 0 = off, 255 fully lit; controls the current brightness state...increasing or decreasing
//[lightDir] 0 = decreasing, 1 = increasing tells system if the light is increasing or decreasing
//[R][G][B] = the target RGB state
int myNeoSettings[16][5];

//const variables to make the code more readable when dealing with the array
const int lightState = 0;
const int lightDir = 1;
const int myRed = 2;
const int myGreen = 3;
const int myBlue = 4;

void loop() {
  int q;
  for (q = 0; q < 16; q++) {
    if (millis() > myNeoTimers[q]) {
      if(myNeoSettings[q][0] < 0){//light is out so ready to start a new cycle
        
        //first select a colour for this neopixel
        
        //this gives a white/yellow/blue effect
        //add extra case statements and increase random max to add more fixed colours
        switch(random(3)){
            case 1://Yellow
              myNeoSettings[q][myRed] = 255;
              myNeoSettings[q][myGreen] = 255; 
              myNeoSettings[q][myBlue] = 0;
              break;
            case 2://Blue
              myNeoSettings[q][myRed] = 0;
              myNeoSettings[q][myGreen] = 0; 
              myNeoSettings[q][myBlue] = 255;
              break;
            default://white
              myNeoSettings[q][myRed] = 255;
              myNeoSettings[q][myGreen] = 255; 
              myNeoSettings[q][myBlue] = 255;
              break;
           }
           
          
          //This will select random colours
          /*
          myNeoSettings[q][myRed] = random(256);
          myNeoSettings[q][myGreen] = random(256); 
          myNeoSettings[q][myBlue] = random(256);
          */

           
           myNeoSettings[q][lightDir] = 1;//as light is out we want it to increase in brightness
           myNeoSettings[q][lightState] = 1;//give the light a brightness value
      }
      if(myNeoSettings[q][0] == 256){//light maxxed so needs to start to turn off
        myNeoSettings[q][lightDir] = 0; //tell system this pixel will be getting dimmer 
        myNeoSettings[q][lightState] = 255;//give the light a brightness value
      }
        
      //this section deals with setting the led brightness value and resetting the millis timer
      
      //get the correct values for the RGB and set the neopixel to is.
      pixels.setPixelColor(q, colourState(myNeoSettings[q][myRed],myNeoSettings[q][lightState]), colourState(myNeoSettings[q][myGreen],myNeoSettings[q][lightState]), colourState(myNeoSettings[q][myBlue],myNeoSettings[q][lightState]));
      pixels.show();
      //increase or decrease the lightState depending if getting brighte or dimmer.
      if(myNeoSettings[q][lightDir] > 0){//getting brighter
        myNeoSettings[q][lightState]++;  
      }else{//dimming
        myNeoSettings[q][lightState]--;  
      }
      if(myNeoSettings[q][lightState] < 0 || myNeoSettings[q][lightState] > 255){
        //at max state so longer value
        myNeoTimers[q] = millis() + random(200, 4000);//0.2 - 4 seconds
      }else{//increasing or decreasing in value so short value
        myNeoTimers[q] = millis() + random(20);//0 - 20 milliseconds  
      }
    }
  }
}
//this function works out what the individual RGB values should be depending on what stage it is in dimming/lighting
int colourState(int pixelMax,int pixelBrightness){
  int returnBrightness;
  //to keep values positive
  returnBrightness = pixelMax * 100;
  //because the value is stored as an int anything beynd the decimal point will be automatically removed.
  //see http://www.digitaltown.co.uk/lesson6bitsandbytes.php for more info.
  returnBrightness = (returnBrightness/255) * pixelBrightness;
  returnBrightness = returnBrightness/100;
  return returnBrightness;
}

Additional Resource Links

Lesson 6: Basic Numeric variables, boolean, byte, int, unsigned int, long, unsigned long 23/07/2021

Lesson 7: delay() v's millis(), controlling timing of programs

Lesson 11: Arrays 28/08/2021

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use Neopixels as a reference.