Build a simple one-chip phototimer

-July 08, 2004

Recently, I needed to automatically switch on a lamp when it became dark and keep it on for a given time. Trying not to reinvent the wheel, I looked through what was available on the market, but I could not find an inexpensive device that satisfied the requirement. Some products worked like a photoswitch, lighting a lamp when it becomes dark and keeping it on while it is dark—in other words, the whole night. Others were designed as timers to turn a load on and off at a given time and had no correlation with darkness. These devices had more functions than I needed, and they were rather expensive. As a result, I had to design the phototimer from scratch, and it turned out to be simple and inexpensive. The phototimer (Figure 1) is based on the low-end, eight-pin flash microcontroller MC68HC908QT2 from Motorola (

When switch S1 is in the Manual position, the microcontroller disconnects from the battery, and the lamp immediately switches on. When this switch is in the Auto position, the microcontroller waits until it becomes dark and, after that, switches the lamp on for a predetermined time that the designer chooses. This project has time settings for one hour and two, four, and six hours. During initialization, the timer sets the one-hour delay as the default. You set the other delays by the pushbutton mode, switch S2. LED0 and LED1 indicate the prevailing mode. After the delay time, the microcontroller switches the lamp off and waits for the next night to automatically repeat the process.

An advantage of the MC68HC908QT2 is that it generates the time delay with its internal oscillator (12.8 MHz with 5% tolerance), meaning that you need not use RC timing circuitry and struggle with component tolerances. I took some additional steps to simplify the design. The microcontroller's PA5 input has an internal, 30-kΩ pullup resistor, so there is no need for an external resistor for the photocell. The LTL-4231T-R1 LEDs from LiteOn ( come with built-in in resistors. You could also eliminate resistor R2, but, in this case, Mode1 would be the start-up default mode. The Teccor ( L2004F31 logic triac needs 3-mA gate current from the microcontroller, and it can deliver 4A load current. Listing 1 is the C program for controlling the phototimer.

You can also modify the timer. You can easily add time-delay modes making software changes plus adding indicating LEDs; available microcontroller pins limit the number of LEDs you can add. For more advanced projects, you can even use a seven-segment display, either directly or via a decoder, for time indication. You can eliminate the Auto/Manual switch by modifying Mode 0, for, example, as a continuous mode to light the lamp just after power-up, without waiting for night. You can use any type of microcontroller in the project. For example, using the 16-pin microcontroller MC68HC908QY2 from the same Motorola family allows you to increase the number of bidirectional I/O lines to 13. Also, instead of the photocell, you could use a different kind of sensor—temperature, pressure, or motion, for example—to activate the time delay.

Check out our Best of Design Ideas section!

Loading comments...

Write a Comment

To comment please Log In