Public post

4.2 Create Neopixel strip effects with Arduino

· 4. LED STRIPS

In this tutorial, you will learn how to apply various effects with NeoPixels. You can use these NeoPixels to create awesome projects.

This tutorial will use the NeoPixel framework to create the effects. To be able to change the colors of the LEDs you will need RGB code. Therefore, a color picker is added to this tutorial to enable you to choose the colors that you want.

Materials you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 2x
Breadboard 400 points 1x
100 uF Capacitor 1x
WS2812 NeoPixel strip 1x
DC Barrel Jack 1x

Breadboard Layout

Color Picker

The color picker tool can be used for chosen colors.

The colors of the LEDs in the NeoPixel strip are based on three numbers: RED, GREEN and BLUE (RGB). Every number is one byte thus, every number can contain a value between 0 and 255. In HEX numbers: 00….FF.

The color picker is usefull since we do not know the RGB numbers by head.

You can click on the input field and a pop-up will appear from which you can choose a color. After you have chosen your color the hexadecimal value will appear. You will use this value in the Arduini Sketch to give the LEDs in the NeoPixel strip a color.

  • The first two characters represent RED
  • The second two characters represent Green
  • The last two characters represent Blue

Example:

For the color light green the hexadecimal value is 35FF85

So, RED is 35, GREEN is FF and BLUE is 85.

If you would like to use this in your Arduino sketch, you will have to put “0x” before the number.

strip.Color( 0x35, 0xFF, 0x85);

COLOR PICKERColor:

NeoPixel Framework

For every NeoPixel effect that you are going to create, you will need some basic code. In the basic part of the code you will define the settings of the NeoPixel strip and the library. Do not forget this!

You will use three basic functions which are used by the different effects of the NeoPixel.

showStrip();

This function makes the LEDs in the NeoPixel strip visible.

setPixel(Pixel, red, green, blue)With this function you can adjust the color of each individual LED (pixel) and the RGB values. Remember, the counting of NeoPixel starts with 0. In other words, the first LED in the strip has is 0.

setAll(red, green, blue);

This function will set the entire strip to a color. For instance, setAll(0,0,0); will turn the strip off.

To control NeoPixels you will need to include the NeoPixel library and also define the Arduino Pin that is connected to the NeoPixel strip. In this case it is pin number 3.

Remember, you should connect the datapin of the strip to an PWM pin.
// Basic code NeoPixel 
// wwww.arduinoplatform.com

#include <Adafruit_NeoPixel.h>
#define PIN 5
#define NUM_LEDS 60
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

// *** REPLACE FROM HERE ***
void loop() {
 
void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.show();
 #endif
 
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}
 }
}

Effect – Fade in Fade Out

The Effect Fade in Fade out below fades the colors Red, Green in and out. Since the function is in the loop the function will repeat itself.

// Fade in Fade out Effect NeoPixel with own colors.
// wwww.arduinoplatform.com

void loop() {
 RGBLoop();
}

void RGBLoop(){
  for(int j = 0; j < 3; j++ ) {
    // Fade IN
    for(int k = 0; k < 256; k++) {
      switch(j) {
        case 0: setAll(k,0,0); break;
        case 1: setAll(0,k,0); break;
        case 2: setAll(0,0,k); break;
      }
      showStrip();
      delay(3);
    }

// Fade OUT
    for(int k = 255; k >= 0; k--) {
      switch(j) {
        case 0: setAll(k,0,0); break;
        case 1: setAll(0,k,0); break;
        case 2: setAll(0,0,k); break;
      }
showStrip();
      delay(3);
    }
  }
}

Effect with own colors

It would be more fun to define your own colors that fade in and fade out.

You can choose colors with the color picker above to combine a nice effect.

So if you would like to use the colors red, blue and green you will need to call that function three times (for each color once). You can choose any numbers of colors that you would like to fade in and fade out.

// Fade in Fade out Effect NeoPixel with own colors.
// wwww.arduinoplatform.com

void loop() {
  FadeInOut(0xff, 0x77, 0x00);
}

void FadeInOut(byte red, byte green, byte blue){
  float r, g, b;
     
  for(int k = 0; k < 256; k=k+1) { 
  r = (k/256.0)*red; 
  g = (k/256.0)*green; 
  b = (k/256.0)*blue; 
  setAll(r,g,b); 
  showStrip(); } 
  
  for(int k = 255; k >= 0; k=k-2) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    showStrip();
  }
}

Strobe Effect

Maybe you have seen this function before. The strobe effect turns all the LEDs in the NeoPixel strip on and quickly off. By doing this, it creates a flashing effect that is called strobe.

So this function is quite easy to moderate since you can determine how many milliseconds there should be between the flashes and how long the delay is at the end of the function.

The function uses 6 parameters to create the strobe effect.

  • the first three are for the RED, GREEN and BLUE.
  • StrobeCount determnines the amount of flashes.
  • FlashDelay determines the amount of delay between two flashed.
  • EndPause determines the delay after the flashes are executed.
//Strobe Effect NeoPixel with the color white.
// wwww.arduinoplatform.com

void loop() {
  // Slower:
  // Strobe(0xff, 0x77, 0x00, 10, 100, 1000);
  // Fast:
  Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
}

void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause){
  for(int j = 0; j < StrobeCount; j++) {
    setAll(red,green,blue);
    showStrip();
    delay(FlashDelay);
    setAll(0,0,0);
    showStrip();
    delay(FlashDelay);
  }
 
 delay(EndPause);
}

Twinkle Effect

With this effect 1 or more of the LEDs in the strip will light up randomly in a given color.

  • there are three parameters for the colors
  • the 4th parameter (count) determines how many LEDs should light up
  • The 5th parameter determines the delay.
  • The 6th parameter (OnlyOne) will be set on true if you would like to see only 1 LED light up, set it on FALSE if you would like to see all LEDs

In the example below, we want to see 10 LEDs with a twinkle effect with a delay of 0.1 second.

//Twinkle Effect
// wwww.arduinoplatform.com

void loop() {
  Twinkle(0xff, 0, 0, 10, 100, false);
}

void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
  setAll(0,0,0);
 
  for (int i=0; i<Count; i++) {
     setPixel(random(NUM_LEDS),red,green,blue);
     showStrip();
     delay(SpeedDelay);
     if(OnlyOne) {
       setAll(0,0,0);
     }
   }
 
  delay(SpeedDelay);
}

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member

Comments

Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.