Wednesday, 4 July 2012

Building The Best Night Light Ever

So I saw an LED moon night light thing on firstin.co.nz for like $10, and immediately imagined hacking it with RGB LEDs and whatever else because it seemed such a waste of potential for it to only do what it was supposed to. Cycling through the phases every 10 minutes is all very well, but pulsing blue or red would look so much cooler, specially because the plastic faceplate would make anything look swish. It also refuses to turn on unless it is dark enough, but it does turn on automatically when the lights turn off.

Some of the ideas I had for it were:
  • do selectable/changing colours
  • do different modes e.g. pulsing, phases
  • intensity setting via a potentiometer to save power/crank it bright during the day

The Insides

After a week or so of planning it out in my head, I took it to school to dismantle and see how the insides looked. Inside the unit was a light dependent resistor (LDR) to detect when to turn on, a small micro that directly powers the LEDs via a ribbon cable, and an IR receiver unit with inbuilt processing to give a logic level switching output. A datasheet was found for this IR unit but it was in chinese so not much help.


Choosing a Brain

I had a few chips lying around so I figured that would be easier than getting some more. An ATTiny44 seemed like a good option as this can be programmed via Arduino, but the 44 has 4 KB of memory and an empty Ardunio sketch is more like 4.5 kB. This also counted out the ATMega48 I had as the 4 means 4 kB, so in the end an ATMega328 was chosen as this has 32 kB, and was already in the Arduino board I intended to prototype with so I could program it, test it then take it out and drop it into the final system.

Programming

The first step was to work out whether I could use the remote or not. The output of the remote receiver could not be connected to a serial port directly, but a library for Ardunio was found which received commands in the background and read them off as a hexadecimal value e.g. "FF40BF", making it simple to decode.

The rest of the "draft" coding was done on an Ardunio board with a potentiometer, the IR sensor and one LED (with FETs) attached. This made it easier to test timings and make sure the remote commands and stuff were working properly without the system being together.


Building the Circuit

Designing the circuit was a bit more complex than anticipated because I had to be able to control the colours of the RGB LEDs, as well as switching the phases on and off independently. In the end, it seemed too complicated to control the colours for each LED independently as that would require 3 switches per LED, and there aren't many cheap chips with 18 separate pulse width modulation (PWM) outputs.

The LEDs are common cathode (ground pin), meaning that the switching has to take place at the top rail. Trying to switch NMOS would not work well as it would not be fully on, so PMOS FETs were used with ON being 0 volts on the gate. Switching each LED on separately was done at the bottom with an NMOS for the same reason, and resistor values for the LEDs were calculated based on a datasheet I found. These ended up being 180R for red, and 100R for green and blue as these have a higher forward voltage drop.


With the design done, this side of the system was constructed on the back side of the moon. The LEDs were hot glued to a sheet of paper first to secure the legs apart so they would not touch and short-circuit, and the two-pin holes in the moon plastic were drilled out to make room for the 4 leads on these LEDs. They were then placed into the holes left by the original LEDs and hot glued in place, before having their resistors, power leads and their individual FETs (not shown) soldered on.



On the microcontroller side, first more sturdy wires were soldered to the battery supply so that they would handle the higher drain of these LEDs. Then a piece of proto-board got a DIP-28 socket for the ATMega chip, a 16 MHz crystal and some PMOS FETs to switch the colours. A potentiometer and the IR receiver were also attached. The original ribbon cable was repurposed to supply the gates of the phase switches, and the whole thing was assembled. A hole was drilled in the back of the plastic for the pot to stick out of, giving a nice knob to turn for brightness setting.


At this point, leads were run from the Arduino board into the socket in order to test whether it worked without exploding before committing to putting the chip in there.


With that working successfully, the chip was dropped into the board and the system was tried again...and didn't work. Several times. After some head scratching, decoupling capacitors were added on the chip power supply and it worked perfectly. 

Additions

The only problem remaining was that the thing had to be completely dissembled in order to return the ATMega to the Arduino board for reprogramming. Luckily, using the ATMega means it has the Arduino bootloader running and can therefore be programmed easily via serial. A connector was added to the outside  of the moon with Tx, Rx and a ground (to tie the floating battery supply to the computer). A reset button was also added to the circuit with a pull up resistor to +V and the button was hot glued to the inside of the case, poking out one of the existing holes. This allows the system to be programmed via a USB serial converter easily.


The final design for the proto-board looked something like this, with the off-board wires left long so that it could be taken out and have things added without having to disconnect everything else. Haven't put all the pins in or stuff in the right place or really done a good job at all, but you can work it out.


Final Moon

With everything in and working, its definitely better than it was before. It does colours, it does different things, it also goes to sleep to save power after 10 minutes or when told to.

Power use was measured for different uses to show how much power it used, and measured againsst an AA battery's supposed 2000mAh capacity.
Full on all LEDs with white used 114 mA, which means 16.7 hours of constant use.
Low on all used 18mA which gives >100 hours of use.
In sleep mode everything is turned off and the micro is shut right down. It uses 1.7mA and 1.5mA is used by the IR remote sensor, so thats >1000 hours of use. Not bad.

Here is the final sketch MoonCode.ino

Still working on adding more stuff for it to do, but here it is at the moment going through pulse, spin, phases, strobe and multicolour strobe (which both look bad on cellphone camera).




1 comment:

  1. mean haha "as seen on hackaday"

    http://hackaday.com/2012/07/06/taking-a-moon-light-from-grayscale-to-full-color/

    ReplyDelete