LED Flickering issue with Neostrip led

The below is the code, i tried making a project where i will glow 6 leds only after getting output from PLC, below is the code after using below code leds are glowing even without getting output from plc, and when output of plc is provided as input to the leds, leds start flickering(i.e High, Low simultaneously).

#include <Adafruit_NeoPixel.h>

const int ledCounts[6] = {52, 40, 17, 17, 16, 16};
const int stripPins[6] = {8, 9, 10, 11, 12, 13};
const int inputPins[6] = {2, 3, 4, 5, 6, 7};

Adafruit_NeoPixel* strips[6];

void setup() {
  Serial.begin(9600);

  for (int i = 0; i < 6; i++) {
    pinMode(inputPins[i], INPUT);  // Real PLC input

    strips[i] = new Adafruit_NeoPixel(ledCounts[i], stripPins[i], NEO_GRB + NEO_KHZ800);
    strips[i]->begin();
    strips[i]->show();  // Clear LEDs
  }
}

void loop() {
  for (int i = 0; i < 6; i++) {
    strips[i]->clear();

    if (digitalRead(inputPins[i]) == HIGH) {
      uint32_t color;

      if (i < 2)        color = strips[i]->Color(0, 0, 255);   // Blue
      else if (i < 4)   color = strips[i]->Color(0, 0, 255);   // Green
      else              color = strips[i]->Color(255, 255, 255); // Yellow

      for (int j = 0; j < ledCounts[i]; j++) {
        strips[i]->setPixelColor(j, color);
      }
    }

    strips[i]->show();
  }

  delay(100);
}

Unless connected to something, your inputs are floating, and will randomly read high or low. Make the pinMode call
pinMode(pin, INPUT_PULLUP);

and you will consistently get a high if the input is floating.

As for the rest of your loop(), read through it carefully, I don't think it's doing what you think it's doing; but, since you didn't describe what it should be doing, I may be wrong on that.

1 Like

Already used all the methods still not wokring, and the thing i want to achieve is i want to glow led strip when output from plc is high and want to turn off led when input got low from plc.

2 is the Pin where led is connected and 3 is pin where PLC output will be given.

The input pins should be LOW by default. You can do this by adding external 10kΩ pull-down resistors from each input pin to GND or enabling internal pull-downs (if your board supports it — e.g., ESP32 or STM32).

On an Arduino Uno or Mega, internal pull-downs are not available, so you must add external resistors:

// Add this in setup if your MCU supports pull-downs (NOT Uno or Mega):
pinMode(inputPins[i], INPUT_PULLDOWN);

1 Like

Does the PLC have open collector / open drain output, or does it actively pull the output HIGH?

How are you powering the LED strips?

< edit >
There is no need to call show() for every strip every time through loop(). Better to detect when the input changes state, and only call show() when the input transitions from LOW to HIGH or HIGH to LOW.

1 Like

I am using PLC Siemens S7-1200, which has digital transistor outputs (sourcing type) — they actively pull the output HIGH when ON, and float or pull LOW when OFF.

The NeoPixel LED strip is powered by arduino 5V itself connected to MY laptop Via Cable

You need a much larger power supply. With a total of 158 LEDs, I would use a 10 amp power supply if there is a possibility of having all LEDs on full brightness with white color, although with your current code a 5 amp supply should be sufficient.

Do they actually float at an unknown state or are they actually pulled LOW ?

Not consistent with your code.
const int inputPins[6] = {2, 3, 4, 5, 6, 7};

Please review.

158 LEDs, yes, too much current draw for USB. Provide external supply.

Have a look at a similar project that uses a library and correct coding to operate the NeoPixels. Now see what your code is missing.

@otproject By the way, Welcome to the forum, and congratulations for posting your code correctly first time!
Please remember to post a new version of your code after consideration of feedback so far, explaining what it does now vs what you want it to do. When you do post new code, DO NOT update your first posts, but rather create a new post with the new version, to maintain the timeline.
Thanks

To what voltage? If greater than the voltage your Arduino is running at, you need to be careful.

Which is it? It matters.

Your code simulates perfectly if you tie all your button pins LOW.

ezgif-4ea44bcab32185

