Shift Register Powered Realistic Candle Flicker

[Kevin Darrah] recently went out to dinner at a restaurant that was using some cheap LED candles (yuck) instead of the real thing. And in the true spirit of a hacker, he started to notice the patterns programmed into the fake flame repeat over and over again. And like any hacker might, his mind started to devise a better way.

Now’s the time where some of us lazy hackers might grab a microcontroller, and copy and paste in some pseudo-random number generating code you found on the Internet, but not [Kevin]. The basics of his hack uses two shift registers tied together that are fed a single clock signal, and also a latch signal that is slightly delayed version of the same signal made by a RC-time circuit.

The randomness of the output is created is by feeding back the outputs of the shift registers to an XOR gate. If you want to learn more about this, the technique it’s called a “linear feedback shift register“. It’s commonly used as a poor-man’s random number generator, although it’s not technically truly random, statistically it does a very good job. You can see the results in the video after the break where [Kevin] describes the circuit.  He wraps up the hack with a battery and solar charging circuit as well to make a completed project.

54 thoughts on “Shift Register Powered Realistic Candle Flicker

        1. Call us when the shift registers and the XOR gate cost as much as your average computer…. Never mind the time spent learning an architecture, language and other fiddly details.

          Yeah, I’d say it’s cheaper for the two shift registers. Probably not faster, but it’s how you look at it.

          This is like arguing that someone who makes their own case using a hacksaw needs get a laser cutter because it’s, “better and faster.” Sure, *after* that person puts down $500 for a piece of shit laser, then spending hours fiddling with the damn thing then, yeah, it might be easier to use a laser. It’s certainly not cheaper though. It might not necessarily be faster if he wants/needs it today. A $5 hacksaw is certainly cheaper, and certainly faster, if the learning curve to use the laser is accounted for. Not saying a hacksaw has zero learning curver, but IMHO, it’s a lot faster to get into it.

          Like the comments below allude to. Hackers use what they have available, or at least know how to.

          I’m sure I’m like a lot of people here and have a shitload of microcontrollers in my tool box, but I don’t always reach for them when I want something done. The parts list cited in the video would cost me about ten cents or so. My AVR’s are a bit pricier than that. I think the cheapest (non Arduino of course) is about .25 less any support components.

          1. Except with the microcontroller, once you’ve learned it, you can make your LED do whatever you want. And once you’ve learned the laser cutter you can work far more efficiently than with the hacksaw.

            Also, have you accounted for the time to solder all that stuff together, and the increased likelihood of making an error that ether causes your circuit to fail to function or go up in smoke?

            “The parts list cited in the video would cost me about ten cents or so.”

            I checked Digikey. AtTiny is $1.60. Cheapest shift register was $0.50. And you need two of those, an inverter, and an xor gate.

          2. Lololol
            I can do this with my PC without spending 0.16 ¢ and if I want to make the led do more interesting stuff I just play crisis on my led monitor .

            Lololol

          3. @scswift

            Single unit prices? I gave prices what I actually paid because I guess I lucked out big time a while back but the relative experience is the same. No matter, let’s run with your logic. Let’s say $3.20 for the whole thing. Two shift registers, inverter, XOR gate and supporting hardware. Let’s give that the $1.60 covers the entire cost for building the equivalent circuit using the AVR.

            Checking Newegg, cheapest laptop is $160. A quick check on Google shows a USB based AVR programmer for about $8. (I paid about $12 for mine and it’s PIC based). We’ll ignore the supporting tools to solder the mess together. TO START, that’s $168+ tax and S&H _before_ you can even consider programming the AVR.

            We don’t see the cost of the PC as being a factor, just like we don’t see the cost of the soldering iron as a factor, because these tools and their costs are amortized across numerous projects. The tools are the entrance costs before anyone can even consider using the AVR in this type of project.

            This is why it’s absurd to tell people, “hey, just use your laser cutter because it’s better.” No shit. There are _tons_ of things that are made easier with, generally, more expensive tools. I can split wood using my axe, but I’d rather use my hydraulic log splitter. People who do that have completely missed the point, especially when it’s mentioned in the damn article.

            And yes, “Time spent learning” is never a waste, but why shouldn’t you learn how a shift register works? XOR gates? inverters? Nothing wrong with learning how to program a microcontroller (not just AVR), but there’s also nothing wrong with learning how to actually use some of those legacy IC’s. What happens if a person understands legacy IC’s before they come to grips with the move instruction?

          4. The computer is a sunk cost. Are are you reading this telepathically? And yes if you go from ebay you can include the programmer in your BOM and come out lower than the cost of this project.

          5. You do realize that he has posted a video of his gizmo online, don’t you? So that he likely has a computer to do so, right? Nice red herring argument, mate.

            You have a point on the price vs an AVR which are ridiculously overpriced, but then there are other chips, like the tiny PIC10s and PIC12s that cost cents. They would do this job easily and in much less space – which is likely something that would matter for the target application.

            Discrete logic is fine, but don’t dismiss programmable chips – you could be missing out on a good solution.

    1. Nice ,I think that’s the first time I’ve seen someone say “He could’ve used an Arduino/microcontroller instead of bothering with logic ICs”, instead of the usual “He used a microcontroller where he could have just used a 555 timer for a tenth of the cost.”

      1. Well, I did learn to use microcontrollers first. :)

        And honestly, if you do things this way you don’t have perfect control over what the output is like. In this case, he got a good result, but it probably took him a while to tweak it just so, and there’s a million other effects you might want to achieve which you would require entirely different circuits for. In contrast, if you just learn to use the microcontroller you can do any number of different effects quickly and easily.

        So it’s neat, but as a practical circuit I’d never design something this way; unless I just so happened to be designing mass produced items that had to be made as cheaply as possible.

        1. Quote: “you don’t have perfect control over what the output is like”
          – “probably took him a while to tweak it”
          – “In contrast”

          Well he wanted a pseudo random flicker and that’s exactly what a LFSR gives, so I guess he got exactly the output he wanted.
          The ‘tweaking’ would have been the same as you would have with a micro as it’s generally the analog (or real world) parts of the circuit that require the tweaking.

          So , in ‘contrast’ lol a micro would have been much the same.

          BUT (as required in the HAD comments negative feedback rule) –

          1) At least he didn’t do this with an Arduino.

          2) A 555 would have been better.

          3) This project sux – it has NO nixie tubes!

          </sarcasm>

  1. Dear people who post “this could have been done a different way.” Please, please, PLEASE stop posting! Just get your fucking head out of your a$$ and build the thing in the improved way and submit it! I’m 100% sure the editors will love to show your work. (Right editors?) When you post the way you do, you should like those kids on the playground watching other kids play and yelling “bad play. I could do it better.” And that’s pretty pathetic. I’d like to suggest people respond to these kind of posts with “sideliner’s are pathetic.” And maybe that will go away.

    1. Dear Alan it’s called constructive criticism, and helpful information. These people mean no harm by it, they are only trying to help.

      Just as a coach who thinks he knows better will correct a player these people offer helpful comments that will help anyone replicating this build. I would rather have people object to me than agree 100% as that is how personal growth happens. The important thing is that we do it in a civil way as we are on the same team.

      1. You could do this with an ic isn’t vonstructive, I’d bet nearly everyone who reads the comments on HAD knows an ic can blink an LED…
        Knowledge of LFSR are less common tho, and I bet few people actually considered making a hard ward one and not simply using it in your HDL designs (like I do).

        Good cheap sources of pseudo randomness is always a welcome fixture imho on HaD forget saying that you could do it on an ic and tell something interesting and I may not know about how.

        Would you use a Lehmer and take advantage of the natural mod 256 in an 8 bit ic????
        What would be your a and c? Why?

        Have a nifty means of doing it mod 65535? How do you use two bytes??

        I bet most of us have seen a linear congruential generator and just copy pasted it. Why not discuss it?

        Those options would be constructive…

    2. I’m with Alan on this one, when the article explicitly says “could have used a micro” and then people post exactly that it shows they didn’t grok the reason behind the article. Kudos to Kevin for explaining some fundamental electronics to a world obsessed with doing things “the easy way”.

      1. I sometimes get the feeling people aren’t genuinely offering constructive criticism so much as looking for an opportunity to show off that they know how to do a thing.

        It’s basically the hacking version of the guitarist “I could play it faster” stereotype.

    1. It’s a 16 bit Linear Feedback Shift Register (LFSR). It will repeat after 65,536 cycles. It’s probably just like the ones her replaced except they were probably an 8 bit LFSR so they would have repeated every 256 cycles and the repetition would have been noticeable.

      Still a good ‘throw together’ type hack though. LFSR’s are good when you only have to fool a human into believing a ‘pattern’ is ‘random’. I use them (LFSR’s) to generate test signals in FPGA during VHDL development.

      LFSR’s are like the Lorem Ipsum of the HDL world.

  2. Honestly, I’d love to have something like a mass airflow sensor (but instead of mass airflow, it detected micro air flow) so that if you walked by it or waved air at it, it would flicker, etc. Might produce a more natural flicker, without having to rely on randomness.

  3. A *real* artificial candle needs to have red, amber, and yellow LEDs (too bad that combination isn’t available on a chip!), and do combinations of the colors to simulate a flame. Would also be good for fake fireplaces.

          1. I wonder how hard it would be to tear about some monocolor LED packages, extract the chips, and solder (weld?) them back together in a 5050 shell… It would also be great to have a smart version with a “fire” driver chip built in, especially if it were addressable (like a WS2812) to modify its behaviour. I’ll bet someone could make big bucks on fake fire gadgets if these LEDs were available. We need someone with good Chinese skills to have a chat with an LED manufacturer.

        1. It seems comment threads can only go so deep, so I’m replying here instead. Maybe I’m wrong, but I think LED elements will oxidize and die if exposed to air, so it’ll be very hard to create a red-yellow-amber LED “manually”. I did find, however, some Red/Yellow BiColors. May not be perfect for the job but still easier than learning Chinese :-)

    1. To increase the possible variances in the output over a given time. The circuit isn’t a true “random” generator. So given enough time, a pattern does emerge.

      8 bits will eventually repeat the sequence every 255 ticks. 16 bits will repeat the sequence every 65,535 ticks.

      Though… I’m not entirely sure his circuit design guarantees that all 65,535 possible sequences will make an appearance. I’ve seen similar circuits before, but never actually had the need to construct one.

      1. Pedantically, this is not [i]quite[/i] an LFSR; it’s the inverted version (see the XNOR gate) that’s technically not linear.

        Which is good, because there are no 16-bit maximal-length 2-tap LFSRs. Unfortunately, his inverted one here with the polynomial X¹⁵+1 is not maximal period either. Simulating it, it seems to have a period of 255.

        1. I wondered about this too, so when rj pointed this out I ran a simulation and got the same 255 state sequence that rj indicated. 16-bits gives you 65536 states, but the XNOR gate and the taps that Kevin chose is not maximal length. Instead the logic partitions those 65536 states into 257 sequences of 255 states each. The initial conditions of the 16-bit shift register determine which of the 257 sequence will by cycled through. After 255 clocks, you’re back to the same initial condition. There is one degenerate condition of all-ones, state 65535 (FFFF) which continuously loops on itself. So 257 pseudo-random sequences of 255 states each is 65535, plus 1 for the degenerate sequence, yields 65536.

          With that said, its interesting that in his video Kevin indicated the clock rate at about 15Hz. So 255 states, sequenced at 15Hz, means that the candle would repeat after only 17 seconds. I attempted to play back the video in two separate YouTube windows and started one 17 seconds after the other. However, I never spotted the two candles flickering in unison.

          If Kevin wants to achieve a full 16-bit maximal length of 65535, he’ll have to use four-taps into an XOR network. Alternatively, if he wants to stick with a single XOR gate, he can discard the final stage in the shift register because for a 15-bit sequence one can create a maximal length of 32767 using an XOR feedback from bit-15 and bit-14.

          The 74HC595 might not be the best shift register for the job due to the output register. An additional signal had to be generated to clock the output register and using an RC delay in digital circuits is generally frowned upon. Perhaps the 74HC164 could be used. Simple and even cheaper.

          Finally, depending on the LEDs used, it might be possible to drive them directly from the shift-register output pins; they appear to be good for about 5mA each (remember, this is a dim candle LED). Kevin’s same idea of using a network of three weighted resistors to achieve different brightnesses would still work, along with his “softening” capacitor to smooth rough intensity transitions.

          Finally, finally, I hope he’ll experiement with additon a second network of resistors and an LED to try out emulating the way a flame jumps around the wick.

          1. Every time I look into doing anything involving (pseudo) random number generation it reminds me of a fantasy novel I read as a kid where the main character played an ultra-complex poker variant by laying down his cards, smiling smugly, and letting the other players figure out if he’d won.

            “Oh no! We lose because he has a side-straight mega-flush.”
            “No no, it’s tuesday and he’s facing north. It’s only a semi-mega-flush.”
            “You’re both wrong, it’s..” etc

            It’s a good thing there aren’t any lives relying on the code I write.

  4. The author describes using multiple drive transistors with weighted resistors to form a crude D/A (digital-to-analog) convertor. This softens the hard transitions so the intensity variations mimic an actual candle flame. These fluctuations occur with time.

    Another trick, that I’ve seen on similar LED candle emulators, is to add an additional LED to introduce spatial variations. The apparent center of the LED “flame” is the weighted average of the individual LEDs. In addition there are geometrical effects based on the angles that the LEDs are mounted as well as shadowing effects that the lens and lead frame of each LED has on itself and neighboring LEDs. These variations go a long way in mimicking the appearance of the flame as it dances around the wick, as well as wick shadowing.

    I was surprised how much the addition of just a single second LED, a few millimeters away, lent to the fullness of the effect.

    1. glad I’m not the only one who spotted that!
      Unrelated to your comment: All these commentaries on the limited number of steps before repeating make me think: Why not run the two shift-registers at slightly different clock-frequencies. Do that math, crazy calculator-fiends!

  5. Reading with some bemusement the discussion of “could have done it another way”.

    Of ourse there are other approaches that could be taken – but we are talking of a one-off hack here.

    I needed to produce some flickering LED lanterns and in the end bought flickering LEDs.

    The point though is if you only have a hammer I your toolkit, it limits the problem-solving approaches you have available.

    So why use shift registers rather than microcontroller? why not?
    Would it be “better” to use a germanium diode, transistor amplifier, and comparator?

    Not better but different – and that’s my point. Choose an approach tbat satisfies your requirements. If it takes longer but you derive pleasure or learn something on the way, then that’s no bad thing.

    Want to do it by programming? Fine – assembly language, higher level language (say C/C++) , or higher (NodeMCU)?

    Efficiency can become important if you need to repeat it several times or if you have a tight deadline.

  6. Many years ago I remember an electronics magazine (probably Gernsback’s Popular Electronics) having an article about making artificial flickering candles. They used thermistors if I remember right so that it would actually flicker when you blow on it. I never built one to see how they work but I like the idea that they actually respond to the real world. I suppose that article is probably available over at americanradiohistory.com. Too bad there isn’t an index to search!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.