NeoPixel LED strip randomly flash green unexpectedly

Hello, I'm new to Arduino and this forum. I'd highly appreciate it if anyone can help.

I'm crafting a circuit where a nano ESP32 controls the display of an LED strip (NeoPixel, three lines with only one data line, using WS2812B protocol). The light color is set to white with 1% power. When I connected ESP32's output pin to LED data line, I get the moving LED pattern as expected. However, it sometimes flash green with 100% power. I can't tell what's wrong in my code. I also checked my connection to make sure nothing is obviously loose (ESP32 is connected to the ground, LED data line is connected to the output pin through a bread board).

My guess: the WS2812B is prone to perturbation. Since it seems like the longer the data line I have, the more flashes I will get. And I'm working with many exposed lines in a hot & humid environment.

What can be the reason of this wired phenomena? Is there any suggestions about how to fix this problem? Any insights would be appreciated!

Code attached:

#include <Adafruit_NeoPixel.h>

#define LEDPIN 4          // connected to NeoPixel LED data line
#define BUTTON_LEFT D2   // input 1; I'm using two high/low voltage inputs to guide the behavior of LED strip. The detail is not relevant with my issue, I think.
#define BUTTON_RIGHT A0  // input 2
#define PATTERN_SPACING 5  // 1 light + 4 dark = 5; specify LED pattern

int patternOffset = 0;
int turn_gain = 1; // How many turns every 0.1s
const unsigned long loopInterval = 100; // 0.1s interval
unsigned long refreshInterval = loopInterval/turn_gain;
int NUM_LEDS = 80;
int BRIGHTNESS = 1; // only 1% brightness to keep surroundings dark

// create LED subject
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LEDPIN, NEO_GRBW + NEO_KHZ800);

void setup() {
  Serial.begin(115200);
  // Initialize input pins
  pinMode(BUTTON_LEFT, INPUT);
  pinMode(BUTTON_RIGHT, INPUT);
  
  // Initialize LED strip
  strip.begin();    
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, 0, 0, 0,0);  // Turn off LED
  }
  // Draw the pattern (1 light, 4 dark repeating)
  for (int i = patternOffset; i < NUM_LEDS; i += PATTERN_SPACING) {
    strip.setPixelColor(i,0,0,0,BRIGHTNESS);  // White at 10% brightness
  }
  strip.show();
  Serial.println("Your log message");
}

void loop() {
  
  unsigned long curMillis = millis();
  Serial.println("one loop"+String(curMillis));
  if (digitalRead(BUTTON_RIGHT) == HIGH) {
    movePatternRight();
    unsigned long timeLeft = refreshInterval - millis() + curMillis;
    if (timeLeft > 0) {
      delay(timeLeft);
    }
  } else if (digitalRead(BUTTON_LEFT) == HIGH) {
    movePatternLeft();
    unsigned long timeLeft = refreshInterval - millis() + curMillis;
    if (timeLeft > 0) {
      delay(timeLeft);
    }
  } else {
    unsigned long timeLeft = refreshInterval - millis() + curMillis;
    if (timeLeft > 0) {
      delay(timeLeft);
    }
  }

}


void movePatternLeft() {
  patternOffset = (patternOffset + 1 + PATTERN_SPACING) % PATTERN_SPACING;
  updatePattern();
  Serial.println("left");
}

void movePatternRight() {
  patternOffset = (patternOffset - 1) % PATTERN_SPACING;
  updatePattern();
  Serial.println("right");
}

void updatePattern() {
  // Clear all LEDs
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, 0, 0, 0, 0);  // Turn off LED
  }
  // Draw the pattern (1 light, 4 dark repeating)
  for (int i = patternOffset; i < NUM_LEDS; i += PATTERN_SPACING) {
    strip.setPixelColor(i, 0,0,0, BRIGHTNESS);  // White at 1% brightness
  }
  
  strip.show();
}

Neopixels require 5volt-logic signals, which a Nano ESP32 doesn't provide.
How did you solve that.
See this.
Leo..

1 Like

At least you manage millis incorrectly :

First - using an addition to calculate a time interval doesn't treat a millis overflow properly.
The second - and I think more important in the case - your timeLeft calculation produces unexpected (very big) value after refreshinterval period finished because the unsigned result type of the expression. Equally, comparison of the unsigned value to zero has a little sense at all.

This bug could be fixed by changing the expression something like:

1 Like

Thank you! This can be the problem. I just bought a voltage converter to see if it works

Thank you so much! Just implemented the revised code