When I go traveling, I like to leave a timer-controlled lamp in my living room to deter burglars. But where I live, the sun sets two hours later in midsummer than midwinter, and I have to remember to adjust the timer with the seasons. Even then, what about stormy nights when the light fades early?
I needed a way to turn on a lamp when outside light levels dim, but off at a fixed time every night when I would normally go to sleep. The “on” function should be controlled by light, the “off” function by a clock. I couldn’t buy a gadget to do this, so I built my own.
A Phototransistor and a Comparator
Suppose you wire a phototransistor as in Figure A, and point it at the sky. If you recall my key-card door lock from MAKE Volume 33, you’ll know that the voltage at point X will gradually fall as the sun sets.
We use a comparator, symbolized in Figure B, to convert this gradual change into a sharp, clean signal to turn on a lamp. The + and – signs on the input pins do not mean to apply positive and negative voltage. Rather, both pins accept positive voltage, and if the + input rises higher than the – input, the comparator output flips from low to high — and vice versa.
The LM339 is a cheap, reliable chip containing 4 comparators (Figure C), each built around an NPN transistor with “open collector” output. (Some comparators use CMOS transistors with an “open drain” output, but the principle is the same.) To get a positive signal, you attach a “pull-up” resistor, as in Figure D.
A “high” output from the comparator is created when the transistor inside it blocks current from the pull-up resistor and diverts it to other components. A “low” output is when the transistor sinks current from the pull-up resistor and takes it from other components.
If we connect the output to a 555 timer wired in monostable mode, the transition will make it emit a single high pulse, which can be amplified by a transistor to activate a latching relay, which can turn on a lamp. (For more information about relays and other components, please see my Encyclopedia of Electronic Components, Volume 1.)
The circuit is powered by an AC adapter, with an alkaline battery backup (Figure E). In a blackout, the battery will run the circuit for at least 24 hours. In normal use, an AC adapter rated 10VDC or more will prevent the battery from discharging. Be sure it’s an alkaline battery, not rechargeable. Don’t omit the fuse.
Clocking the Lamp
Now we can switch the lamp on. What about off? The easiest way is to modify a cheap battery-powered digital alarm clock. If you can’t find a Sharp SPC500J, just make sure to get one that runs off two 1.5V batteries.
Put in the batteries, open the case, and set the alarm one minute ahead. Press your multimeter’s black probe against the negative end of the battery pair, and when the alarm starts, test voltage into and out of the beeper with the red probe. Set your meter to AC volts, since most clocks use a stream of pulses to create an audio signal. Now silence the alarm, and check for DC volts.
In my clock, the beeper has a positive DC voltage on both sides when it is off, because it is wired as in Figure F. A transistor triggers the beeper by grounding it, pulling down the voltage on one side. (I have shown the transistor separately, but it’s probably built into the chip that runs the clock.) The voltage between the beeper and transistor is normally high; when the alarm goes off, it goes low.
If we tap into the circuit, as in Figure G, the voltage transition can activate the 555 timer we use to switch off the relay. Attach an output wire to any point in the alarm wiring where the voltage fluctuates above and below 2V. In some clocks, the beeper may go low-high instead of high-low, but that’s OK. The circuit will accept low-high-low or high-low-high, steady or pulsed current, AC or DC, just so long as the high is above, and the low below, 2V. If you can’t get that range, substitute a resistor for the beeper (or LED), as shown. Start with 1K and increase as needed.
My clock also illuminates an LED for 5 seconds when the alarm sounds. I decided to use that as my alarm signal instead of the beeper, but the LED didn’t create the necessary voltage drop, so I substituted a 100K resistor (Figure H).
Figure I shows an overview of the circuit, and Figure J the schematic. The clock signal passes through resistor R5 to a second LM339 comparator, triggering a second 555, powering the relay’s “off” coil via transistor Q2.
For long-term use, the clock is powered from the switcher circuit with an LM7833 voltage controller. Remove the batteries, solder wires to the battery carrier contacts, and run them out through a hole drilled in the case, along with the alarm output wire. Set the reference voltage on the second comparator to 2V using potentiometer P2, and test it with a meter. Now, when the alarm goes off, it should trigger the second 555 timer.
Capacitors C1 and C2 isolate the timers from DC voltage while allowing transitions to pass. Resistors R7 and R8 hold the timer inputs high until a transition occurs. R3 provides positive feedback from the first comparator’s output to its input to reinforce the transition and make it fast and clean.
Figure K shows the relay pinouts from above. The transistors powering the coils will cause a slight voltage drop, so I’ve specified a 3V relay rated for pulses up to 4.5V. Test its outputs with 2 LEDs. Once you’ve got it working on a breadboard, solder the relay onto a perf board, and check that the joints are secure. Cover them with liquid insulation, and absolutely do not omit the 1A fuse. Use an incandescent bulb no brighter than 60W.
To protect your phototransistor, point it through a window without any direct sun. At sunset, set a very low reference voltage with potentiometer P1, and gradually increase until the lamp turns on. (To test this function, use any bright light that dims gradually.) Now trigger the alarm. The lamp should turn off.
NOTE: If you upgrade to house current to switch a lamp, be careful! Never run house current to a breadboard!
Why Not Use a Microcontroller?
You’d still need 2 transistors to power the relay and an external clock for accurate long-term timekeeping. You wouldn’t need a comparator, because the microcontroller could directly process the alarm signal and phototransistor input. You’d have to guess at the software value that corresponds to a “sunset” state, download the program, test it, edit it to adjust the value, etc. Personally, I think twiddling a potentiometer is easier.