Dynamically determine length of WS2812B LED strands

hey guys! I'm a huge fan of these forums and I have been searching for a few days here (and other places) and have not been able to find what i'm looking or. :frowning:

I have purchased a couple of these for experimenting and getting ideas http://www.aliexpress.com/item/3-Key-Mini-Controller-For-Test-WS2811-WS2812-WS2812B-Pixel-LED-Strip-Light-DC5V/1498586246.html and I'm trying to figure out how they have been able to determine the length of the strand. I have many of these light strands (ws2812B). And when connected to the device a 1meter strand will function the same way as a 5meter strand. Not just for rainbow affects but a chasing led affect. (Sends 2 pixels down the strand until the end is reached then starts over, as well as another which sends a few pixels (more on the larger strand so clearly math is used for this function) to the end with a faded tail until the end is reached, then it starts over again.

My only assumption is that due to the fact that the device is designed to run only so many leds that it is checking after each pixel color is set if that pixel actually has a color. if there is no pixel then it returns a false/null type statement thus restarting the affect in the next color. The adafruit library does this...are there others?

Also, is there a repository for many light affects? I really like the water torture affect, but everywhere I go it's in java script. no c++ :frowning:

Anyway, thanks in advance everyone!!!!

There's no way to tell how many LEDs are connected. You change the effects by editing the program to match the strip.

A repository for many light affects? Try your imagination...

Presumably you could loop the shift output from the end of the chain back to the controller and examine the codes received, but considering how tricky it is merely to transmit the codes in the first place, this would be quite a challenge.

Paul__B:
Presumably you could loop the shift output from the end of the chain back to the controller and examine the codes received, but considering how tricky it is merely to transmit the codes in the first place, this would be quite a challenge.

Hmmm...I guess that might work. When you see data coming out of the last LED you know you've reached the end of the strip.

So, i've tested the getpixelcolor method using this

#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(100, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  Serial.begin(115200);
  Serial.println("First_Color");
  Serial.println(strip.getPixelColor(60));
}

void loop() {
  // Some example procedures showing how to display to the pixels:
  colorWipe(strip.Color(255, 0, 0), 50); // Red
  Serial.println("Pixel 1");
  Serial.println(strip.getPixelColor(1));
  Serial.println("pixel 44");
  Serial.println(strip.getPixelColor(44));
  Serial.println("pixel 60");
  Serial.println(strip.getPixelColor(60));
  Serial.println("pixel 120");
  Serial.println(strip.getPixelColor(120));
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

but all that did was give me the logic that was supposed to go to that pixel. not read what was actually in that pixel. I'll have to test Octows2811's library as it has a similar function. Maybe it runs it differently.

There's no way to tell how many LEDs are connected. You change the effects by editing the program to match the strip.

My controllers say differently my friend. as a 1m strip will function the same as a 5m strip which would require being able to determine the number of functioning LEDs. I'm assuming the method is relatively old as these controllers (though really new) were designed for compatibility with the ws2811 hardware.

A repository for many light affects? Try your imagination...

This is backwards support. I'm currently building a repository of collected and homemade affects. I'm at just over 30 affects currently (and growing). Which isn't very many I know, but will be nice to be able to share them with people. I just didn't want to rebuild something that had already been publicly built. Instead I'd rather add or touch up what was already existent.

Presumably you could loop the shift output from the end of the chain back to the controller and examine the codes received, but considering how tricky it is merely to transmit the codes in the first place, this would be quite a challenge.

That does sound very helpful! hrmmmmm...I wonder how one would best go about doing this. I'm learning a lot about these lights...they are very impressive! :slight_smile:

Thank you everyone for the help and support on this. I look forward to reading more as it comes. :slight_smile:

V1x0r:

There's no way to tell how many LEDs are connected. You change the effects by editing the program to match the strip.

My controllers say differently my friend. as a 1m strip will function the same as a 5m strip which would require being able to determine the number of functioning LEDs. I'm assuming the method is relatively old as these controllers (though really new) were designed for compatibility with the ws2811 hardware.

Are both ends of the strip connected to the controller?

I can send 5m of data to a 1m strip, no problem. It will simply display 1/5 of the pattern.

It is connected to a single end. This device can send (for instance) a few leds down the strand and cycle right after it reaches the end. your example would have a pause if you wrote it for 150leds. This device does not have a pause. it starts over right after it reaches the end of the strand. with 30 leds, or with 150 leds.
It IS dynamically determining the length of the strip. Whether it is a check before executing light affects or during. I'm more curious how to replicate this. a cheap unit can do it effectively as well as with additional modes, speed transitions, etc. I've tested this repeatedly on my strands and it is impressive. I'm trying to talk the manufacturer out of the source code but I doubt I'll get it. So far I can get them to build a custom device for me at least if I supply them my stuff.

V1x0r:
It is connected to a single end. This device can send (for instance) a few leds down the strand and cycle right after it reaches the end.

Maybe it monitors the current draw. Does the 5V power go through the device?

V1x0r:
It is connected to a single end. This device can send (for instance) a few leds down the strand and cycle right after it reaches the end. your example would have a pause if you wrote it for 150leds. This device does not have a pause. it starts over right after it reaches the end of the strand. with 30 leds, or with 150 leds.
It IS dynamically determining the length of the strip. Whether it is a check before executing light affects or during. I'm more curious how to replicate this. a cheap unit can do it effectively as well as with additional modes, speed transitions, etc. I've tested this repeatedly on my strands and it is impressive. I'm trying to talk the manufacturer out of the source code but I doubt I'll get it. So far I can get them to build a custom device for me at least if I supply them my stuff.

Put it in debug mode and run it and check the loops yourself. It it probably sending 5 meters worth of data regardless of what is connected. The patterns will still look the same - you're just viewing a "window" on the whole pattern when you have less lights.

Post the code, maybe I can explain it. I've worked with these quite a bit. There is no repository of effects because most of them are visually obvious what it's doing, but I've posted a couple effects on Github.

(There was a few more codes up there, Github isn't playing nice, I'll see if I can get them back up there.)

fungus:
Maybe it monitors the current draw. Does the 5V power go through the device?

That's what I was thinking at first too. it is on a 5v circuit. But how would it know it's at the strand? i've monitored voltage draw with my own setup so as to determine how large of a battery pack will be needed to run the needed amount of hours and unless it's on a change affect it stays pretty stable on the draw. Which makes sense...unless it is sending through a very large strand it shouldn't have any problem with voltage other than natural resistance from the components. I haven't tested the octows2811 library yet, but it does give support for get led color...maybe it doesn't reference the code and instead references the colors actually on the chip. :confused:

@Jasmin - Thank you for your suggestion. the problem is with trying to reverse engineer someone else's code based on how it appears to function. i.e. Sending 2 pixels of color to the end of the strip, and as soon as it reaches the end it starts over again with a very very short cycle to the beginning (almost instantly). This is true for 30leds to 180 leds. 150 led difference on a slow line send would have a very long wait before recycling the lights again.

@Jasmin - Thank you for your suggestion. the problem is with trying to reverse engineer someone else's code based on how it appears to function. i.e. Sending 2 pixels of color to the end of the strip, and as soon as it reaches the end it starts over again with a very very short cycle to the beginning (almost instantly). This is true for 30leds to 180 leds. 150 led difference on a slow line send would have a very long wait before recycling the lights again.

That isn't how it works. When you send data to the line of WS2812s (or TM1803s) you are sending a complete data packet with a space, then an "EOF" signal. The chips don't reset the LEDs to a new color until they see the "EOF" signal. It's not like a chip holds a color and then passes it to the next one on the next refresh. You have to refresh ALL the LEDs EVERY TIME.

So, here's how it works - lets say you have 10 WS2812s on a line...
You can send 10 RGB colors down that line - the WS2812s will eat each color in sequence, color0 will go to chip0, color1 goes to chip1, etc... then you send the EOF signal, and at that point, is when the WS2812s will light up with the colors you just sent.

You can send 20 colors down the line - 10 WS2812s will eat the first 10 colors, and the last 10 colors will be sent "to nowhere" because there's nobody connected to the last chip. It will begin passing data after 10 colors arrive, but that data won't go anywhere.

You can send 5 colors down the line - the first 5 WS2812s will light up, and the last 5 will NOT - they will never see anything.

So, in the case you're describing, where you have 1 meter of lights and 5 meters of data, it's just ignoring 4 meters worth of data, and going slower than it needs to. That is just how the WS2812 works, and there isn't any way to check how many are connected. Look at the data sheet... it explains the 50us "frame sync" and everything.

And yes, it does have a "data out" pin - but that pin doesn't do anything but pass the data through, after the chip receives 24 bits. Each chip on the line does that - it eats 24 bits and passes everything else along the line, waiting for the EOF signal.

I think what V1x0r is saying is that he has a strip of 10 LEDs where just the first led is lit. That lit LED is the "moved" to the second position after a second. This repeats until the lit LED reaches the 10th position and after a one second gap that lit LED appears on the first position again.
Now with the same code you attach a 20 LED strip on it. This time the lit LED goes all the way to the 20th position before coming back to the start.

Myself I think V1x0r is wrong but that is what he appears to be saying.
He does say:-

i've monitored voltage draw with my own setup

now there is no such thing as a voltage draw, only a current draw which gives some room to some doubt him / her.
@V1x0r - is this a true reflection as to what you are saying? If so can you post the code that does this please.

Grumpy_Mike:
I think what V1x0r is saying is that he has a strip of 10 LEDs where just the first led is lit. That lit LED is the "moved" to the second position after a second. This repeats until the lit LED reaches the 10th position and after a one second gap that lit LED appears on the first position again.
Now with the same code you attach a 20 LED strip on it. This time the lit LED goes all the way to the 20th position before coming back to the start.

Yeah I know, that's a chasing effect... you do need to know how many LEDs are on the string if you don't want there to be a delay between the last one and the re-start. If he has a controller where you can have that kind of chasing effect (where all the lights are unique) and it will "fix" the sequence when you just chop off the end of the LED strip, then I really would love to see the code, and the wiring on it. As far as I know, there is no way to determine how many LEDs are on the strip, unless you bring the last data line back in to the controller. THEN, you could listen, just as the WS2812 does, to see when you start to see bits, because that pin would see nothing until you sent enough bits for all the WS2812s on the line. But, that would require re-wiring if you wanted to change the length of the string.

To do a chasing effect, lets say you could turn LEDs on and off with one bit instead of 24... this is what you would do...

1000000000
0100000000
0010000000
0001000000
etc...

So, if you change the number of lights, you have to change the number of blocks of data you send out with each frame. Rainbow chasing effects are visually confusing and it may appear fine when you cut things short even if you aren't seeing the whole pattern.

Oh BTW, you can't measure the current draw and do some kind of calculation because the different colors are produced by sending different current amounts to each LED. I suppose you could be sure if you lighted up the whole strand with full strength white, then each WS2812 should draw 60mA, but I don't know how accurate that would be.

@V1x0r - is this a true reflection as to what you are saying? If so can you post the code that does this please.

I don't think it's easy to pull the code off the product he's using. A lot of companies sell products like it though, and there is plenty of code for it out there. One company that sells them has code here...

But, that's just the library to drive it, you still need to write code. I show how it all works in this video... (It's about the TM1803 but they work the same)

I did mean current draw, my apologies. I'm measuring overall wattage. Suffice it to say I was pretty tired when I wrote my last posting.
I also understand how the lighting is sent through each led on the strand.
I'm going to flat out say that you are wrong. The device is determining the length of the strand. It is not using the poorly written Pololu library. (my opinion on the library). I am very impressed with how this device functions. You are used to having to put code down to define length. This is (in my opinion) bad form overall. Being able to determine the length logically is very impressive to me. And the lighting affect and outcome that grumpy mike stated is correct. If you do a very basic "Chasing" affect illuminating two leds at a time down the strip until the final end is reached and then start back at the beginning of the strip immediately after completion then you have the general idea of what this does. YOU and I have to write down the number of LEDs for this affect to work properly between different sizes of strips. THIS device that I originally posted DOESN'T do that as the effect is exactly the same without delay between the different sizes. If you would like I can post a youtube video to prove my point.
I may be relatively new to the addressable LED side of things, but in regards to electronics and coding I am FAR from new. I understand these forums are usually started by novices in all fields but please do give understand and respect to the fact that I am not a complete fool. Clearly you all are just as confused as how this would function as I am. I greatly appreciate the support when support is given, but I do not care to be spoken down to because you assume I am wrong because I must be new. Thank you again for your time on this and I do hope we can go further on this as it clearly would be beneficial for all of us to know how this works as a hobbyist. I am tempted to pull the code from the chips themselves as they are essentially an attiny25 or 45 for the smaller device. Problem is converting from machine code to understandable code.

Something else to consider, is that these were likely designed with a different avr designer not using the standard libraries available to arduino users. so a custom library would likely have been used for this. something liked Edit2012/ Edit2013 could have been used also. As for the LEDs holding the light color...take two arduinos. externally power your led strip, (ws2811 ic based, so ws2811, ws2812, and ws2812B) and connect the grounds between the two arduinos and the lighting strip as well as the power source (which should be the same connection to your led strip), usb power your arduinos....one arduino send a color light segment to your strip...varying colors and so forth), disconnect your data pin from arduino 1 and connect it to arduino 2..on arduino 2 have a get color code (ONLY GET COLOR) code...and see what data you get back from your leds. I do want to try this myself personally...I have a few arduino's at home (one is devoted to being an ISP for attiny but can still use to check)...if the second arduino receives information from the leds that the first arduino sent then that would make sense why my RF version of this adapter has two microcontrollers connected in parallel. I thought it was for double the code...but the second could be a check segment...I still think parallel'd for double the code.

Anyway...thanks again everyone. :slight_smile: @Fungus...where are you? you know more than any of us do on here. :slight_smile:

