Lighting up a whole room, a staircase or a festival display takes hundreds of LEDs — and that is where the Mega’s 8 KB of RAM matters. A 5-metre, 300-LED strip uses 900 bytes of buffer, which the Mega handles comfortably.
The data comes from pin 6 through a 330 Ω resistor. The power does not: the strip runs from its own 5 V supply, with the grounds joined. The sketch runs a colourful comet that fades as it goes.
Wiring
| Arduino Mega 2560 | WS2812B addressable RGB LED strip (NeoPixel) | Wire | Notes |
|---|---|---|---|
| D6 | DIN | green | Via 330 Ω — Resistor close to the first LED. |
| 5 V 10 A SMPS · + | 5V | red | 1000 µF across + and − at the strip input. Inject 5 V again at the far end of a 5 m strip. |
| 5 V 10 A SMPS · - | GND | black | |
| GND | GND | black | Common ground between Mega and supply — the data signal needs it. |
5 V (about 60 mA per LED at full white) part, 5 V board. The strip expects a data HIGH of about 3.5 V at 5 V supply. From ESP32 or Pico the 3.3 V signal often works with short wires, but to be safe drive DIN through a 74AHCT125 (or 74HCT125) powered from 5 V.
Board quirks
- Each LED takes 3 bytes of RAM: 300 LEDs = 900 bytes, about 11 % of the Mega’s 8 KB. The same strip would eat almost half an Uno’s RAM.
strip.show()for 300 LEDs takes about 9 ms with interrupts off. Serial input can drop bytes at high baud rates during that time.- Never power 300 LEDs through the Mega. At full white they could draw 18 A;
setBrightness(60)keeps this animation to a few amps. - Feed 5 V at both ends (or every 2–3 m) with thick wire, or the far end turns yellow-brown.
- The SMPS’s 230 V terminals must be covered and wired by someone qualified. Only the 5 V side is part of this build.
Code
// Long WS2812B strip (300 LEDs = 5 m at 60/m) on an Arduino Mega 2560: a running "comet".
// DIN → D6 through 330 Ω, strip 5V and GND from a 5 V 10 A supply, supply GND joined to Mega GND.
// 300 LEDs use 900 bytes of RAM — easy on the Mega's 8 KB.
#include <Adafruit_NeoPixel.h>
const uint8_t DATA_PIN = 6;
const uint16_t NUM_LEDS = 300;
Adafruit_NeoPixel strip(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
strip.begin();
strip.setBrightness(60); // ~25 %: caps worst-case current around 4-5 A for 300 LEDs
strip.clear();
strip.show();
Serial.print(F("Strip ready, free RAM after begin: "));
extern int __heap_start, *__brkval;
int v;
Serial.println((int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval));
}
void loop() {
static uint16_t pos = 0;
static uint16_t hue = 0;
for (uint16_t i = 0; i < NUM_LEDS; i++) { // fade every LED a little
uint32_t c = strip.getPixelColor(i);
uint8_t r = (c >> 16) * 3 / 4, g = (c >> 8 & 0xFF) * 3 / 4, b = (c & 0xFF) * 3 / 4;
strip.setPixelColor(i, r, g, b);
}
strip.setPixelColor(pos, strip.ColorHSV(hue));
strip.show(); // ~9 ms for 300 LEDs; interrupts are off meanwhile
pos = (pos + 1) % NUM_LEDS;
hue += 200;
delay(10);
}// Long WS2812B strip (300 LEDs = 5 m at 60/m) on an Arduino Mega 2560: a running "comet".
// DIN → D6 through 330 Ω, strip 5V and GND from a 5 V 10 A supply, supply GND joined to Mega GND.
// 300 LEDs use 900 bytes of RAM — easy on the Mega's 8 KB.
#include <Adafruit_NeoPixel.h>
const uint8_t DATA_PIN = 6;
const uint16_t NUM_LEDS = 300;
Adafruit_NeoPixel strip(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
strip.begin();
strip.setBrightness(60); // ~25 %: caps worst-case current around 4-5 A for 300 LEDs
strip.clear();
strip.show();
Serial.print(F("Strip ready, free RAM after begin: "));
extern int __heap_start, *__brkval;
int v;
Serial.println((int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval));
}
void loop() {
static uint16_t pos = 0;
static uint16_t hue = 0;
for (uint16_t i = 0; i < NUM_LEDS; i++) { // fade every LED a little
uint32_t c = strip.getPixelColor(i);
uint8_t r = (c >> 16) * 3 / 4, g = (c >> 8 & 0xFF) * 3 / 4, b = (c & 0xFF) * 3 / 4;
strip.setPixelColor(i, r, g, b);
}
strip.setPixelColor(pos, strip.ColorHSV(hue));
strip.show(); // ~9 ms for 300 LEDs; interrupts are off meanwhile
pos = (pos + 1) % NUM_LEDS;
hue += 200;
delay(10);
}Library: Adafruit NeoPixel (Adafruit) — compiled with Adafruit NeoPixel 1.15.5.
Expected serial output
Strip ready, free RAM after begin: 6934Strip ready, free RAM after begin: 6934
Common errors
First LED flickers or wrong colours further down
No common GND between Mega and supply, or no 330 Ω resistor. Add both.
Colours fade to orange towards the end
Voltage drop along the strip. Inject power at the far end.