Arduino Library – Adafruit NeoPixel
Installatie van Arduino IDE libraries: Arduino info
Informatie (ENG)
Arduino library for controlling single-wire-based LED pixels and strip such as the:
– Adafruit 60 LED/meter Digital LED strip.
– Adafruit FLORA RGB Smart Pixel.
– Adafruit Breadboard-friendly RGB Smart Pixel.
– Adafruit NeoPixel Stick.
– Adafruit NeoPixel Shield.
It’s assumed at this point that you have the Adafruit_NeoPixel library for Arduino installed and have run the strandtest example sketch successfully. If not, return to the prior page for directions to set that up.
To learn about writing your own NeoPixel sketches, let’s begin by dissecting the strandtest sketch…
All NeoPixel sketches begin by including the header file:
#include <Adafruit_NeoPixel.h>
1 2 3 4 5 6 7 8 9 10 |
#define PIN 6 // Parameter 1 = number of pixels in strip // Parameter 2 = pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800); |
The first line assigns a number to the symbol “PIN” for later reference. It doesn’t need to be done this way, but makes it easier to change the pin where the NeoPixels are connected without digging deeper into the code.
- The number of sequential NeoPixels in the strip. In the example this is set to 60, equal to 1 meter of medium-density strip. Change this to match the actual number you’re using.
- The pin number to which the NeoPixel strip (or other device) is connected. Normally this would be a number, but we previously declared the symbol PIN to refer to it by name here.
- A value indicating the type of NeoPixels that are connected. In most cases you can leave this off and pass just two arguments; the example code is just being extra descriptive. If you have a supply of classic “V1” Flora pixels, those require NEO_KHZ400 + NEO_RGB to be passed here.
1 2 3 4 |
void setup() { strip.begin(); strip.show(); // Initialize all pixels to 'off' } |
The second line, strip.show(), isn’t absolutely necessary, it’s just there to be thorough. That function pushes data out to the pixels…since no colors have been set yet, this initializes all the NeoPixels to an initial “off” state in case some were left lit by a prior program.
In the strandtest example, loop() doesn’t set any pixel colors on its own — it calls other functions that create animated effects. So let’s ignore it for now and look ahead, inside the individual functions, to see how the strip is controlled.
There are two ways to set the color of a pixel. The first is:
The first argument — n in this example — is the pixel number along the strip, starting from 0 closest to the Arduino. If you have a strip of 30 pixels, they’re numbered 0 through 29. It’s a computer thing. You’ll see various places in the code using a forloop, passing the loop counter variable as the pixel number to this function, to set the values of multiple pixels.
The next three arguments are the pixel color, expressed as red, green and blue brightness levels, where 0 is dimmest (off) and255 is maximum brightness.
To set the 12th pixel (#11, counting from 0) to magenta (red + blue), you could write:
Here, color is a 32-bit type that merges the red, green and blue values into a single number. This is sometimes easier or faster for some (but not all) programs to work with; you’ll see the strandtest code uses both syntaxes in different places.
You can also convert separate red, green and blue values into a single 32-bit type for later use:
Just like setPixel(), this does not have an immediate effect. You need to follow this with a call to show().
setBrightness() was intended to be called once, in setup(), to limit the current/brightness of the LEDs throughout the life of the sketch. It is not intended as an animation effect itself! The operation of this function is “lossy” — it modifies the current pixel data in RAM, not in the show() call — in order to meet NeoPixels’ strict timing requirements. Certain animation effects are better served by leaving the brightness setting alone, modulating pixel brightness in your own sketch logic and redrawing the full strip with setPixel().
I’m calling setPixel() but nothing’s happening!
There are two main culprits for this:
- forgetting to call strip.begin() in setup().
- forgetting to call strip.show() after setting pixel colors.
Another (less common) possibility is running out of RAM — see the last section below. If the program sort of works but has unpredictable results, consider that.
Can I have multiple NeoPixel objects on different pins?
1 2 |
Adafruit_NeoPixel strip_a = Adafruit_NeoPixel(16, 5); Adafruit_NeoPixel strip_b = Adafruit_NeoPixel(16, 6); |
The above declares two distinct NeoPixel objects, one each on pins 5 and 6, each containing 16 pixels and using the implied default type (NEO_KHZ800 + NEO_GRB).
Can I connect multiple NeoPixel strips to the same Arduino pin?
I’m getting the wrong colors. Red and blue are swapped!
The colors fall apart when I use setBrightness() repeatedly!
See note above; setBrightness() is designed as a one-time setup function, not an animation effect.
Also see the “Advanced Coding” page — there’s an alternative library that includes “nondestructive” brightness adjustment, among other features!
Pixels Gobble RAM
Each NeoPixel requires about 3 bytes of RAM. This doesn’t sound like very much, but when you start using dozens or even hundreds of pixels, and consider that the mainstream Arduino Uno only has 2 kilobytes of RAM (often much less after other libraries stake their claim), this can be a real problem!
For using really large numbers of LEDs, you might need to step up to a more potent board like the Arduino Mega or Due. But if you’re close and need just a little extra space, you can sometimes tweak your code to be more RAM-efficient. This tutorial has some pointers on memory usage.
[#/arduino/libraries/adafruit_neopixel” ]