Learn to code

ARDUINO & CODES

Previous Post
Making an autonomous BeerPong Robot Car with Arduino
Next Post
How to use a relay module with Arduino

Create LED strip effects with Arduino

in Arduino Visual Output

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.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Dupont Wires Dupont Wires × 6
100 uF Capacitor 100 uF Capacitor × 1
WS2812 NeoPixel strip WS2812 NeoPixel strip × 1
DC Barrel Jack DC Barrel Jack × 1
Solder Tin Solder Tin × 1
USB Solder Iron USB Solder Iron × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

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 PICKER

Color:

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() {
 // ---> here we call the effect function <--- } // ---> here we define the effect function <---
// *** REPLACE TO HERE ***

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

//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

//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);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu