NeoPixels, How Do They Work? © GPL3+

A very basic intro into some very cool LEDs!

  • 9 views
  • 0 comments
  • 2 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
The microcontroller of choice
×1
Neopixel strip
NeoPixel strip
My strip has 8 on it but any length will work
×1

Apps and online services

About this project

Intro: NeoPixels, How Do They Work?

For the last month or so I've been working on a project involving a NeoPixel, I chose to use a NeoPixel instead of the usual RGB LED strip because I felt that the individual addressable LEDs would be easier to work and create better effects. While this is true I did not take into account the learning curb needed to start working with the NeoPixel library and I ended up spending most of my time trying to figure out how to use it.

Well after figuring out basics I thought I would make a quick tutorial to show you all how the basics of the NeoPixel work!

Step 1: What Is a NeoPixel?

Before we jump into learning about the library that helps us control them lets take a second to talk about NeoPixels.

These are individually addressable LEDs all housed on a string that can be controlled from a single pin on a microcontroller. This means one pin can control all of the LEDs colors and which LEDs are on at any given time. When compared to a normal RGB LED you'll notice that we need 3 pins to control the RedGreen and Blue value and all LEDs have to remain on or off. So as you can see using individually addressable can help create some cool effects.

Lots of companies sell strips of addressable LEDs however the most popular is the NeoPxiel by Adafruit. Adafruit produces a line of addressable LED strip and also supplies the library to control them, this is called "Adafruit NeoPixel", there are other companies that have produced libraries to control these addressable LEDs however, in my opinion, I've found that Adafruits has the best support and is the easiest to get started one.

Step 2: Install the Library

So we have decided we are going to use the Adafruit library to control our NeoPixel, how do we install it?

Well, first things first you need to have the Arduino IDE installed on your computer which can be downloaded from here. Once that's all loaded up go ahead and launch the Arduino IDE and do the following:

  • Click on "Sketch" and then hover over "Include library"
  • Click on "Manage library"

You should now see a pop-up window open, this is where we install and update new libraries.

  • In the search bar, search for "Adafruit NeoPixel"
  • Click on the one named "Adafruit NeoPixel"
  • Click "Install"

Once that finishes loading the NeoPixel library will now be in your IDE. You can test this by checking to see if the examples loaded into your IDE:

  • Click on "File" and then hover over "Examples"
  • Somewhere on the list, you should see "Adafruit NeoPixel"
  • Click on "Adafruit NeoPixel" too see all the examples

At this point, the library is successfully loaded into our Arduino IDE and we can now move on to checking out the examples.

Step 3: Examples

Now if you clicked on any of the examples you may be a little surprised at how much code is involved in getting one of these to light up. The "Standardtest" example has 6 extra functions! Controlling one of these is definitely more challenging than just setting the Red Green or Blue pin to high like on a standard RGB strip but it's worth figuring out as it allows you to do some really cool stuff.

"Standardtest" is actually one of the more advanced examples so lets start of by loading in a simple example and what simpler example than the "simple" example:

  • Click on "File" and then hover over "Examples"
  • Click on "Adafruit NeoPixel"
  • Click on "Simple"

Now with the simple example open in our IDE we can get started.

Step 4: Simple Example

There's a lot of important code happening in this example so we are going to take a look at it piece by piece.

At the very top of the example we have the following code:

#include <Adafruit_NeoPixel.h>

This tells the Arduino that we want to use the NeoPixel and all the properties it brings in this sketch, the sketch would not function without this.

Next, we see the following:

#ifdef __AVR__
#include <AVR/power.h>
#endif

This states that if we are using an AVR board such as the Trinket or Gemma to include a library that is compatible with them. This has no effect if you are using an Arduino.

Next, we see two very important lines of code:

#define PIN 6
#define NUMPIXELS 16

The first value named "PIN" is used to tell the sketch which pin we want to use on the microcontroller. If you wanted your NeoPixel to be controlled from pin 8 on your microcontroller you would need to change "6" to "8".

The value named "NUMPIXELS" is used to tell the string how many Pixels there are on your specific string. For example, my string has 8 Pixels so I changed the value to 8. This value helps the sketch keep track of how many Pixels are on your string allowing us to create functions using this value which we will do later on.

Next, we have a line of code that references the values we defined already:

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