click here for diagram.json tab for wokwi
{
  "version": 1,
  "author": "",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -43.2, "left": 9.1, "attrs": {} },
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring1",
      "top": -301.92,
      "left": -14.83,
      "attrs": { "pixleate": "1", "rows": "4", "cols": "13" }
    },
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring2",
      "top": -301.92,
      "left": -274.67,
      "attrs": { "pixleate": "1", "rows": "4", "cols": "10" }
    },
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring3",
      "top": -189.36,
      "left": -12.48,
      "attrs": { "pixleate": "1", "rows": "2", "cols": "9" }
    },
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring4",
      "top": -189.36,
      "left": -252.48,
      "attrs": { "pixleate": "1", "rows": "2", "cols": "9" }
    },
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring5",
      "top": -122.16,
      "left": -9.5,
      "attrs": { "pixleate": "1", "rows": "2", "cols": "8" }
    },
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring6",
      "top": -122.16,
      "left": -220.7,
      "attrs": { "pixleate": "1", "rows": "2", "cols": "8" }
    },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -344.84, "left": -28.8, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": -57.6, "left": -29.4, "attrs": {} },
    { "type": "wokwi-dip-switch-8", "id": "sw1", "top": -32.1, "left": 183.9, "attrs": {} },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": -140.05,
      "left": 230.4,
      "attrs": { "value": "10000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r4",
      "top": -130.45,
      "left": 230.4,
      "attrs": { "value": "10000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r5",
      "top": -120.85,
      "left": 230.4,
      "attrs": { "value": "10000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r6",
      "top": -111.25,
      "left": 230.4,
      "attrs": { "value": "10000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r7",
      "top": -101.65,
      "left": 230.4,
      "attrs": { "value": "10000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r8",
      "top": -92.05,
      "left": 230.4,
      "attrs": { "value": "10000" }
    }
  ],
  "connections": [
    [ "vcc1:VCC", "ring1:VCC", "red", [ "v115.2", "h163.2" ] ],
    [ "vcc1:VCC", "ring2:VCC", "red", [ "v115.2", "h-134.4" ] ],
    [ "vcc1:VCC", "ring3:VCC", "red", [ "v182.4", "h115.21" ] ],
    [ "vcc1:VCC", "ring4:VCC", "red", [ "v182.4", "h-124.79" ] ],
    [ "gnd1:GND", "ring5:GND", "black", [ "v-9.6", "h86.4" ] ],
    [ "gnd1:GND", "ring6:GND", "black", [ "v-9.6", "h-115.2" ] ],
    [ "gnd1:GND", "ring3:GND", "black", [ "v-76.8", "h105.6" ] ],
    [ "gnd1:GND", "ring4:GND", "black", [ "v-76.8", "h-144" ] ],
    [ "gnd1:GND", "ring2:GND", "black", [ "v-144", "h-153.6" ] ],
    [ "gnd1:GND", "ring1:GND", "black", [ "v-144", "h153.6" ] ],
    [ "nano:13", "ring5:DIN", "green", [ "h-9.6", "v-86.4", "h76.8" ] ],
    [ "vcc1:VCC", "ring5:VCC", "red", [ "v249.6", "h105.6" ] ],
    [ "vcc1:VCC", "ring6:VCC", "red", [ "v249.6", "h-105.6" ] ],
    [ "nano:12", "ring6:DIN", "green", [ "v-28.8", "h-144" ] ],
    [ "nano:11", "ring4:DIN", "green", [ "v-28.8", "h-57.6", "v-67.2", "h-115.19" ] ],
    [ "nano:10", "ring3:DIN", "green", [ "v-28.8", "h-67.2", "v-67.2", "h124.81" ] ],
    [ "nano:9", "ring2:DIN", "green", [ "v-28.8", "h-76.8", "v-134.4", "h-124.8" ] ],
    [ "nano:8", "ring1:DIN", "green", [ "v-28.8", "h-86.4", "v-134.4", "h172.81" ] ],
    [ "nano:2", "r8:1", "green", [ "v-19.2", "h96", "v-19.2" ] ],
    [ "nano:3", "r7:1", "green", [ "v-19.2", "h105.6", "v-28.8" ] ],
    [ "nano:4", "r6:1", "green", [ "v-19.2", "h115.2", "v-38.4" ] ],
    [ "nano:5", "r5:1", "green", [ "v-19.2", "h124.8", "v-38.4" ] ],
    [ "nano:6", "r4:1", "green", [ "v-19.2", "h134.4", "v-48" ] ],
    [ "nano:7", "r3:1", "green", [ "v-19.2", "h144", "v-76.8" ] ],
    [ "nano:GND.2", "r8:2", "black", [ "v-9.6", "h86.4" ] ],
    [ "r8:2", "r7:2", "green", [ "v0" ] ],
    [ "r7:2", "r6:2", "green", [ "v0" ] ],
    [ "r6:2", "r5:2", "green", [ "v0" ] ],
    [ "r5:2", "r4:2", "green", [ "v0" ] ],
    [ "r4:2", "r3:2", "green", [ "v0" ] ],
    [ "nano:5V", "sw1:1a", "red", [ "v9.6", "h57.6" ] ],
    [ "sw1:1a", "sw1:2a", "green", [ "v0" ] ],
    [ "sw1:2a", "sw1:3a", "green", [ "v0" ] ],
    [ "sw1:3a", "sw1:4a", "green", [ "v0" ] ],
    [ "sw1:4a", "sw1:5a", "green", [ "v0" ] ],
    [ "sw1:5a", "sw1:6a", "green", [ "v0" ] ],
    [ "r8:1", "sw1:1b", "green", [ "v0", "h-19.2", "v48", "h-19.2" ] ],
    [ "r7:1", "sw1:2b", "green", [ "h-19.2", "v57.6", "h-9.6" ] ],
    [ "r6:1", "sw1:3b", "green", [ "v0", "h-19.2", "v57.6" ] ],
    [ "r5:1", "sw1:4b", "green", [ "v0", "h-19.2", "v76.8", "h9.6" ] ],
    [ "r4:1", "sw1:5b", "green", [ "v0", "h-19.2", "v86.4", "h19.2" ] ],
    [ "r3:1", "sw1:6b", "green", [ "v0", "h-19.2", "v96", "h28.8" ] ]
  ],
  "dependencies": {}
}