An Improved Nightlight




Image of night lightMy daughter loves to sleep with a nightlight, as many kids do, but they tend to eat batteries fast. This is especially true with incandescent bulbs – which only last a single night if you’re lucky. Her nightlight was one of those tap-lights with a girlish Tinker Bell graphic. I’ve tried plugin style of lights, but she likes to keep the light with her so those didn’t work. I began thinking of ways to modify the light so that it would automatically turn off after a set time, thus saving the batteries. At first, I thought about designing a simple transistor based circuit to keep the bulb lit for a few minutes, then turning off. This would have worked, but then I thought, why not just add a dimming feature so the light slowly dims until it shuts off. I began building and simulating circuits until I found one that would work. I eventually came to the conclusion that a traditional dimming circuit wouldn’t work since whatever power the bulb doesn’t use must be dissipated by the regulating transistor. This would translate into a lot of wasted heat. I then started experimenting with LED solutions to give the same brightness and color but they just weren’t as warm as an incandescent bulb.

My next idea was to use a microcontroller to provide a fading PWM output, which could be used to drive a switching transistor and thus the bulb. Since the transistor would be driven to saturation, there would be minimal power loss and therefore a much more efficient design.



I first began by cracking the light open so see what I had to work with: Very few parts. Just a molded battery compartment, a switch, a light bulb and some wire.

I set out to breadboard my design first, since I was bound to make several changes until I liked what I had. The only microcontroller that I had on hand was an Atmel tiny85, which I had been using on several other projects. It is a little overkill for this nightlight as it has 8k of program storage but rather than order something else and wait for shipping, I just used what I had. The circuit is pretty simple, with just the microcontroller, a transistor and a few passive components. I decided to add a capacitor across the power input of the microcontroller as I was getting a few glitches from what I’m assuming were the batteries losing contact for a split second while my daughter was playing with it. This is my final design:

Schematic of night light controllerTo program the microcontroller, I used an AVR Dragon, which has a few nice features, such as debugWIRE, ICP and HV programming capabilities. The debugWIRE interface made loading new code much easier as I didn’t have to deal with the ICP pins being used for the PWM output. It just uses one data line (RESET), as well and a connection to ground and the reference Vcc of the circuit. There were a few times that I had locked up the microcontroller by the watchdog timer, in which case I had to use the HV programmer.

The program is extremely simple as it spends most of its time sleeping, and as a result the overall power draw is reduced. At power up, the program goes through an initialization sequence which turns off internal components that aren’t needed and then configures the timers (Timer0 and Timer1) as well as the I/O pins. During initialization, Timer0 is implemented as PWM with the output being fed to one of the pins (more on that later). Timer1 is set to slowly count up until the timer overflows (approximately 30 seconds), at which point an interrupt is called and the counter resumes. Every time the Timer1 interrupt is executed, the PWM duty cycle is reduced by one. This produces a gradual dimming effect on the bulb. The main body consists of a loop that continues until the PWM duty cycle drops to a specified limit, at which point the PWM output is stopped and the controller enters a deep sleep mode where virtually everything is shutdown.

So with no user input, the bulb turns on (when batteries are first installed) and slowly dims over a course of 10 minutes until the light shuts off. There is one button that allows a push on-push off function. This is done through an interrupt-based system, as when the microcontroller enters the deep sleep, only an external interrupt will wake it. INT0 serves this purpose and when held low, wakes the controller and enters the interrupt service routine. The interrupt routine first checks if the light is either on or off. If the light is on, then it simply disables the PWM output and puts the microcontroller back to sleep. If the light is already off, then it issues a reset, which will turn the light back on. The reset is done through the use of the watchdog timer. It is first enabled for a short reset interval then the program enters an infinite loop, which waits for the watchdog timer to issue the reset.

My next difficulty was to find a new switch to replace the SPDT switch with a momentary one. I wanted to utilize the same mounting spot since this was also used hold the outer lens in place. I came up empty handed so I decided to convert the old switch into a momentary one – if that was even possible. Luckily for me, it was. I simply had to dismantle the switch (there were two clips on either side that held it together) and remove the piece of metal that held it in either position. Viola – now it’s a momentary switch!



I was impressed with the results as I achieved a 43% power reduction and 24% less current usage during maximum power use (maximum duty cycle). That is, until I remembered that I’m using one less battery so the actual power used is about the same (a little more thanks to the microcontroller). The real improvement comes as the light starts dimming and the power used begins to decrease. When sleeping, nearly no power is being used – which is a huge improvement!


Voltage Current Power Current reduction Power reduction
Before Modifications 6.38v 514mA 3.28W
After modification 4.79 393mA 1.88W 23.5% 42.7%
Sleeping 4.79v 170uA 814uA 99.9% 99.9%



Oscilloscope capture of maximum duty cycle


The image to the left shows the maximum duty cycle of the light (first starting)




Oscilloscope capture of minimum duty cycle


This next image above shows the minimum duty cycle of the light (just before turning off)





I ended up constructing the final product on a small piece of prototyping board that I had left over from a previous project.

Everything fit very well with still a lot of space to spare. The original design of the light was for 4xAA batteries,

Completed prototype board

which produce a little over 6V on fresh batteries – well above

the ‘safe limits’ of the microcontroller. Instead, I decided to utilize one of the unused taps on the

effectively uses just 3xAA batteries at 4.5V – well within the limits. This still produces more than enough light with only three of the four batteries.battery compartment, which

This image shows a close-up of the completed circuit (note – the capacitor was not yet added)


Everything wired up


The final design with everything positioned correctly. I used a lot of hot glue to hold the wires and completed board in place (very handy).






Completed night light with light on






Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>