I'm using FastLEDs builtin fill_rainbow function for generating a rainbow spectrum on an LED strip. The issue is, I get a random red pixel midway through the yellow region. I've tried it on 3 different Arduino nano's, and two different LED-strips. Here's the code:

#include <FastLED.h>

#define DATA_PIN 2
#define NUM_LEDS 114

uint8_t hue = 0;

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

  fill_rainbow( leds, NUM_LEDS, hue, 1);
  for(int i=0; i<NUM_LEDS; i++) {
    Serial.print(' ');
    Serial.print(' ');
    Serial.print(' ');

  CHSV hsv;
  CRGB rgb;
  //rgb = hsv;

void loop() {
  fill_rainbow( leds, NUM_LEDS, hue, 1);

Here pixel number 60 gets the rgb code 160, 0, 0. When it should have something like 160, 150, 0.

The strangest part of all is that I can get rid of the problem in two ways, either by uncommenting the line rgb = hsv;, or by commenting out the fill_rainbow function in the loop function. This strange behavior leads me to believe im somehow messing up a pointer or misusing a FastLED feature. Additionally the fill_rainbow function is very simple and relies on FastLEDs hsv to rgb typecasting, which is another reason to believe I'm doing something wrong.

So help me out here, am i doing something silly, or have I discovered a bug with FastLEDs hsv to rgb converter?

  • Issues with a specific third-party library are best raised with the author of the library. Raise an issue here.
    – Majenko
    Oct 9, 2018 at 21:59
  • @Majenko Sure, but the reason for posting the question is i suspect i made a mistake, as explained in the latter part of the question. I'm hoping to get some feedback on weather I have made a mistake before posting an issue at the developer.
    – Tehanu
    Oct 10, 2018 at 6:45

2 Answers 2


I just copied your exact code and ran it with 600 of my LEDs. I have WS2812B as well, and using the same data pin coincidentally.

I haven't experienced your red LED issue. I then ran it again on my LEDs after adding


at line 33.

Only problem I can see is that the last 30 LEDs at the end of the rainbow kind of flicker. Groups of 10 LEDs spontaneously flicker at once, then another 10, and so on. This could be a problem with my LEDs though because they were the cheapest one's I could find on ebay 2 years ago. The code seems to work fine for me though!


It seems that this is caused by a compiler bug when compiling with the default -Os (optimize for size) option.

see Issue : fill_rainbow() - Unexpected red pixel #668

A workaround is to change the optimization level to -O3 for fill_rainbow, ie :

#pragma GCC push_options       // Disable the size optimization
#pragma GCC optimize ("-O3")   // to prevent the “red pixel” problem
void fill_rainbow( struct CRGB * pFirstLED, int numToFill,
                  uint8_t initialhue,
                  uint8_t deltahue )
    CHSV hsv;
    hsv.hue = initialhue;
    hsv.val = 255;
    hsv.sat = 240;
    for( int i = 0; i < numToFill; i++) {
        pFirstLED[i] = hsv;
        hsv.hue += deltahue;
#pragma GCC pop_options // Enable the prior optimization

You would have to edit colorutils.cpp (the file where fill_rainbow lives above) in the FastLED source in your \libraries directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.