This code doesn't really serve a purpose for us, its there to help the microcontroller communicate with the NeoPixel. You may notice that those values we defined are here, we could remove the words "NUMPIXELS" and "PIN" and just replace them with the value if we wanted to.

Next, we have a delay value which we reference later.

int delayval = 500;

This is pretty simple its just being stated that "delayval" = 500 which is half a second

Now we see the Void Setup:

void setup() {
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
pixel.begin();
}

The first couple of lines of code are again just there to make this work with the trinket and other microcontrollers of that class so we don't really need to worry about it. "Pixel.begin" is important though, this tells the sketch to initialize the library.

Next, we see the Void Loop. This is where everything interesting happens.

for(int i=0;i<NUMPIXELS;i++){

Now, this code is quite complex and is used to create a function, we are going to come back to this in the next step.

Next, we see the code that controlls everything

pixels.setPixelColor(i, pixels.Color(0,150,0));

The code starts out with "pixels.setPixelColor" so it seems reasonsable to belive that this code is going to allow us to change the color of the Pixel. The first value is "i" now this is the part of the code were we tell it exactly which pixel we want to be lit up, right now its set to "i" because its part of that function we spoke about earlier but if I change this value from "i" to "3" and then upload the code the third pixel on the string will light up. This is how we control indavidual pixels.

After this we see "pixels.Color(0, 150, 0)" these are the values we change to change the color, the first value is Red the second value is Green and the final value is Blue. Right now the values are set to "(0, 150, 0)" which means the the strip will be light green (0 means the color is off and 255 is the brightest the color can go). If I were to change this to "(0, 0, 255)" and upload it the strip would then become very bright blue. We can also mix colors like this "(150, 0, 150)" to make violate.

Next we have our last peice of code which is:

pixels.show();
delay(delayval);

"pixels.show" sends the current settings to the pixel, if we didnt have this it would never update the pixel and it would stay the same. The "delay" is there to stop the pixel from updating too quickly and will play a bif role in the fucntion we are going to make in the next step.

This was a very long stop but we learned the key basics of how to control the NeoPixel, in this step we learned about how to set individual LEDs on or off and how to set the colors of those individual NeoPixels.

Step 5: Simple Example Functions

Now with all of the basic setup code out of the way lets take a look at the basic function I was talking about earlier.

The function we are going to create will turn on each NeoPixel after each other, so the first one will light up then a short delay will happen then the second one will light and followed by another short delay all the way to the 8th NeoPixel.

For this we use this code:

for(int i=0;i<NUMPIXELS;i++){

and this:

pixels.setPixelColor(i, pixels.Color(0,150,0));

Youll notices the letter "i" in the space where there should be a number which is used to set which NeoPixel turns on. you'll also notice the "i" is also in the first piece of code as "i=0" and "i++".

What's happening here is we are saying "i" = to the first NeoPixel on the string (i=0) (The first NeoPixel on the string is 0, the second is 1, the third is 2.....the eighth is 7) and then we are saying that we need to increase "i" by 1 every loop "i++" and then finally when we are asked which NeoPixel we want to light up we give it the value "i" so everytime the loop runs the value of "i" increase by 1 and the next NeoPixel lights up.

Go ahead and upload this and you should see the first Neopixel light up followed by the second followed by the third until they are all lit up.

Now take a look at this code:

for(int i=7;i<NUMPIXEL,i--)}

In this version of the code "i" starts on 7 and has "i--" which will decrease the value of "i" by 1 every loop which means that the last Neopixel will light up followed by the second last all the way until "i=0" where the first one will light up.

Step 6: Thats All!

And that's the basics of how the Adafruit NeoPixel library works. I hope you found that useful and if you have any questions or concerns please feel free to send me a message or drop a comment below!

Code

SimplePlain text
By Adafruit
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      16

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

  for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
}

Comments

Similar projects you might like

Arduino Bluetooth Basic Tutorial

by Mayoogh Girish

  • 455,488 views
  • 42 comments
  • 240 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 289,917 views
  • 96 comments
  • 678 respects

Security Access Using RFID Reader

by Aritro Mukherjee

  • 236,921 views
  • 42 comments
  • 250 respects

OpenCat

Project in progress by Team Petoi

  • 200,708 views
  • 161 comments
  • 1,403 respects
Add projectSign up / Login