Skip to content

First led of WS2812B is glitching with NodeMCU #1260

Open

Description

@pla10

Hello! I decided to work again in a old project with the WS2812B and noticed that after updating the libraries, the first led started blinking randomly. I could isolate the problem. It happens only when the esp8266 connects to the wifi and the FastLED.show() is called, otherwise no glitch happens. Do you have any suggestions?

IMG_0299.MOV

Activity

  1. pla10 commented on Jul 20, 2021

    @pla10
    Author
    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
    
    #define FASTLED_ALLOW_INTERRUPTS 0
    #include <FastLED.h>
    
    #define LED_PIN     D5
    #define NUM_LEDS    279
    #define LED_TYPE    WS2812B
    #define COLOR_ORDER GRB
    CRGB leds[NUM_LEDS];
    
    int BRIGHTNESS = 64;
    int preBrightness = 64;
    int r = 255, g = 255, b = 255;
    bool change = false;
    int i = 0, dir = 1;
    
    #define UPDATES_PER_SECOND 100
    
    bool cylonFlag;
    CRGBPalette16 currentPalette;
    TBlendType    currentBlending;
    
    // VARIABILI DA MODIFICARE CON LE INFORMAZIONI DEL DISPOSITIVO CONTROLLATO -----------------------------------------------
    #define IDX 11                                                       // Codice idx del dispositivo sul server domoticz
    #define TOPIC_NAME "Scale/Led"                                       // Nome del topic relativo al dispositivo controllato
    //------------------------------------------------------------------------------------------------------------------------
    
    
    const char* ssid = "**************";                                     // Dati connessione WIFI
    const char* password = "***************";
    const char* mqtt_server = "***.***.*.*";
    
    WiFiClient espClient;
    PubSubClient client(espClient);
    
    char hellomsg[40];                                                  // Formattazione del messaggio di handshake
    
    
    void setup() {
      delay( 3000 ); // power-up safety delay
      FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
      FastLED.setBrightness(  BRIGHTNESS );
      
      //Serial.begin(9600);                                           // Configurazione Seriale
      pinMode(LED_BUILTIN, OUTPUT);                                     // Imposto LED per stato WiFi
      
      sprintf(hellomsg,"{\"content\":\"%s connesso\"}",TOPIC_NAME);
      
      setup_wifi();                                                     // Connessione alla rete WIFI
      client.setServer(mqtt_server, 1883);                              // Connessione al server MQTT
      client.setCallback(callback);                                     // Configurazione dell'ascolto del topic in MQTT
    }
    
    void loop() {
      if (!client.connected()) {                                        // Gestione connessione WIFI
        reconnect();
      }
      client.loop();
    
      if(change)
        ChangePalettePeriodically();
    
      if(cylonFlag) cylon();
      else{ 
        static uint8_t startIndex = 0;
        startIndex = startIndex + 1; // motion speed
        
        FillLEDsFromPaletteColors( startIndex);
        
        FastLED.show();
        FastLED.delay(1000 / UPDATES_PER_SECOND);
      }
      
    
    }
    
    void cylon() {
      static uint8_t hue = 0;
      uint8_t brightness = BRIGHTNESS;
      // First slide the led in one direction
      if(dir == 1) {
        if(i <= NUM_LEDS){
            // Set the i'th led to red 
            leds[i] = CHSV(hue++, 255, brightness);
            // Show the leds
            FastLED.show(); 
            // now that we've shown the leds, reset the i'th led to black
            // leds[i] = CRGB::Black;
            for(int i = 0; i < NUM_LEDS; i++) { leds[i].fadeToBlackBy(6); }
            // Wait a little bit before we loop around and do it again
            delay(3);
            i++;
        }else{
            dir = -1;
        }
      }
    
      // Now go in the other direction.  
      if(dir == -1) {
        if(i > 0) {
            // Set the i'th led to red 
            leds[i] = CHSV(hue++, 255, brightness);
            // Show the leds
            FastLED.show();
            // now that we've shown the leds, reset the i'th led to black
            // leds[i] = CRGB::Black;
            for(int i = 0; i < NUM_LEDS; i++) { leds[i].fadeToBlackBy(6); }
            // Wait a little bit before we loop around and do it again
            delay(3);
            i--;
        }else{
            dir = 1;
        }
      }
    }
    
    void FillLEDsFromPaletteColors( uint8_t colorIndex)
    {
        uint8_t brightness = BRIGHTNESS;
        
        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()
    {
            cylonFlag = false;
            if(r == 0 && g == 0 && b == 255)      { currentPalette = OceanColors_p;         currentBlending = LINEARBLEND; }//Serial.println("Ocean");}
            if(r == 255 && g == 255 && b == 255)  { SetupAllWhitePalette();                 currentBlending = LINEARBLEND; }//Serial.println("Cloud");}
            if(r == 255 && g == 0 && b == 0)      { currentPalette = LavaColors_p;          currentBlending = LINEARBLEND; }//Serial.println("Lava");}
            if(r == 255 && g == 0 && b == 169)    { currentPalette = RainbowColors_p;       currentBlending = LINEARBLEND; }//Serial.println("Rainbow");}
            if(r == 181 && g == 10 && b == 255)   { cylonFlag = true;                                                      }//Serial.println("Cylon");}
            if(r == 8 && g == 255 && b == 0)      { SetupBlackAndWhiteStripedPalette();     currentBlending = LINEARBLEND; }//Serial.println("Stripes");}
            change = false;
    }
    
    // This function fills the palette with white.
    void SetupAllWhitePalette()
    {
        // all 16 palette entries white...
        fill_solid( currentPalette, 16, CRGB::White);
        
    }
    
    // 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;
        
    }
    
    void callback(char* topic, byte* payload, unsigned int length) {    // Ricezione messaggio dal server MQTT
     
      if((char)payload[0] == '7'){                                      // Imposto il flag per la fase di upload OTA
        //Serial.println("ATTIVO OTA");
        ota_flag = true;
        timerOTA = millis();
      } else 
      if((char)payload[0] == '8'){                                    // Imposto il flag per la fase di upload OTA
        ESP.reset();
      }
    
      int lengthBr = 0,lengthR = 0,lengthG = 0,lengthB = 0;
      
      int auxBrightness = 0;
      int var = 0;
    
      int rn = 0, gn = 0, bn = 0;
    
      int conta = 0;
    
      for(int i=0;i<length;i++){
        if((char)payload[i] == ',') {
          if(var == 0)
            lengthBr = conta;
          if(var == 1)
            lengthR = conta;
          if(var == 2){
            lengthG = conta;
            lengthB = length - i - 1;
          }
          var++;
          conta = -1;
        }
        conta++;
      }
    
      var = 0;
    
      for(int i=0;i<length;i++){
        if((char)payload[i] == ',') {
          var++;
        } else {
          if(var == 0)
            auxBrightness += ((char)payload[i] - '0') * pow(10,lengthBr-(i+1));
          if(var == 1)
            rn += ((char)payload[i] - '0') * pow(10,lengthR-(i-lengthBr));
          if(var == 2)
            gn += ((char)payload[i] - '0') * pow(10,lengthG-(i-lengthBr-lengthR-1));
          if(var == 3)
            bn += ((char)payload[i] - '0') * pow(10,lengthB-(i-lengthBr-lengthR-lengthG-2));
        }
      }
      
      if (rn != r || gn != g || bn != b) change = true;
      r=rn;g=gn;b=bn;
      
      auxBrightness = auxBrightness * 255 / 100;
      if(auxBrightness == 0 && BRIGHTNESS != 0)
        preBrightness = BRIGHTNESS;
      BRIGHTNESS = auxBrightness;
      if(BRIGHTNESS == -72)
        BRIGHTNESS = preBrightness; 
      
      /*for (int i = 0; i < length; i++) {
        //Serial.print((char)payload[i]);
      }*/
      //Serial.println();
    }
    
    void reconnect() {                                                  // Riconnessione in caso di perdita del segnale WIFI
      while (!client.connected()) {                                     // Loop until we're reconnected
        digitalWrite(LED_BUILTIN, LOW);                                 // Accensione led per segnalare inizio connessione
        //Serial.println("Attempting MQTT connection...");
        if (client.connect(TOPIC_NAME)) {                                // Attempt to connect
          //Serial.println("connected");
          delay(200);
          wdt_reset();
          client.subscribe(TOPIC_NAME);                                  // Iscrizione al Topic dedicato alla lampada gestita
          client.publish("Hello", hellomsg);                            // Invio messaggio di avvenuta connessione al server MQTT
          digitalWrite(LED_BUILTIN, HIGH);                              // Spegnimento led per segnalare l'avvenuta connessione
          delay(300);
          wdt_reset();
        } else {
          //Serial.print("failed, rc=");
          //Serial.print(client.state());
          //Serial.println(" try again in 5 seconds");
          for(int i=0;i<100;i++) {                                      // Wait 5 seconds before retrying
            delay(50);
          }
        }
      }
    }
    
    void setup_wifi() {                                                 // Collegamento alla rete WIFI
      delay(10);
      //Serial.println();
      //Serial.print("Connecting to ");
      //Serial.println(ssid);
      digitalWrite(LED_BUILTIN, LOW);                                   // Accensione led per segnalare inizio connessione
      
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);                                       // We start by connecting to a WiFi network
      
      while (WiFi.status() != WL_CONNECTED) {
        for(int i=0;i<10;i++) {
          delay(50);
        }
        //Serial.print(".");
      }
      //Serial.println();
      //Serial.println("WiFi connected");
      //Serial.println("IP address: ");
      //Serial.println(WiFi.localIP());
      digitalWrite(LED_BUILTIN, HIGH);                                    // Spegnimento led per segnalare l'avvenuta connessione
    }
    
  2. heumann-a commented on Jul 22, 2021

    @heumann-a

    Got the same Problem with nearly identical setup.
    I am calling FastLED.show() every minute to update matrix LEDS, i divided 11 Strips with 11 LEDs each on 2 Pins ( one 55, other 66 LEDS). On both Strips the first LED after Data-In lights up in random color (blue and pink)
    forcing it to black doesnt work.

    EDIT: downgrade to [email protected] fixes that problem for me, everything works as intended

  3. kriegsman commented on Jul 29, 2021

    @kriegsman
    Member

    Thanks for reporting this; all of the reports from ESP8266 are helpful to try and track this down and fix itt.

  4. samguyer commented on Jul 29, 2021

    @samguyer
    Contributor

    @pla10 and @heumann-a: our working theory right now is that changes to the 8266 core (probably the WiFi part) are causing new/more interrupts that disrupt the sensitive timing of the sending data to the pixels. What you're seeing is partial data sent to the pixel -- when we detect the timing problem, we abort and try again. Eventually, it works, but in the meantime the first pixel shows a lot of weird data. One thing we might try to add is a counter that you can access to determine if frames are getting dropped. But the interrupt problem is hard to fix.

  5. self-assigned this
    on Jul 29, 2021
  6. pla10 commented on Jul 29, 2021

    @pla10
    Author

    Thank you for the reply! Is there any known quick fix to this in the meantime?

  7. SergeSkor commented on Jul 30, 2021

    @SergeSkor

    I have the same problem on multiple projects. Before esp8266 core v3.0.0 never see such problem.
    Are there any working fix?

  8. samguyer commented on Jul 30, 2021

    @samguyer
    Contributor

    @SergeSkor I think the only quick fix is to revert to a pre-3.0.0 version of the core.

  9. SergeSkor commented on Jul 31, 2021

    @SergeSkor

    @SergeSkor I think the only quick fix is to revert to a pre-3.0.0 version of the core.

    Yep, seems like...

  10. pla10 commented on Aug 3, 2021

    @pla10
    Author

    Reverting back to version 2.7.4 of esp8266 core solved the problem for me

  11. ArtursGailis1995 commented on Aug 9, 2021

    @ArtursGailis1995

    Same issue with 3.0.1 or 3.0.2 core and latest FastLED. Tried latest dev FastLED and it went into ful lred color and bootloop with stack trace.

    Downgrading to 2.7.4 core and 3.4.0 FastLED fixed it for me too.

  12. Anatoliy464 commented on Aug 13, 2021

    @Anatoliy464

    Try to add
    #define FASTLED_INTERRUPT_RETRY_COUNT 0
    Before
    #include <FastLED.h>

    It fixed problem for me

  13. SergeSkor commented on Aug 13, 2021

    @SergeSkor

    #define FASTLED_INTERRUPT_RETRY_COUNT 0
    does not work for me, still blinking on 3.0.X

  14. phil31 commented on Oct 3, 2021

    @phil31

    Hello,

    on my custom ESP32 board, same issue ! first led is glitching.
    using D4 or D5 pin on ESP32-WROOM-32UE 4MB and 9 x WS2812B.

    example "FirstLight" show it ! even without any wifi connection or webserver running.

    Arduino 1.8.13, FastLED 3.4.0
    same issue using platformIO

    Any advice please ?
    regards

  15. SergeSkor commented on Oct 4, 2021

    @SergeSkor
  16. 8 remaining items

  17. g3gant commented on Dec 3, 2021

    @g3gant

    Got the same issue, using ESP8266 core 3.0.2, FastLED 3.4.0
    #define FASTLED_INTERRUPT_RETRY_COUNT 0
    #define FASTLED_ALLOW_INTERRUPTS 0
    and WiFi.setSleepMode(WIFI_NONE_SLEEP) fixes the problem until trying to connect to WiFi with static IP, if I'm using WiFi.Config(IP,GWY,MASK). First LED start blinking randomly.
    Tried to downgrade core to 2.7.4, but got an error with FastLED:
    "src/fastpin.h:210:2: error: static assertion failed: Invalid pin specified static_assert(validpin(), "Invalid pin specified");

  18. SergeSkor commented on Dec 3, 2021

    @SergeSkor
  19. g3gant commented on Dec 3, 2021

    @g3gant

    g3gant, not clear what pin it is complaining about. I have no problems at all switching between 2.7.4 and 3.. without changing in the code.
    …
    On Fri, Dec 3, 2021 at 1:16 AM g3gant @.***> wrote: Got the same issue, using ESP8266 core 3.0.2, FastLED 3.4.0 #define FASTLED_INTERRUPT_RETRY_COUNT 0 #define FASTLED_ALLOW_INTERRUPTS 0 and WiFi.setSleepMode(WIFI_NONE_SLEEP) fixes the problem until trying to connect to WiFi with static IP, if I'm using WiFi.Config(IP,GWY,MASK). First LED start blinking randomly. Tried to downgrade core to 2.7.4, but got an error with FastLED: "src/fastpin.h:210:2: error: static assertion failed: Invalid pin specified static_assert(validpin(), "Invalid pin specified"); — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#1260 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOYEDTSAKVTKTI35HPSNBVTUPBODNANCNFSM5AWFHI3Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

    Because ESP8266 core below 3.x.x does not support pin mapping like D1, D2 and so on. I had to change them to the numbers. And I had FastLED pin defined as D8, after downgrade I changed it to 15 according to ESP pinout.

  20. SergeSkor commented on Dec 3, 2021

    @SergeSkor
  21. g3gant commented on Dec 3, 2021

    @g3gant

    g3gant, I am also using FastLED pin D8, but I believe it is defined in the FastLED library, not in the board core. P.s. But you mast to have define FASTLED_ESP8266_RAW_PIN_ORDER before FastLed include... --- 8< --- #define FASTLED_ESP8266_RAW_PIN_ORDER #include <FastLED.h> FASTLED_USING_NAMESPACE .... FastLED.addLeds<CHIPSET, D8, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050); --- 8< ---

    My bad, I had to mention that I had to change pins for other libraries I'm using. And I was missing this line "define FASTLED_ESP8266_RAW_PIN_ORDER", that solved it. Thanks a lot!
    I hope the fix for 3.x.x core will be found soon :-)

  22. perkinsinnovation commented on Dec 17, 2021

    @perkinsinnovation

    I'm simply trying to flash my first led independently of the others, and it changes random colors. The other LED's seem to be fine, however, flashing LED's 2 and 3, sometimes change the color of 1.

    I'm having the same problem with 3.2.0. I also tried 3.0.0 with no luck.

    I wasn't able to go back to 2.6.3 since my build fails, and I really don't want to troubleshoot backward compatible issues.

    I tried the suggestions you guys posted, but with no luck. Are there any other options? I'm just looking to flash three LED's reliably without color change.

    Any help or suggestions would be greatly appreciated.

    Thanks,
    Shawn

  23. Johboh commented on Dec 18, 2021

    @Johboh

    Same here. Going back to 2.7.4 eliminates the flicker on led 0.

  24. dKatsuro commented on Dec 26, 2021

    @dKatsuro

    Was setting up this myself and I was using a ESP8266 12-E NodeMCU board.

    It did work when using esp8266 2.7.4 (but not 3.0.2) with FastLed 3.4.0 and defined FASTLED_ESP8266_NODEMCU_PIN_ORDER with data pin set to 4 since I'm using D4. Of course, It also worked using RAW_PIN_ORDER with data pin set as 2.

  25. added
    type: bug
    platform: esp8266
    area: drivers
    status: stale
    status: triage
    on Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: driversplatform: esp8266status: stalestatus: triagetype: bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions