NEOPIXEL WORKING AND DEMO

NEOPIXEL WORKING AND DEMO - Robocraze

Neopixels are small colourful LEDs which can be chained together in the form of rings, strips or virtually any shape you wish and controlled using a microcontroller.

 

 

In this blog, we will interface a Neopixel strip consisting of 30 Neopixel LEDs with an Arduino Uno and display various colourful patterns on it.

The Neopixel strip consists only of three pins that need to be connected to the Arduino. These are as follows,

  • VCC connected to 5v of Arduino
  • DIN connected to pin 8 of Arduino
  • GND connected to the pin ground of Arduino

We will be using the Adafruit Neopixel library in our code. Let us now go over the code once.

 First, let us look at a basic application where we simply put a single colour across the led strip.

 

 

#include <Adafruit_NeoPixel.h>

#define PIN 8

#define NUM_LEDS 30 //the number of Neopixel LEDs in the strip

#define BRIGHTNESS 100

 

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

 

void colorWipe(uint32_t c, uint8_t wait)

{

 for (uint16_t i = 0; i < strip.numPixels(); i++)

 {

   strip.setPixelColor(i, c);

   strip.show();

   delay(wait);

 }

}

 

void setup()

{

 strip.setBrightness(BRIGHTNESS);

 strip.begin();

 strip.show(); // Initialize all pixels to off

}

 

void loop()

{

 colorWipe(strip.Color(255, 0, 0), 50);     // Red

 colorWipe(strip.Color(0, 255, 0), 50);     // Green

 colorWipe(strip.Color(0, 0, 255), 50);     // Blue

 colorWipe(strip.Color(255, 255, 255), 50); // White

}

Here you can see the colorWipe() function which basically iterates over all the LEDs in the strip and sets the colour of each LED to the colour passed. The first parameter is the colour and the second parameter is the delay of propagation of colour to the next LED in milliseconds.

 

The strip.Color() function converts the RGB colour format to a colour bitmask. It takes three parameters which are the red, green and blue component of the desired colour.

 

The strip.show() function makes the set colour to be sent to and reflected on all the LEDs.

 

Let us now look at a slightly more advanced application

 

#include <Adafruit_NeoPixel.h>

#define PIN 8

#define NUM_LEDS 30 //put the number of Neopixel LEDs

#define BRIGHTNESS 100

 

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

 

uint8_t red(uint32_t c) {  return (c >> 16); }

uint8_t green(uint32_t c) {  return (c >> 8); }

uint8_t blue(uint32_t c) {  return (c); }

 

uint32_t Wheel(byte WheelPos)

{

 WheelPos = 255 - WheelPos;

 if (WheelPos < 85)

 {

   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);

 }

 if (WheelPos < 170)

 {

   WheelPos -= 85;

   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);

 }

 WheelPos -= 170;

 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);

}

 

void rainbowFade2White(uint8_t wait, int rainbowLoops)

{

 float fadeMax = 100.0;

 int fadeVal = 0;

 uint32_t wheelVal;

 int redVal, greenVal, blueVal;

 

 for (int k = 0; k < rainbowLoops; k++)

 {

 

   for (int j = 0; j < 256; j++)

   { // 5 cycles of all colours on wheel

 

     for (int i = 0; i < strip.numPixels(); i++)

     {

       wheelVal = Wheel(((i * 256 / strip.numPixels()) + j) & 255);

       redVal = red(wheelVal) * float(fadeVal / fadeMax);

       greenVal = green(wheelVal) * float(fadeVal / fadeMax);

       blueVal = blue(wheelVal) * float(fadeVal / fadeMax);

 

       strip.setPixelColor(i, strip.Color(redVal, greenVal, blueVal));

     }

 

     //First loop, fade in!

     if (k == 0 && fadeVal < fadeMax - 1)

     {

       fadeVal++;

     }

 

     //Last loop, fade out!

     else if (k == rainbowLoops - 1 && j > 255 - fadeMax)

     {

       fadeVal--;

     }

 

     strip.show();

     delay(wait);

   }

 }

 delay(500);

}

 

void setup()

{

 strip.setBrightness(BRIGHTNESS);

 strip.begin();

 strip.show(); // Initialize all pixels to off

}

 

void loop()

{

 rainbowFade2White(30, 3);

}

 

 

Wishing you a Merry Christmas and a Happy New Year 2019.

Team Robocraze

 

 

Components and Supplies

    You may also like to read

    Frequently Asked Questions

    Back to blog

    Leave a comment

    Please note, comments need to be approved before they are published.

    Components and Supplies

      You may also like to read