LED strip blinking

Hey guys.

After setting up my led strip with a simple sketch, everything is fine, but as soon as I upload a more complex sketch with networking and MQTT, this weird blinking is starting to show. (Please see video at: https://www.reddit.com/r/arduino/comments/cgj8uo/led_strip_blinking/ )

I have a WS2811 strip connected to a NodeMCU(Amica).

I am using a notebook charger as my power supply for the strip and the nodemcu. Everything is taken from BruhAutomation's guide (GitHub - bruhautomation/ESP-MQTT-JSON-Digital-LEDs: (OBSOLETE) ESP8266 MQTT JSON Digital LEDs for Home Assistant), same wiring and all.

Mosquitto server running on Raspberry pi 3.

First simple sketch, with this one everything works as expected.

#include <FastLED.h>

#define LED_PIN 5
#define NUM_LEDS 100
#define LED_TYPE WS2811
#define COLOR_ORDER BRG
CRGB leds[NUM_LEDS];

bool stateOn = false;

void setup() {
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}

void loop() {
  if(stateOn)
  {
    leds[0] = CRGB::Red;
    FastLED.show();
    delay(10000);
    stateOn = false;
  }
  else
  {
    leds[0] = CRGB::Black;
    FastLED.show();
    delay(1000);
    stateOn=true;
  }
}

Second one is a more complex one and the blinking start showing.

#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

/*** FastLED ***/
#define LED_PIN     5
#define NUM_LEDS    100
#define BRIGHTNESS  64
#define LED_TYPE    WS2811
#define COLOR_ORDER BRG
CRGB leds[NUM_LEDS];

byte red = 255;
byte green = 255;
byte blue = 255;
byte brightness = 255;

/*** WIFI ***/
const char* ssid = "";
const char* password = "";

/*** MQTT ***/
const char* mqtt_server = "";
const char* mqtt_username = "";
const char* mqtt_password = "";
const int mqtt_port = 1883;

const char* light_state_topic = "esp/state";
const char* light_set_topic = "esp/set";

const char* on_cmd = "ON";
const char* off_cmd = "OFF";
const char* effect = "solid";

/*** JSON ***/
const int BUFFER_SIZE = JSON_OBJECT_SIZE(10);
#define MQTT_MAX_PACKET_SIZE 512

/*** Globals ***/
bool stateOn = false;
String effectString = "solid";

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi()
{
  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Wifi connected.");
  Serial.println();
}

void reconnect()
{
  Serial.println("Connecting to MQTT..");
  while(!client.connected())
  {
    Serial.print("Attempting MQTT connection..");
    if(client.connect("ESP", mqtt_username, mqtt_password))
    {
      Serial.println("Connected.");
      
      client.subscribe("esp/set");
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void setup()
{
  Serial.begin(9600);
  
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS); 
  
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);

  Serial.println("Ready.");
  Serial.println("IP Address: ");
  Serial.print(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.println("Message arrived [");
  Serial.print(topic);
  Serial.print("]");

  char message[length + 1];
  for (int i = 0; i<length; i++)
  {
    message[i] = (char)payload[i];
  }
  message[length] = '\0';
  Serial.println(message);

  if(!processJson(message))
  {
    return;
  }
  
  if(stateOn)
  {
    Serial.println("STATE IS ON");
  }
  else
  {
    Serial.println("STATE IS OFF");
  }

  sendState();
}

bool processJson(char* message)
{
  StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;

  JsonObject& root = jsonBuffer.parseObject(message);

  if(!root.success())
  {
    Serial.println("parseObject() failed");
    return false;
  }
  else
  {
    Serial.println("Successfully parsed");
  }

  if(root.containsKey("state"))
  {
    if(strcmp(root["state"], on_cmd) == 0)
    {
      stateOn = true;
    }
    else if(strcmp(root["state"], off_cmd) == 0)
    {
      stateOn = false;
    }
  }

  if(root.containsKey("effect"))
  {
    effect = root["effect"];
    effectString = effect;
  }

  if(root.containsKey("color"))
  {
    red = root["color"]["r"];
    green = root["color"]["g"];
    blue = root["color"]["b"];
  }

  if(root.containsKey("brightness"))
  {
    brightness = root["brightness"];
  }

  return true;
}

void sendState()
{
  StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;

  JsonObject& root = jsonBuffer.createObject();

  root["state"] = (stateOn) ? on_cmd : off_cmd;
  JsonObject& color = root.createNestedObject("color");
  color["r"] = red;
  color["g"] = green;
  color["b"] = blue;

  root["brightness"] = brightness;
  root["effect"] = effectString.c_str();

  char buffer[root.measureLength() + 1];
  root.printTo(buffer, sizeof(buffer));

  //client.publish(light_set_topic, buffer, true);
}

void loop()
{/*
  for(int dot = 0; dot < NUM_LEDS; dot++){
    leds[dot] = CRGB::Blue;
    FastLED.show();
    leds[dot] = CRGB::Black;
    delay(30);
  }*/

  if(!client.connected())
  {
    reconnect();
  }

  if(WiFi.status() != WL_CONNECTED)
  {
    delay(1);
    Serial.print("WIFI disconnected. Attempting reconnection.");
    setup_wifi();
    return;
  }

  client.loop();

  if(!stateOn)
  {
    leds[0] = CRGB::Black;
    FastLED.show();
  }
  else
  {
    leds[0] = CRGB::Red;
    FastLED.show();
  }
}

After setting the state to on, the led lights up as expected, but it also starts to blink.

I also tried a third sketch, from from BruhAutomation's git (Linked above), thought I was doing something wrong, so I took his code, but the blinking still remains.

I tried the same things with 2 different setups, different nodeMCUs, different led strips, so I don't think it is hardware related. Could you guys please point out what's wrong? I'm at a loss.

The first sketch only turns on one LED, the second turns on many.

So what is the way you are powering the LED strip?

So the powering is as it follows:

I have a laptop charger that goes into the led strip and into a converter. From the converter I am powering the NodeMCU and logic level converter's 5v side. From the NodeMCU I get 3v and the signal to the logic level converter's 3v side and lastly pass the 5v signal into the strip.

As for the sketches, the second one should be doing the same thing, only the networking and MQTT is implemented as a plus, if i'm not mistaken.

The charger is rated at 72W. Converter is rated 15W. Led strip is rated 90W. Can you see a problem?

A 15-20 Amp 5V PSU would be a better choice.

Also, those logic level shifters are not a good choice for ws2812b led strips and will not give out a clean signal at the data speed used with these leds. A 74hc14 chip would be much better.

Grumpy_Mike:
The second turns on many.

Mike, the part of the code lighting every led is commented out. Both codes light only the first led.

(Please see video at: Reddit - Dive into anything )

Both codes light only the first led. It is difficult to see what is happening in your dark video, but I see maybe 3 leds blinking and 3 leds on steady. But they don't appear to be the first led.

PaulRB:
Mike, the part of the code lighting every led is commented out. Both codes light only the first led.

Yeah, my bad, it's my bad habbit to leave commented code in, for eternity.

PaulRB:
The charger is rated at 72W. Converter is rated 15W. Led strip is rated 90W. Can you see a problem?

A 15-20 Amp 5V PSU would be a better choice.

Also, those logic level shifters are not a good choice for ws2812b led strips and will not give out a clean signal at the data speed used with these leds. A 74hc14 chip would be much better.

Yep, I guess I can see the problem there. Do you have any suggestion for a better PSU? I'm gonna look into it and also get a 74hc14 chip, see if that fixes it.

PaulRB:
Both codes light only the first led. It is difficult to see what is happening in your dark video, but I see maybe 3 leds blinking and 3 leds on steady. But they don't appear to be the first led.

You see 3 leds, because the led strip that i use has one controller for every 3 leds. So the first led (group) is fine, but where the blinking happens is just a random place, but it seems to be always those 2-3 places, if that makes sense.

Thank you guys for your time.

I'm sorry, I just realised I misread your posts. Your leds are ws2811 and require 12V power, not ws2812 which require 5V power. The 5V DC converter only powers the NodeMCU and logic level shifter, as you did explain. So the charger is a little underpowered, but should be OK if you do not use full brightness. The DC converter is more than enough for this purpose.

When I used ws2811 leds, I initially used a logic level shifter like the one you are using and it only just worked. Any disturbance from touching the circuit and the data signal would be corrupted. These boards were designed for use with i2c devices, which generally use a lower data speed than ws2811 and also require bi-directional conversion. WS2188 uses a higher data rate and requires only single-direction shifting. So these shifters are not the ideal design for led strips. Using the 74hc14 instead of the logic level shifter corrected this for me and I suspect may fix your problem also.

I also noticed that the circuit you are following has two important missing components. You should have at least one large cap, e.g. 1000uF, near the power input to the strip, and a resistor in series with the data line, around 500R, exact value not critical, also near the strip.

74hc14 contains 6 separate inverting buffers, so you will need to wire two of them in series. The other 4 will not be needed. Power the chip with 5V and don't forget to add a 0.1uF bypass cap close to the power pins.

PaulRB:
74hc14 contains 6 separate inverting buffers, so you will need to wire two of them in series. The other 4 will not be needed.

So you can either connect the inputs for those four to ground or connect one to ground and the others in series, or connect them in parallel with the second one to substantially increase the drive capability.

Similar to this but without the resistors:

Allright, I will get the missing parts and try to fix this thing. Thank you guys for your time and help, much appreciated!

A quick question tho, could someone explain why the error happens with the second sketch and not with the first one? I'm just trying to understand what the difference is.

I'm pretty sure it's because of how frequently FastLED.show() is called. In the first sketch, it is only called when the led states change, so after every 10s on and again after 1s off, so twice in 11s. In the second sketch, it is called every time loop() executes, even if nothing has changed, so probably hundreds of times per second. Every time it is called and data is sent to the strip, there is a possibility of the data being corrupted by the inadequate signal rise/fall rate of the logic level convertor.

If you change the second sketch to update the strip only when the led states have changed, it would probably appear to work just as well as the first sketch. But we know that's just sweeping the problem under the carpet. If the strip was running a pattern with frequent updates or slow, smooth fades, the problem would reappear.

PaulRB:
I'm pretty sure it's because of how frequently FastLED.show() is called. In the first sketch, it is only called when the led states change, so after every 10s on and again after 1s off, so twice in 11s. In the second sketch, it is called every time loop() executes, even if nothing has changed, so probably hundreds of times per second. Every time it is called and data is sent to the strip, there is a possibility of the data being corrupted by the inadequate signal rise/fall rate of the logic level convertor.

If you change the second sketch to update the strip only when the led states have changed, it would probably appear to work just as well as the first sketch. But we know that's just sweeping the problem under the carpet. If the strip was running a pattern with frequent updates or slow, smooth fades, the problem would reappear.

That actually makes a lot of sense. Thank you Paul for clarifying. I'll get the missing parts and fix everything. Appreciate your help.

Hey guys, so here I am with an update to my earlier post.

So I'm using the same components BUT as it has been suggested, I dumped my logic level shifter for a 74hc14 chip.

As the suggestsion I wired up the chip like this:

Paul__B:
So you can either connect the inputs for those four to ground or connect one to ground and the others in series, or connect them in parallel with the second one to substantially increase the drive capability.

Similar to this but without the resistors:

And of course added the 1000uF cap, near the power input to the strip.

So everything should be okay, but I am still experiencing some flashing, just like before changing to the 74hc14. Also, as soon as I add a resistor in series with the data line, doesn't matter how big or small, the leds just freeze, like they don't receive any signal.

It's probably because I'm a noob, but I am stuck with this. Also if someone would be so kind to make the schematics of this whole ordeal, that would be awesome, I might find something that I did wrong.

Quick edit: Also, I'm now testing with FastLED's ColorPalette sketch, so that I know it's not my code that is flawed.

@pezisaye
Please stick to your original thread for this so as not to waste peoples time.

Bob.

Now let's get this straight. (Thanks Bob, for fixing that for me. :sunglasses: )

If there is any length of cabling between the 74HC14 and the LED strip, the 470 Ohm resistor in series with the data line is physically mounted at the input to the LED strip.

Check the voltage supplied to the 74HC14,

Paul__B:
Now let's get this straight. (Thanks Bob, for fixing that for me. :sunglasses: )

If there is any length of cabling between the 74HC14 and the LED strip, the 470 Ohm resistor in series with the data line is physically mounted at the input to the LED strip.

Check the voltage supplied to the 74HC14,

Right now I wired up everything with a breadboard, so there is cabling between.. well, everything really. I was thinking of soldering everything together properly, but I wasn't sure it was a good idea, when I'm still experiencing the flickering. So yes, there is cabling between the chip's output that goes to the resistor, and that goes to the strip's input.

Do you think I'm losing enough signal strength through all these wires to cause this?

Also, thanks Bob for fixing it.