I'm sorry but I'm not confused, I know how the WS2812 operates. I also know how that strip is wired up, but I am not sure about the connections to the controller. If it has a return data line, it could easily count the LEDs, and I can now think of a couple ways to calculate it or figure it out by measuring the current draw. The thing is, you can not send the same data to a different length of lights. You must adjust the number of bits you send in the frame before the refresh, or you will either fail to light up all the lights, or you will light up extra lights that don't exist. That why I chose to enter this discussion, because I think it's a very good question. The thing is, if we can't see the code or the wiring, we can't know what method your controller might be using. And, I apologize and realize it isn't helpful to tell you the ways it won't work. So, here's how it could work...

I can suggest a way to sort of quickly measure the length of the strand by current measurements. Simply send zeros down the line until you get to the 500th light, then send 24 ones to make a full white display. If you detect no current, try again with 499 - eventually you will get to the point where you light up exactly one light at the end of the strand, your current sensor will be non-zero, and you will know that's the last one. I suppose you could do that dynamically during the effect as well.

OH BTW... you found this, right?
https://github.com/DannyHavenith/ws2811/blob/master/src/water_torture.hpp

Maybe this will help. I am actually working on something with this stuff right now so I had it in Fritzing anyway. This image is a little confusing but it's basically what you describe, with a second controller at the end of the data line. In this example, there's only one WS2812, and its "Dout" pin is connected by the highlighted wire to the digital pin on the controller on the right. Data comes from the controller on the left.

What will happen here is, if you send 24 bits and then the EOF signal, the Arduino on the right will never see anything. The line will remain low the whole time. If you send 48 bits, enough data for two lights, the WS2812 eats the first 24 bits while leaving "Dout" low, then when it sees the 25th bit it passes that bit to the Arduino in our case, in most cases, to the next WS2812. So, we could send 24 bits for a test, get nothing, try 48 bits, see data, and know we only have one WS2812. The important thing to remember is that each WS2812 sees nothing until the previous chip has seen 24 bits of data. Once it sees 24 bits, it passes all the subsequent data on the "Dout" line.

I must apologize...that small adapter only does 50 light sequences. so every 50 lights it runs the same code. however, it still functions the same where it will instantly adapt from the smaller 30 led strip and re-cycle the affect instantly upon reaching the end of the strand. with the 5m strip it will restarts as soon as it reaches the 50th led on the strand OR the final led on the strand. I say OR because on one of the settings it doesn't wait for the 50th light to be lit but instead it finishes once the final LED is lit. naturally 5meters of this isn't 50 leds a meter, it is 30 a meter so the strip gets restarted much sooner. The remote control version is the one that goes to the end completely. (it is also advertised for more leds however). I ordered another small inline adapter that has all the affects as the large remote adapter so I'm waiting for that to come in. maybe it will do the same as the remote. it is newer after all. still, i'm very impressed with the fact that it knows once the end of the strip has been reach.

your image is intriguing. but it would require a loop of some form. there is nothing attached at the end of the strand. and only utilizes three pins.
The attached image displays four pins, the two center pins are grounds. I'm a bit upset that they scraped off the info of the chips, but the first chip is an atmel chip for the RF receiver. the other two are essentially attiny components. (not actually attiny...but essentially the same thing).

IMAG0518[1].jpg

IMAG0520[1].jpg

The small remote looks like this.

Hopefully this site doesn't downgrade the image quality...if it did then the chip is

STC
15F104E
HAH388.A

Alibaba and ebay sells these like crazy

IMAG0532[1].jpg

IMAG0533[1].jpg

I think a video of these effects might be helpful. I think maybe I can figure out what's going on. The adapter units seem to only have three wires, so I really don't think they are determining the length of the strand. It just doesn't seem possible.

In the image I posted, notice that there are four things connected to the WS2812 - power and ground, data in and data out... data in is connected to the left controller, data out to the right controller. This setup would work, and the right Arduino could see the data, except for the first 24 bits in each frame in this case. The LED strips would be the same circuit, but more WS2812s in between.

I completely agree!!! this does NOT seem possible. which is why I posted on here. this concept seemed dumbfounding to me.

The leds maintain a memory though...if you are sending a signal to the leds then disconnect the signal they do not simply turn off. they instead hold to the last bit of signal that they had before. which means they have a value written to them temporarily. and to pull this value only seems like the next logical thing one could do. instead of a send request you create a get request. as for forming this get request I wouldn't know how to build it. but I would assume that the great minds of china have probably already figured this out...especially since they are the once who mass product these things.

I will have to upload a video tomorrow of these affects. I gave my a 1m strip and an arduino so he could play around with it (his code is absolutely beautiful compared to what i'm used to seeing. it's much smaller and more capable. I will definitely have to share his stuff when he is comfortable with me sharing it. Anyway...I'll have to cut a strip a to show you what i mean. :slight_smile: i'm off to bed now though.