I started this endeavor with wanting to build a light controller that can be set to turn on a string of Christmas lights for a specific time before the lights finally dim and turn off. This was going to be used for my kids as they have colored Christmas lights strung up around their room as a night light. I wanted to make something that would be efficient (in terms of power used) so I didn’t want to use a bulky transformer to supply the power. This would also reduce the overall size of the controller and allow it to fit nicely in a small box.
Since I designed this around a transformerless power supply, I needed to make sure that the user would not be able to come in contact with the mains power. This is extremely important, since it will be used around kids. For this, I decided to use an IR remote control receiver. I chose not to use a specific protocol for the IR remote so that any remote control can be used, instead of trying to find the correct one every time I wanted to turn the lights on. This works great for me where no other IR devices are used around the controller.
The hardware consists of a power supply, Atmel tiny85 microcontroller, a triac and an IR receiver module. For the power supply, I used a capacitor drop method, followed by a linear voltage regulator. This produced a nice clean output voltage to make the rest of the electronics happy.
The input capacitor produces a reactance based on the input frequency (60Hz), which limits the current through the rest of the circuit. This, in conjunction with the zener diode, limits the voltage to about 11v, before being half-wave rectified by the second diode. This charges a filter capacitor and provides a fairly clean output under minimal current. It is then regulated further, down to 5v, with the help of a linear voltage regulator, and again filtered with a second capacitor. At this point, the power supply remains stable at moderate current usage for brief periods. The down side to this type of power supply is that the current available greatly depends on the size of the input and filter capacitors. I chose this method to simplify driving the triac later on.
The IR receiver I used was a leftover extension module for a cable box that found its way into my junk box. It is basically a generic IR eye, which outputs a low on the data line when a 38KHz carrier frequency is detected and a high the rest of the time. I found that it would often output a random low now and then. Under normal conditions, this wouldn’t be a problem since software would be looking for a specific protocol and weed out the random noise. So to fix that, I used a simple low-pass filter that rejects anything under a few milliseconds in length. This works great for most remote controls, however some remotes have buttons that wont work – the data signal doesn’t contain enough low pulses and thus get filtered out. In future versions, I may just decide on a protocol and remote type to prevent accidentally turn it on.
To control the output I used a triac to switch the load on or off. The triac has several benefits over a relay, most notably with being able to “dim” the output. This is accomplished by turning the triac on at a specific angle, but more on that in a second. A triac is a switching device capable of switching AC current. Triacs have the benefit of remaining on after they have been triggered provided that the trigger current was above the minimum and the load current is above the threshold. This is great since a small current is needed to ‘trigger’ the device, then it stays on indefinitely; but how do you turn it off? Well, since AC switches direction 120 times a second (60 cycles here in the US, where the direction changes twice each cycle) there are periods where no current flows through the load and thus the triac, so there is no longer enough current to maintain the ‘on’ state and it turns off. This means that the triac must be triggered at a rate of twice the AC mains frequency every time (for the load to be energized). That might seem like a lot of work for something that behaves like a relay, but there is one benefit here – dimming. If we wait for a specific time, or say angle, before we trigger the triac then the load will be getting less average power and in the case of an incandescent bulb, appear to dim. This is key – this method only works on incandescent bulbs! Fluorescent and LED lighting requires a different method or they’ll flicker horribly (if they even work).
The last part to talk about is the zero crossing detector. This is the crucial part of the circuit that tells the microcontroller when the AC is at 0v (remember its changing direction 120 times a second, so there are going to be periods of zero volts at every direction change). This allows the microcontroller to synchronize the triggering of the triac with the state of the AC mains. I’ll talk more about how it works later but hardware-wise, it is very simple. The zero crossing detector consists of just a resistor connecting the microcontroller to the hot side of the AC mains. You may be wondering what I’m thinking by connecting the microcontroller to 120V, well, there is actually more to it than that. Inside the microcontroller are diodes connected to every pin. These diodes are called clamping diodes and they tie each pin to both the Vcc and Gnd of the microcontroller. Their purpose is to prevent the input to the pin from going above Vcc or below Gnd. So backing up to the circuit, the microcontroller is connected to the mains by a resistor, but the internal clamping diodes prevent the voltage from going beyond the safe levels of the input pin. Every time the AC is high, the input to the pin is just above 5v and when the AC is low, the input is just below 0v.
The software to drive all this hardware is entirely interrupt based. In fact the only code in the main body is there to handle when the microcontroller wakes up and put it back to sleep after the interrupt has been serviced. The software uses two timers to control the dimming and on time (each with its own interrupt routine) and two external interrupt routines. The zero crossing interrupt is configured to trigger on a pin state change (from high to low or low to high). This interrupt just starts the timer for the triac trigger, does a little housekeeping for the fade mode (if enabled) and returns. The second external interrupt is for the IR receiver. Since I designed this for use with virtually any remote, it doesn’t do any IR code checking and rather makes sure the button press was debounced before setting the second timer for the desired on time. The interrupt for the first timer (controlling the triac trigger) simply enables the gate, delays for a few microseconds, then turns the gate off. The datasheet for the triac lists the minimum time the gate must be triggered before it begins latching as a few nanoseconds, however experimenting with this number, I found that their numbers were way off. The second timer interrupt is triggered every 4 seconds (give or take) and it simply decrements another counter (which determines the on time) and if the device is in dimming mode, then it lengthens the time for the triac-trigger-interrupt-timer which lengthens the time between the zero crossing and when the triac is fired (creating the dimming effect). If this is confusing then have a look at the code which may help you understand it better.
The light controller functions as follows: when it’s first plugged in, it remains off. When you press a button on any remote, the lights turn on and the timer is set for 2 hours. It now enters “setup mode” for 3 seconds. Within this mode, any further button presses adds 2 hours to the timer (up to 6 hours). After 4 button presses, the timer resets to 2 hours and enters a “fading mode” where the lights fade on then off over a course of about seconds and repeats. Any further button presses after “fading mode”, or after the “setup mode” elapses will turn the lights off.
I was going to just buy a small project enclosure box and stuff everything inside, but I had trouble finding anything locally that would fit. Instead of buying a box that was 4 times too big, I decided to put my 3d printer to good use and designed one myself. The case itself was made as two parts with a lip that allows both halves to be glued together. I made an opening for the power cord and IR receiver on top, the switched outlet on the bottom, and the status LEDs on the front. Overall, everything fit very well considering that I threw the enclosure design together in less than an hour.
As you may be thinking right now, connecting a circuit directly to AC power is a dangerous and bad I idea. Yes, you may be right. If you are careful and smart about it however, then it can be quite safe. First, I want to say that you must use extreme caution when working with live power! I can not be held responsible for any damage or injury that is caused by following any part of my circuit. I included a fuse in my circuit as protection just incase something goes wrong. Its only 100mA and only protects the circuit and not the load. You should also use an x class capacitor for the input in the capacitor drop supply portion. The reason is because this x class of capacitor is designed for connecting directly to AC power and when in fails, it will fail in an open state (which disconnects the circuit). Other types of capacitors are not guaranteed to fail in an open state and would send full current (and voltage) through the circuit and cause some serious damage (which was a reason why I added the fuse).