Arduino library 路 LEDs

Adafruit NeoPixel: WS2812B strips, rings and matrices

NeoPixel is Adafruit鈥檚 name for the WS2812B and its relatives: RGB LEDs with a tiny controller in each one, chained on a single data wire. The first LED takes the first 24 bits of color it receives and passes the rest on, so one Arduino pin can set every LED of a strip, ring or panel to its own color.

Built in on Mokxi: #include <Adafruit_NeoPixel.h> and it builds, with nothing to install. Free, and no account needed.

What Adafruit NeoPixel does

The library keeps a color for every LED in RAM (three bytes each, four for RGBW) and sends them all out when you call show(). The signal timing is strict, so the library turns interrupts off while it sends. On an AVR board that briefly stops millis() and can drop Serial bytes during a long strip鈥檚 update.

Power is the part to plan. Each LED draws up to about 60 mA at full white, so a 60 LED strip can need 3.6 A, far more than a board鈥檚 5 V pin can supply. Give the strip its own 5 V supply with a common ground, put a 300 to 500 惟 resistor in the data line and a large capacitor across the strip鈥檚 power input.

Library Manager nameAdafruit NeoPixel
AuthorAdafruit
Include#include <Adafruit_NeoPixel.h>
BoardsArduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ATtiny85, ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, Raspberry Pi Pico, Raspberry Pi Pico W, Arduino Nano RP2040 Connect, Seeed XIAO RP2040, Waveshare RP2040-Zero, Arduino Uno R4, Arduino Uno R4 WiFi
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesAddressable RGB LEDs

Install it

It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ATtiny85, ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, Raspberry Pi Pico, Raspberry Pi Pico W, Arduino Nano RP2040 Connect, Seeed XIAO RP2040, Waveshare RP2040-Zero, Arduino Uno R4, Arduino Uno R4 WiFi.

In Mokxi it is built in: #include <Adafruit_NeoPixel.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit NeoPixel" by Adafruit from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit NeoPixel to lib_deps in PlatformIO. Mokxi follows the calls of release 1.12.3, so a sketch written here builds there too.

What to know in the simulator: The bit is timed in AVR assembly on the AVR boards, by a counted loop on the ESP32-C3 and C6, and on the core cycle counter on the ESP32, ESP32-S3, Pico and Uno R4 (the real cores use RMT or PIO, which are not modeled). RGBW strips are not here.

Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 Arduino Pro Mini 3.3 V 路 ATtiny85 路 ESP32-C3 路 ESP32-C6 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32 DevKit 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 ESP32-S3-DevKitC-1 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 路 Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

Adafruit_NeoPixel strip(60, 6, NEO_GRB + NEO_KHZ800)

Sixty LEDs on pin 6, with the color order and speed of a WS2812B. Older WS2811 strips may need NEO_RGB or NEO_KHZ400.

strip.begin()

Sets the pin up and allocates the color buffer. Call strip.show() right after to turn every LED off.

strip.setPixelColor(i, strip.Color(255, 0, 0))

Sets LED i (counting from 0) to red in the buffer. Nothing changes on the strip until show().

strip.show()

Sends the whole buffer down the data line. Call it once after setting all the pixels for a frame.

for (int i = 0; i < strip.numPixels(); i++) {
  strip.setPixelColor(i, strip.Color(0, 0, 40));
}
strip.show();

strip.ColorHSV(hue)

A color from a hue of 0 to 65535 around the color wheel, at full saturation and brightness. Wrap it in gamma32() for more natural fades.

for (int i = 0; i < strip.numPixels(); i++) {
  uint16_t hue = first + i * 65536L / strip.numPixels();
  strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(hue)));
}
strip.show();

strip.setBrightness(50)

Scales every LED, from 0 to 255. It is meant to be set once in setup(); calling it repeatedly loses color detail.

strip.fill(strip.Color(0, 255, 0), 0, 10)

Sets a run of LEDs, here the first ten, to one color. clear() sets them all to off.

Example: Red, green and blue on a NeoPixel strip

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino 路 Arduino Uno R3
#include <Adafruit_NeoPixel.h>

#define LED_PIN 6
#define NUMPIXELS 8

Adafruit_NeoPixel pixels(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  pixels.begin();
  pixels.clear();
  pixels.setPixelColor(0, pixels.Color(150, 0, 0));
  pixels.setPixelColor(1, pixels.Color(0, 150, 0));
  pixels.setPixelColor(2, pixels.Color(0, 0, 150));
  pixels.show();
  Serial.println("shown");
}

void loop() {
  // A slow wheel on the last LED, so there is something to watch.
  static uint16_t hue = 0;
  pixels.setPixelColor(NUMPIXELS - 1, pixels.ColorHSV(hue, 255, 64));
  pixels.show();
  hue += 512;
  delay(50);
}

Wiring

  • Arduino Uno R3 6 to Addressable RGB LEDs DIN
  • Arduino Uno R3 5V to Addressable RGB LEDs VDD
  • Arduino Uno R3 GND to Addressable RGB LEDs VSS

Open it in the editor, wired and ready to run

Common errors and how to fix them

Red shows as green and green as red

The color order in the constructor does not match the strip. WS2812B is NEO_GRB; if red and green are swapped, try NEO_RGB, and NEO_RGBW for a strip with a white LED in each pixel.

Nothing lights, or only the first LED shows a random color

Check that the data wire goes into the strip鈥檚 DIN end (the arrows point away from it), that the strip and the board share ground, and that strip.show() is called after setting colors.

Colors flicker or go wrong at the far end of the strip

Usually power: the voltage sags along a long strip at high brightness. Feed 5 V into both ends, lower setBrightness(), and add the data resistor and supply capacitor.

millis() runs slow or Serial loses characters

Interrupts are off while show() sends data, about 30 microseconds per LED. Update the strip only when something changes, or use fewer LEDs per update.

Try Adafruit NeoPixel without installing anything

Open the example, change the sketch, and keep your version in a free account.