Project 5.00 Using the Neopixels

In this project, we’ll turn the Neopixels purple. We are going to use a library for this project. Most libraries are not built-in to Arduino. To add this library, go to tools -> Manage Libraries and type in “Adafruit Neopixel”. The library you’re looking for looks like this:

Next, click “Install”.

Project Code:

////////////////////////////////////////////////////////
// 5.00 - Using The NeoPixel

#include <Adafruit_NeoPixel.h>

byte dataPin = 10;        
byte numberOfPixels = 2;  
byte brightness = 10;     

byte redValue = 255;      
byte greenValue = 0;      
byte blueValue = 127;     

Adafruit_NeoPixel pixels(numberOfPixels, dataPin, NEO_GRB + NEO_KHZ800);
void setup() {
  pixels.begin();                     
  pixels.setBrightness(brightness);   

  pixels.setPixelColor(0, pixels.Color(redValue, greenValue, blueValue)); 
  pixels.setPixelColor(1, pixels.Color(redValue, greenValue, blueValue)); 

  pixels.show(); 
}

void loop() {
}
////////////////////////////////////////////////////////

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

In order to use a library that is not built-in to Arduino, a library header needs to be added. This tells the program to compile that library along with the code you’ve written.

This does not need done for this sketch (unless you’re typing from scratch), as it’s already been included. For reference, this can be done by going to sketch -> include library -> Adafruit Neopixel. This will add the following line to your program:

#include <Adafruit_NeoPixel.h>

Six byte variables are used in this program: dataPin represents the pin that is connected to the first Neopixel, numberOfPixels represents the number of Neopixels connected to the board, brightness represents the max brightness of the Neopixels, redValue greenValue blueValue respectively represent their portion of the Neopixel light being emitted.

byte dataPin = 10
byte numberOfPixels = 2; 
byte brightness = 10;     

byte redValue = 255;      
byte greenValue = 0;      
byte blueValue = 127;     

After the variable declarations, there is what’s called a constructor. Constructors are used to set things up in many libraries, but not all. It’s not important to understand constructors right now, but just know they create an object that we can use with the library. This constructor needs to know how many Neopixels are connected in a row, what pin they are connected to, and what type of Neopixels they are.

Adafruit_NeoPixel pixels(numberOfPixels, dataPin, NEO_GRB + NEO_KHZ800); 

For now you can think of “Adafruit_Neopixel” in the constructor as a sort of variable type like int, float, char, ect.. “pixels” is just a common name to represent the object initialized by the constructor. It could be any name, like a normal variable. In contrast to a normal variable, objects are used with the “.” (dot) operator. This dot operator allows you to access functions specific to that object.

In the setup() function, we first initialize the Neopixel strip and set its brightness.

pixels.begin();
pixels.setBrightness(brightness);   

Next, we load the color of the neopixels using setPixelColor and Color. The setPixelColor function takes the pixel number, and a color. This second color parameter is passed with the Color function. This function allows us to pass an RGB value to the setPixelColor function. The color values passed to Color are 8-bit values (0-255).

pixels.setPixelColor(0, pixels.Color(redValue, greenValue, blueValue)); 
pixels.setPixelColor(1, pixels.Color(redValue, greenValue, blueValue)); 

Lastly, we need to update the Neopixels. In order to do so, all we need to do is call the show() function. This function needs to be called anytime that the Neopixel needs updated. When you set the pixel’s color using setPixelColor that value is stored in program memory but not actually displayed. To display the new color, we need to call pixels.show(). If you only load the color but don’t call this function, nothing will happen.

pixels.show(); //<- You must call this every time the Neopixel color needs updated.

You might be wondering how you know what functions you can access with your object. Usually there is a class reference that can be found online that details the library and the accompanying functions. This library class reference is located at:

https://adafruit.github.io/Adafruit_NeoPixel/html/class_adafruit___neo_pixel.html

You can also find examples of any library that you’ve installed by going to file -> examples and hovering over the library. This is a great way to learn how to set up and use new libraries and devices.

Previous
Previous

Project 5.01 Cycling Colors with a Button

Next
Next

Project 4.04 There is a library for that