Like a lot of my projects that I create, I intend to have the project perform some function that is useful to me, but at the same time, learn about some previously unused technology or feature. Due to this, most of my projects aren’t very cost oriented to reproduce what I’ve created. I will say that most of my projects are produced in small quantities for almost nothing. Most everything I’ve created with the exception of a few projects such as the Nixie Clock, LED Matrix Display, Thermostat, and OBDII Bluetooth adapters have been built almost entirely from reused parts or free samples.
The main goal of the Pumpkin Lights project was to create a cool method of lighting a pumpkin instead of using a candle or boring old LED tea lights. I wanted something that could produce infinitely variable colors or even mimic a flame if I wanted it to. The technology I wanted to fool with were Microchip’s faster, cheaper 16-bit PICs and PWM (pulse-width modulation) LED control. Up until this project I had only used the 18F 8-bit parts. The whole motivation for this project was from a previous analog version of this, which was based on the popular “Fading Red Eyes” circuit that’s easily found on the net. While the analog solution worked, I didn’t have the control over the colors produced like I had wanted to. It still produced a really cool pumpkin display, but I knew I could do better.
The pumpkin in the link above is one I carved for halloween 2010. It’s hard to see the different colors the unit can produce as the pumpkin tends to turn the lighter colors orange, since the LEDs are bright enough to make the whole pumpkin glow reddish-orange, even through the flesh and skin of the pumpkin.
I ended up picking a PIC chip that was designed for motor control as it had a number of individual true PWM controllers build into it. Each PWM controller shares the same timebase, but the pulse width for each output was adjustable over a 16-bit range. This variability allowed me to change each LED’s pulse width (I’m using a high flux red, green, and blue LED in a TO-220 case from Marktech Opto) and essentially create millions of unique colors (sorry, I haven’t sat down and actually counted the different colors I can produce). The other cool thing about the red green and blue LEDs is that the color mix is additive, not subtractive as with paint, meaning the brighter the output from each LED, the closer you get to attaining white light instead of getting closer to black by adding more and more paint colors together. Since all the LED colors aren’t on the same package and really close together, you get some coo multicolored shadows depending on an object’s location relative to the LED positions. I did mount the LEDs as close together as possible, but I can’t get them as close together as a real RGB LED could.
Also, I have a white LED on it’s own distinct PWM channel, which I use for a strobe. I can vary the frequency of the strobe on the fly, which results in some neat sorta stop-motion effects. The strobe is also quite harsh on the eyes if you’re in a small room, and probably seizure inducing too. I don’t have the strobe mode on long when it’s in the pumpkin so that it’s not super distracting for people driving by.
Of course by using a new, unfamiliar PIC, I also had to use a new compiler as the 16-bit PIC’s internal architecture is much different from the 8-bit PIC. It also turns out that the PCW compiler from CCS is very buggy, which is really disappointing as their 8-bit compiler is so nice. Granted the 8-bit compiler is still buggy, but not to the extent that the 16-bit is. The nice thing about the CCS compilers is that they take care of all the register setup for sending out data on SPI, I2C, UART, …etc. Essentially you can just use these peripherals instead of having to figure out how to setup all the registers. This works nice in 8-bit land, but is broken here and there in 16-bit land, especially for the true PWM controllers in the 16-bit PIC. In fact, the PWM stuff in PCW is completely broken. It doesn’t work at all. Setting up the registers for the PWMs is no walk in the park either. Luckily I found some examples from Microchip (using the C30 compiler) and figured out how to set up the registers properly. Now I’ve got my own PWM setup functions and don’t use the CCS provided ones at all. Problem solved, and I had colors being mixed with the LEDs, which was really neat.
After a while, I got sick of the always the same variations in the LED colors. I had to make this random, and I set out to do so. Luckily PCW’s random function still worked, so I figured I’d use that to create random pulse widths and ramp on/ramp off delays. While the random function worked, I soon discovered what a pseudo-random generator is (PRNG). Yes I was indeed getting “random” values, but it’s completely predictable. On every power cycle, my controller would have the exact same random value each time the random function was called. This was not acceptable. Granted no one else would ever know, but hey, it annoyed me.
I then set out to find out how to make a truly random number generator (TRNG). At first this seemed like it was going to be really hard to do as I couldn’t think up any way to eliminate predictability in the PIC, since by nature, these have to be predictable or else it would be really hard to run software on these things. I did finally discover that you can use a PRNG, but seed it with a value coming from something truly random. I just had to figure out how to make something truly random. It turns out this was way easier than I had thought: Create a ring oscillator. There’s not much use for such a thing since all it is an odd number of inverters strung together in series and the output of the last gets connected to the input of the first. The oscillation occurs because of the delay of the output changing some amount of time after the input changes. The more inverters strung together, the slower your ring oscillator clock output will be (use the output of the last stage as the clocking source to whatever you’re clocking with this thing).
As it is, it doesn’t sound like the ring oscillator is terribly random. I mean you don’t really have control of what state it starts up in, but that’s defined to be only a 1 or a 0. That’s not random! The randomness of a ring oscillator comes from the time delay of the output changing from a change on the input. This time delay is infinitely varied due to the internal temperature of each inverter. As the temperature fluctuates even a small amount, the output frequency varies. The environment is a perfect source for randomness, and the ring oscillator is a convenient way of utilizing the environment’s randomness. The frequency of this oscillator is always changing, and is never exactly the same frequency at any two points in time.
Now that I had a random source, I fed this oscillator into the secondary clock input of the PIC and on startup, I let a counter count based on the random clock and then after a few seconds of collection time, I use that counter value to seed the PRNG, which creates a TRNG! I can honestly say the patterns the unit produces are never the same anymore!
Since this thing is all software controlled, I could go about trying to mimic a flame’s varying color and flicker, or do some other lighting effect. Right now all that is completed is the infinitely random color fading in and out and switching briefly to the strobe effect.