Night Light Saver V6

Wichit Sirichote,

Latest design of night light saver circuit built with AT89C2051. Nice firmware using c coding developed with SDCC.

This new version has internal Ni-MH battery backup, reset button and simple time setting. Now the circuit board was embedded with lamp fixture. Preset turn on period is from 18:00 to 22:00 everyday. You may let the saver turn on whenever you want. The period of time on can be reprogrammed with sdcc. Howvere this project is not recommended for beginner. The circuit uses transformerless supply and no galvanic isolation!

Figure 1: Embedded saver with lamp fixture








Hardware Description

Figure 1 shows complete hardware schematic of the Night Light Saver V6.0. The AC line was protected F1, a 1A fuse. Any short circuit caused by saver's components will blow the fuse. R1 and C1 limit current to the +5V zener diode, D3. R2 discharges capacitor C1 when power terminal of the circuit was opened. The super capacitor C2,+5V 0.01F filters DC supply. D4 acts as unidirectional switch for current supplied to MCU. BT1 is +3V 60mAH Ni-MH battery for backup the MCU when main power failed. The charging current is approx. 2mA with AC main lives. When main power failed, BT1 supplies approx. 4mA to the MCU. With fully charged, it could be able to provide backup time approx. 15Hrs. The MCU runs with +Vbackup. SW1 helps reset the MCU, in case of brownout voltage by BT1. C4 and internal pull-down resistor forms simple reset circuit. The MCU, 89C2051 runs with 3.579MHz Xtal. SW2 is for clock setting, when pressed, time will be 18:00. P3.7 drives tick LED with small sink current. R5, 4.7k limits less than one mA for D5. P3.0 provides 5Hz clock signal for calibration. The output bit is P1.6. It drives PNP transistor, Q1. R3 limits base current. R5 pull base pin to +V when P1.6 is logic '1' to fully turn off Q1. R4 limits DC current injected to Q2, MAC97 small triac. LP1 is incandescent 25W lamp. The lamp's wattage can be up to 40W.

Figure 2: Complete schematic of Night Light Saver V6.0

I built the board with universal pcb and put it under the lamp fixture. SW1 and SW2 are small tact switch. To press them I need a small rod made by bamboo.

Figure 3: Bottom view with Ni-MH battery


The source program is beautiful coding with c language. After timer initialization then idle mode is entered by setting bit0 in PCON register. The timer0 overflows 10 times per second. Tasks for update clock, check SW2, blink LED and update output bit will be executed every 1/10s. The task that controls output instead, will be executed every one second. For different preset time, time on, time off, you can edit the source code and recompile with sdcc easily. I provide sample scheduler scanning function, scan_pgm( ). If you want to modify the code for all 8-bit output on P1 similar to the Miniature Real-time Controller. You may have open collector driver, the same hardware and modify a little bit of the source code.

night light saver V6.0, 89C2051 runs with 3.579MHz xtal
The source code was compiled with sdcc.
 Copyright (C) 2004 Wichit Sirichote,, 20 December 2004
 This program is free software; you can redistribute it and/or modify it
           under the terms of the GNU General Public License as published by the
           Free Software Foundation; either version 2, or (at your option) any
           later version.
 This program is distributed in the hope that it will be useful,
           but WITHOUT ANY WARRANTY; without even the implied warranty of
           GNU General Public License for more details.
#include <at89x051.h>
#define tick_test P3_0 // 10Hz/2 output for clock calibration
#define LED P3_7
#define output1 P1_6
char status;
int count, i;
char tick, flag1, temp, timer1, timer2;
char hour,min,sec,sec10;
// you may press set time button whenever you want the lamp turns on
           // for winter season you may press ealier than local time
           // for summer more late, time will shift relatively!
           // period will be 22:00 - 18:00 = 4 hours
code char time_on1[] = {18,00}; 
code char time_off1[] = {22,00};
// another scheduler, the 3rd byte will write to P1
           // you may replace control_output function by scan_pgm function
code char pgm1[]={18,00,0x80,19,00,0,20,0x80,1,21,00,0};
char _sdcc_external_startup()
           return 1; // skip static and global variables initialization
           char i;
           for(i=0; i<4; i++)
           if(hour == pgm1[i*3] && min == pgm1[i*3+1])
           P1 = ~pgm1[i*3+2];
           if(hour == time_on1[0] && min == time_on1[1])
           status = 0;
           if(hour == time_off1[0] && min == time_off1[1])
           status = 1;
           flag1 &= ~1; // clear flag1.0
           LED = 1; // turns off led
void time ()
           /* update real-time clock */
           if (++sec10 >= 10) /* 100ms * 10 = 1 s */
           LED = 0; 
           control_output(); // control output every second
           sec10 = 0;
           flag1 |=1;
           if (++sec >= 60)
           {sec = 0;
           if (++min >= 60)
           {min = 0;
           if (++hour >= 24)
           {hour = 0;
           status = 0;
           hour = 18;
           min = 0;
           sec = 0;
           if(status) output1 = 1;
           else output1 =0; // update output bit 
void timer_isr(void) interrupt 1 using 1
           TH0 = 0x8b;
           TL0 = 0x7f; // reload with 35711 for 10Hz
           tick_test ^= 1; // test tick for 10Hz/2 or 5Hz
           TMOD = 0x01; // timer1 and timer0 = mode 1
           EA = ET0 = TR0 = 1; // enable timer0 interrupt, start timer 
           PCON |= 1; // enable IDLE mode, to extend backup period



Figure 4 shows start time at 18:00 when SW2 was pressed. The lamp will turn on until 22:00 everyday. Since the preset time, 18:00 can set whenever SW2 was pressed, so if you want to change time on, for examples in summer season, sun will set lately, suppose you want the lamp to be turned on at 19:00, you just press SW2 at 19:00. The day after it will turn on at 19:00, however time off will shift to 23:00.

Figure 4: Preset time on for winter

Figure 5 shows a complete lamp fixture with cover and 25W lamp. I have been tested the energy saving lamp, 5W Sylvania, it also works fine.

Figure 5: with 25W lamp and cover



Schematic saver6.pdf
Firmware lamp.c
C compiler
Orcad 9.1 schematic file NA


1. Nice pictures were taken by Kodak EasyShare CX7430. The picture files were adjusted size by CompuPic Pro.
2. Nice drawing Figure 4 was drawn with openoffice draw program.

27 December 2004

recovered 18 December 2015