Is there any way to have an arduino detect how many LEDs are on a connected strip? I have a 60 strip and 144 strip. The code is identical for both, aside from setting the number of LEDs in a variable to iterate over.
3 Answers
Neopixels only have one-way communication. Some possible ways to detect the quantity are:
Bring the Data-Out of the last pixel back to the arduino and activate an interrupt on an input pin connected to that. If you write 144 LEDs worth of data to the 144-LED strip, the pin doesn't change. If you write 144 LEDs worth to a 60-LED strip, the pin changes. So your code has a startup routine to write data and detect it. You can even write all "black" colors, since the data will still have HIGH and LOW values, and your detect routine won't show any light patterns.
Pass the ground line that goes to the LED strip through a low-value resistor (A current-sense resistor) and use an analog input to read the voltage across the resistor. Write an all-white color to all NeoPixels and sense the current draw. Pre-calculate the 144-LED current draw and 60-LED current draw and set a cutoff value for knowing which strip is connected.
Make your LED-strip connector to have more pins than necessary strictly for the LED strip connections. For example, have 1 extra pin. On the 144-LED strip side of the connector, tie this connection to the 5V line. On the 60-LED strip, connect it to GND. Read HIGH or LOW on an extra digital input on your arduino.
Not without effort. You could take the DOUT from the last LED, feed it back into the MCU, and then detect how long it takes to get some sort of signal from it, but this requires either the end of the chain being very close to the MCU or a long wire to connect them. Easier to just have a DIP switch to set the number of Pixels in the chain and go from there.
Also see Is it possible to infer the length of a neopixel string using 1-wire protocol?. For example, one of Nick Gammon's answers includes code that detects NeoPixel string length via a binary search; Craig's answer refers to a Bunnie Huang blog with code and a library to detect lengths; and my answer includes datasheet links and quotes, and points out that if you put m packets of data into a chain and k packets come out the last unit, the chain length is m-k or more, using which fact can markedly speed up length detection. (All of these methods depend on reading data that flows out of the last unit of the chain; some of them use interrupts, some don't.)