picture home | pixelblog | qt_tools

omino code blog

We need code. Lots of code.
David Van Brink // Sat 2008.08.9 21:58 // {applet wbl weird blinking lights}

Nightlight Algorithm

For our Trylon and Perisphere installation, it seemed like it’d be more dramatic if the lights only came on at night. Some of these lights are Cool Neon, and others are Pic-controlled LEDs. To do this required three problems to be solved: 1) sense light, 2) switch circuits of various voltages on and off, 3) make the switching robust against shadows, twilight, and flashlight-weilders.

Sensing Light

I went to Radio Shack with some vague idea of getting a phototransistor, but all they had was a $2 pack of “Assorted Photocells (Cadmium Sulphide)”. I measured one at 1M ohms inside my travel-coffee mug (lid closed), 10k ohm lid open, and 150 ohms for under a desk lamp, in my optics lab. Conveniently, merely hooking it to the Pic 12f509’s GP3 input, with Weak Pullup Enabled, seemed to work great. It pulls the input low in moderate room light.

For the outdoor installation, I’ll move the sensor into our out of a tube or something, to adjust its sensitivity.

Controlling Circuitry

I got a handful of “Aromat TF2E 4.5 Volt Relays” on e-bay, for a couple bucks. These relays need (as you might surmise) 4.5 volts to activate, and, amazingly, only 24mA. A Pic chip can sink that much on an output pin. I’ve heard that coils are hazardous to chips, but so far it seems fine. It may have a built-in diode protection; I noticed that the voltage must be applied in the correct direction for it to click and activate.


If you just put a threshold on the light sensor, it runs the risk of frequently flickering, at twilight. Also, the whole installation would fire up in the daytime if someone cast a shadow for a moment, and shut down at night if one shined a flashlight on it.

<Wikipedia-says> that streetlights use a photocell to modulate a heater which bends a bimetal contact, which stabilizes the sensitivity a bit.

A streetlight has a different materials and power budget profile than mine. </Wikipedia-says>

I came up with a simple, simple algorithm (about 30 pic instructions) that prevents all that, with just time delays. One could do more with an analog-input for the light, I suppose.

As briefly as possible: A counter migrates UP for darkness, and DOWN for light. The installation activates at the halfway point, by merely tracking bit 7 of the counter. Whenever the light changes, bit 7 is replicated across the counter.

But that’s a mind twister. Here’s the algorithm interactively for your ready apprehension.

More to come.

Douglas Jones // Sun 2008.08.10 08:158:15 am

Another approach… I pulled apart a light-controlled outlet with the hope that a simple reversal of the logic would make it turn on duringthe day instead of at night. Inside it wasn’t so simple; they had one sensor feeding two inputs with different resistor values so that one would change level before the other and based on that you know from the timing if it’s a quick shadow or someone messing with it.

I suppose I should try just inverting both inputs to see how it goes. I think pulling a chip off and finding a way to reprogram it is too much effort for what I want.

David Van Brink // Sun 2008.08.10 15:023:02 pm

hmm! Interesting. They could be using those inputs in all sorts of ways, like expecting a gradual darkening… or it might just be a sort of “self calibration” because photoresistors are so imprecise.

Pull it apart and run the experiments!

oh, i dont know. what do you think?


(c) 2003-2011 omino.com / contact poly@omino.com