# controlling WS2812 with arduino mega 2560

**URL:** <https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439>\
**Category:** LEDs and Multiplexing\
**Created:** [August 2, 2017, 8:51pm UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439 "2017-08-02T20:51:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobi3492](https://avatars.discourse-cdn.com/v4/letter/t/e99b99/32.png) [@tobi3492](https://forum.arduino.cc/u/tobi3492)\
**Post date:** [August 2, 2017, 8:51pm UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439/1 "2017-08-02T20:51:48Z")

</div>

Hello guys

i bought an Arduino mega 2560 R3 ATmega328P on ebay, 5 meter WS2812 RGB led strip and a 5V 60A power supply, i have sit many hours trying to figure out how to get the led strip to light up, i know the power supply work because when i connect all the wires the 1st. and the 3rd. led lights up and if i touch the pins the colors change.  
I have done a lot of researching but i cant get it to work for my self, so i really hope someone in the forum can help me.

i post a picture of how i have wired the borad, power supply and led strip together. under the text.

the code i use is the FastLED

* * *

#include "FastLED.h"

// How many leds in your strip?  
#define NUM\_LEDS 300

// For led chips like Neopixels, which have a data line, ground, and power, you just  
// need to define DATA\_PIN. For led chipsets that are SPI based (four wires - data, clock,  
// ground, and power), like the LPD8806 define both DATA\_PIN and CLOCK\_PIN  
#define DATA\_PIN 6  
#define CLOCK\_PIN 13

// Define the array of leds  
CRGB leds[NUM\_LEDS];

void setup() {  
// Uncomment/edit one of the following lines for your leds arrangement.  
// FastLED.addLeds\<TM1803, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<TM1804, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<TM1809, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<WS2811, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<WS2812, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<WS2812B, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
FastLED.addLeds\<NEOPIXEL, DATA\_PIN\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<APA104, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<UCS1903, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<UCS1903B, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<GW6205, DATA\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<GW6205\_400, DATA\_PIN, RGB\>(leds, NUM\_LEDS);

// FastLED.addLeds\<WS2801, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<SM16716, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<LPD8806, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<P9813, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<APA102, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<DOTSTAR, RGB\>(leds, NUM\_LEDS);

// FastLED.addLeds\<WS2801, DATA\_PIN, CLOCK\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<SM16716, DATA\_PIN, CLOCK\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<LPD8806, DATA\_PIN, CLOCK\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<P9813, DATA\_PIN, CLOCK\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<APA102, DATA\_PIN, CLOCK\_PIN, RGB\>(leds, NUM\_LEDS);  
// FastLED.addLeds\<DOTSTAR, DATA\_PIN, CLOCK\_PIN, RGB\>(leds, NUM\_LEDS);  
}

void loop() {  
// Turn the LED on, then pause  
leds[0] = CRGB::Red;  
FastLED.show();  
delay(500);  
// Now turn the LED off, then pause  
leds[0] = CRGB::Black;  
FastLED.show();  
delay(500);  
}

 ![arduino picture.png](https://europe1.discourse-cdn.com/arduino/original/4X/1/b/c/1bc074ab058ae868cc2e1bd6a047606ad1fcf2a7.jpeg)

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [August 2, 2017, 9:14pm UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439/2 "2017-08-02T21:14:31Z")

</div>

Adding **//** changes a line from code into a comment.  
Removing those slashes changes it from comment to code.

The first comments tell you to remove (comment) #define CLOCK\_PIN 13 if the strip hasn't got a clock line.  
// #define CLOCK\_PIN 13

FastLED.addLeds\<NEOPIXEL, DATA\_PIN\>(leds, NUM\_LEDS); is now active (uncommented).  
I don't think you have Neopixels, so comment that line and uncomment the right one.  
Leo..

---

<div class="post-metadata">

**Author:** ![tobi3492](https://avatars.discourse-cdn.com/v4/letter/t/e99b99/32.png) [@tobi3492](https://forum.arduino.cc/u/tobi3492)\
**Post date:** [August 2, 2017, 9:29pm UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439/3 "2017-08-02T21:29:00Z")

</div>

hello Wawa

thanks for the qiuck reply but i think i posted the wrong code... my fault sorry.

the right code is comming here, i really hope you want to look at it and see if you can see soething tat is wrong. 🙂

```cpp
#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 64 

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 6
#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() { 
	Serial.begin(57600);
	Serial.println("resetting");
	LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
	LEDS.setBrightness(84);
}
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }

void loop() { 
	static uint8_t hue = 0;
	Serial.print("x");
	// First slide the led in one direction
	for(int i = 0; i < NUM_LEDS; i++) {
		// Set the i'th led to red 
		leds[i] = CHSV(hue++, 255, 255);
		// Show the leds
		FastLED.show(); 
		// now that we've shown the leds, reset the i'th led to black
		// leds[i] = CRGB::Black;
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }

void loop() { 
	static uint8_t hue = 0;
	Serial.print("x");
	// First slide the led in one direction
	for(int i = 0; i < NUM_LEDS; i++) {
		// Set the i'th led to red 
		leds[i] = CHSV(hue++, 255, 255);
		// Show the leds
		FastLED.show(); 
		// now that we've shown the leds, reset the i'th led to black
		// leds[i] = CRGB::Black;
		fadeall();
		// Wait a little bit before we loop around and do it again
		delay(10);
	}
	Serial.print("x");

	// Now go in the other direction.  
	for(int i = (NUM_LEDS)-1; i >= 0; i--) {
		// Set the i'th led to red 
		leds[i] = CHSV(hue++, 255, 255);
		// Show the leds
		FastLED.show();
		// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
		fadeall();
		// Wait a little bit before we loop around and do it again
		delay(10);
	}
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 2, 2017, 10:09pm UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439/4 "2017-08-02T22:09:05Z")

</div>

Please follow proper posting rules.

Use CTRL T to format the sketch.  
Please use code tags. Use the \</\> icon in the posting menu.  
`[code] Paste sketch here. [/code]`

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 2, 2017, 10:34pm UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439/5 "2017-08-02T22:34:30Z")

</div>

Are you using the WS2812b strip?

You can only have one loop() function in a sketch.

Suggest you keep { } on lines by themselves.

You cannot have two functions with the same name.

Suggest you study the basic sketches that come with the IDE to see how things are done.

.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 2, 2017, 11:01pm UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439/6 "2017-08-02T23:01:55Z")

</div>

If you have WS2812b try this:

```cpp
#include <FastLED.h>

#define LED_PIN    6
#define NUM_LEDS    15      // <------------<<<<< change this to the number of LEDs
#define BRIGHTNESS  64
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100

// This example shows several ways to set up and use 'palettes' of colors
// with FastLED.
//
// These compact palettes provide an easy way to re-colorize your
// animation on the fly, quickly, easily, and with low overhead.
//
// USING palettes is MUCH simpler in practice than in theory, so first just
// run this sketch, and watch the pretty lights as you then read through
// the code.  Although this sketch has eight (or more) different color schemes,
// the entire sketch compiles down to about 6.5K on AVR.
//
// FastLED provides a few pre-configured color palettes, and makes it
// extremely easy to make up your own color schemes with palettes.
//
// Some notes on the more abstract 'theory and practice' of
// FastLED compact palettes are at the bottom of this file.

CRGBPalette16 currentPalette;
TBlendType    currentBlending;

extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;

void setup() {
    delay( 3000 ); // power-up safety delay
    FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness(  BRIGHTNESS );
    
    currentPalette = RainbowColors_p;
    currentBlending = LINEARBLEND;
}

void loop()
{
    ChangePalettePeriodically();
    
    static uint8_t startIndex = 0;
    startIndex = startIndex + 1; /* motion speed */
    
    FillLEDsFromPaletteColors( startIndex);
    
    FastLED.show();
    FastLED.delay(1000 / UPDATES_PER_SECOND);
}

void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
    uint8_t brightness = 255;
    
    for( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
        colorIndex += 3;
    }
}

// There are several different palettes of colors demonstrated here.
//
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
//
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly.  All are shown here.

void ChangePalettePeriodically()
{
    uint8_t secondHand = (millis() / 1000) % 60;
    static uint8_t lastSecond = 99;
    
    if( lastSecond != secondHand) {
        lastSecond = secondHand;
        if( secondHand ==  0)  { currentPalette = RainbowColors_p;        currentBlending = LINEARBLEND; }
        if( secondHand == 10)  { currentPalette = RainbowStripeColors_p;  currentBlending = NOBLEND;  }
        if( secondHand == 15)  { currentPalette = RainbowStripeColors_p;  currentBlending = LINEARBLEND; }
        if( secondHand == 20)  { SetupPurpleAndGreenPalette();            currentBlending = LINEARBLEND; }
        if( secondHand == 25)  { SetupTotallyRandomPalette();              currentBlending = LINEARBLEND; }
        if( secondHand == 30)  { SetupBlackAndWhiteStripedPalette();      currentBlending = NOBLEND; }
        if( secondHand == 35)  { SetupBlackAndWhiteStripedPalette();      currentBlending = LINEARBLEND; }
        if( secondHand == 40)  { currentPalette = CloudColors_p;          currentBlending = LINEARBLEND; }
        if( secondHand == 45)  { currentPalette = PartyColors_p;          currentBlending = LINEARBLEND; }
        if( secondHand == 50)  { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND;  }
        if( secondHand == 55)  { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
    }
}

// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
{
    for( int i = 0; i < 16; i++) {
        currentPalette[i] = CHSV( random8(), 255, random8());
    }
}

// This function sets up a palette of black and white stripes,
// using code.  Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
    // 'black out' all 16 palette entries...
    fill_solid( currentPalette, 16, CRGB::Black);
    // and set every fourth one to white.
    currentPalette[0] = CRGB::White;
    currentPalette[4] = CRGB::White;
    currentPalette[8] = CRGB::White;
    currentPalette[12] = CRGB::White;
    
}

// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette()
{
    CRGB purple = CHSV( HUE_PURPLE, 255, 255);
    CRGB green  = CHSV( HUE_GREEN, 255, 255);
    CRGB black  = CRGB::Black;
    
    currentPalette = CRGBPalette16(
                                  green,  green,  black,  black,
                                  purple, purple, black,  black,
                                  green,  green,  black,  black,
                                  purple, purple, black,  black );
}

// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM.  A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
    CRGB::Red,
    CRGB::Gray, // 'white' is too bright compared to red and blue
    CRGB::Blue,
    CRGB::Black,
    
    CRGB::Red,
    CRGB::Gray,
    CRGB::Blue,
    CRGB::Black,
    
    CRGB::Red,
    CRGB::Red,
    CRGB::Gray,
    CRGB::Gray,
    CRGB::Blue,
    CRGB::Blue,
    CRGB::Black,
    CRGB::Black
};

// Additionl notes on FastLED compact palettes:
//
// Normally, in computer graphics, the palette (or "color lookup table")
// has 256 entries, each containing a specific 24-bit RGB color.  You can then
// index into the color palette using a simple 8-bit (one byte) value.
// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
// is quite possibly "too many" bytes.
//
// FastLED does offer traditional 256-element palettes, for setups that
// can afford the 768-byte cost in RAM.
//
// However, FastLED also offers a compact alternative.  FastLED offers
// palettes that store 16 distinct entries, but can be accessed AS IF
// they actually have 256 entries; this is accomplished by interpolating
// between the 16 explicit entries to create fifteen intermediate palette
// entries between each pair.
//
// So for example, if you set the first two explicit entries of a compact 
// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved 
// the first sixteen entries from the virtual palette (of 256), you'd get
// Green, followed by a smooth gradient from green-to-blue, and then Blue.

```

---

<div class="post-metadata">

**Author:** ![tobi3492](https://avatars.discourse-cdn.com/v4/letter/t/e99b99/32.png) [@tobi3492](https://forum.arduino.cc/u/tobi3492)\
**Post date:** [August 3, 2017, 8:21am UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439/7 "2017-08-03T08:21:55Z")

</div>

hey larryd

thanks for the help the code works and i have to swap out the led strip, i think it is broke.  
but now the LEDs only light up to arround the 50th led on the strip ant the rest are still off

i hope you can help

i use the code you provided but changed the number of LEDs to 300

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [August 3, 2017, 2:12pm UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439/8 "2017-08-03T14:12:13Z")

</div>

Review this information:

> **[Adafruit NeoPixel Überguide](https://learn.adafruit.com/adafruit-neopixel-uberguide?view=all)**
>
> NeoPixels are “intelligent” full-color RGB LEDs that can be controlled and chained from a single microcontroller pin. This guide presents an overview of NeoPixel products, along with tips for building and powering projects of all shapes and sizes.

Make sure you have the strip wired properly:  
 ![](https://europe1.discourse-cdn.com/arduino/original/4X/2/c/4/2c4826c3fec046b701bf125f133b84cb93ecd71e.png)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 3:05am UTC](https://forum.arduino.cc/t/controlling-ws2812-with-arduino-mega-2560/473439/9 "2021-05-06T03:05:23Z")

</div>

Closed on May 6, 2021, 3:05am UTC


