Never to Build
This is an experimental platform for a programmable 120V light switch controlled by an Atmel ATtiny26L processor. It is based on a transformerless power-supply which means this circuit is 120VAC mains powered and still fits within the space of a wall outlet. The circuit design contains interfaces for two input switches, one can be used with a momentary contact push-button, the other can be used with a reed-relay switch. Both switches can be use together. Other expansions are also possible. For example, the circuit includes a zero-cross detector if you want to try your hand at adding dimming functionality, a pot connected to an analog input and several unused I/O ports which potentially could be used for a Two Wire interface to other components.
Before we begin, take a close look at the circuit diagram below. This is the basic building block used for this circuit design. Do you see why your mother would never allow you to build this circuit? Could it be that a 5v chip is directly connected to the house mains? Yes, and so the only thing separating the chip from the mains, is the transformerless power-supply. The design and construction of the power supply is critical to the operation of the circuit. Get it wrong and the best thing that could happen is the whole thing goes up in a puff of smoke, and that’s the best case scenario….
Figure 1 – the basic ‘dangerous circuit’
If you don’t have a healthy fear of this circuit or don’t understand why it’s dangerous, don’t build it! A mistake might kill you. Before proceeding, make sure you read and completely understand this entire article, its contents, and its implications. If you build, modify, or in any way use this circuit, you do so at your own risk.
This is NOT a ‘beginners’ circuit. This is an experimental circuit. Think of it as a box full of razor-sharp edges that could cut you or even kill you. I have tried to cover some of those ‘sharp edges’ with this design, but I make no claim to have covered them all. Plus, anything you do to or with this circuit might turn up more ‘sharp edges’ or uncover ones I had previously covered. Even normal use might uncover additional dangers. I make no claims for having made this circuit even reasonably safe. Use it at your own risk!
You have been warned… Have fun, be responsible, and be safe.
What’s it for?
This circuit came about from my dissatisfaction with both X-10 and Insteon light switch controllers and an interest in transformerless power supplies. Combining the two ideas you get a programmable power switch that fits within a wall outlet. The transformerless power-supply runs the ATtiny26L processor. The processor monitors inputs and controls a TRIAC which turns a light on or off.
Jumping to the end of the story, here’s some examples of what I did with the AVR Switch;
In the picture on the below, you can see in my pantry, I have added lights that automatically turn on/off with the door open/close. There’s also a timeout feature so that if the door is left open, the light will turn off automatically.
I installed an outlet box on the inside wall, over the door. The left half of the double box holds the AVR Switch. The outlets in the right half of the box go to a string of small ‘under cabinet’ lights which are mounted on the wall, one light for each shelf.
A security alarm type reed-relay switch is embedded in the door frame directly below the outlet box and triggers the AVR switch.
Here’s another example. It’s a simple timed light switch. In the photo on the left, the AVR Switch is located under the left side of the wall plate. It has a push-button and a small LED night light. Pressing the push-button turns the light on. Pressing the push-button again, turns the light off. If the light is left on for more than 45 minutes, the light is automatically turned off.
Here’s what the whole thing looks like on the inside. It’s very small and compact. The AVR processor is near the top. The transformerless power-supply components are near the bottom. The trailing wires go to a push-button and a reed relay switch (yes, you can use both at the same time).
Mains-Powered Transformerless Power-supply
Remember, this is a dangerous circuit. You should not proceed with design and construction unless you are knowledgeable enough to accept full responsibility for your safety and the safety of others.
There are several good resources for transformerless power-supply design on the web. The best I found is Microchip Application Note AN954. Use it to determine the required component sizes. Be sure to read the section on Safety Considerations at the end of the application note. Also, make special note of the warning to use an isolation transformer when working on this circuit!!
Several things you should note about this circuit design are:
- The 5VDC output is connected directly to the mains ‘HOT’ line. This means that the circuit ground floats 5 volts below HOT and is not referenced to earth ground! Remember this if decide to try to measure voltages in this circuit.
- The circuit design can only supply a very limited amount of current, so you won’t be able to run much off this circuit. If you try to increase the output current, you will quickly find that you’re limited by the size of the X2 rated capacitor.
- The zero-crossing signal is phase-shifted from the actual zero crossing. If you plan to use the zero crossing to control the TRIAC turn-on point, you will need to take this delay into account.
The end result in my case was the basic circuit shown in Figure 4, shown below.
Figure 4 – a basic
“capacitive transformerless power-supply”
Making an AVR Power Switch
Quickly putting the pieces together for a platform to experiment with yields the resulting circuit shown below in Figure 12. There are two inputs, a zero-crossing detector, a pot for an analog input, an indicator LED, a Two Wire interface (net yet untested but could be used as 2 additional I/O pins), and of course the TRIAC to control the power.
the basic ATtiny26L controlled light switch
On the topic of TRIACs, make sure you use one with an isolated gate. Depending on how large the load is that you are controlling
you’ll probably need to use a heat-sink on the TRIAC. Without the isolated gate, your heat-sink would be at an elevated voltage potential and so you would not want someone accidentally touching the heat-sink.
Because I used a plastic box with a metal top for an enclosure, I had a readily available heat-sink. The metal top does make a decent heat-sink, but again, only if you use an isolate gate TRIAC!!
A word of caution… If you connect any external device to this circuit, remember that added device will be at 120VAC mains potential! For example, if you add an external push-button, the wiring and most likely any metal switch parts will also be at mains potential. Keep any wires inside the wall where no one can accidentally touch them! I’ve included current limiting resistors on the inputs, but they just try to limit the shock someone will receive if they come in contact with the switch or wires. This does not eliminate the shock, only slightly reduce it. Carefully analyze any switch you use to make sure there are is no exposed metal and no possibility of exposed metal if the switch fails. Your life may depend on that analysis!
Now that we’re got an experimental platform, we need some software. Here’s a sample I wrote in WinAVR C. It is a relatively simple program which can get you started. You can download it here.
The sample program makes use of the two inputs as switch inputs. PA0 isused for a simple on/off switch (used with a reed relay in my pantry example above) and PA1 is used as a push-button switch (used in my timed light switch example above). Yes, you can use both and the same time. In fact, if you don’t use the PA0 switch make sure to jumper it closed otherwise the sample code will assume the switch is open and the light should be on.
Another feature of the sample program is using the analog pot to adjust the maximum ‘on’ time of the switch. Rotating the pot adjusts the maximum on time from 2 minutes up to 60 minutes. The final function in the sample program is utilizing the LED as a night light. That pretty much wraps up the functionality of the sample code.
A few notes about the sample code itself…
- The zero crossing signal from the transformerless power-supply drives the INT0 hardware interrupt which runs the whole show. Everything in the sample code is interrupt driven, debouncing the switches, turning on the LED and TRIAC, everything. Without the INT0 interrupt, the code will not work.
- Timer1 inserts a delay from the INT0 interrupt to the actual point where the TRIAC needs to turn on. This accounts for the phase shift inherent in the transformerless power-supply. If you want to implement dimming, you could do that by adjusting the turn-on delay here. Just be aware that triggering the TRIAC at a location other than the zero point may cause additional EMI resulting in the need to filter the output line (something currently not included in the circuit).
- Timer1 also controls how long processor drives the TRIAC gate. You only need to drive the gate long enough to reach the latching current of theTRIAC. At that point you can release the gate and the TRIAC will stay on. This somewhat reduces the amount of power the overall circuit consumes. It’s also a necessity if you want to implement dimming.
I have circuit boards and parts kits available. However, you’ll have to assemble everything yourself and more importantly, determine if this circuit and its components are safe and appropriate for your use. As previously stated, this is an experimental platform and I make no guarantees of suitability or usability. You must make judge that for yourself. If you feel you are not knowledgeable enough to make this decision, then do NOT build, use, or modify this circuit.
I hope you find this information as useful and as interesting as I have.
For information on boards and parts kits: Order
Note: Your safety is your own responsibility. This includes proper use of equipment and safety gear, and determining whether you have the necessary skill and experience. Power tools, electricity, and other resources used in this project are dangerous, unless used properly and with adequate precautions, including safety gear. Some illustrative photos do not depict safety precautions or equipment in order to show the project steps more clearly.
This project is not intended for use by children. Use of the instructions and suggestions listed here is at your own risk. Tom Struzik / Sixerdoodle, disclaims all responsibility for any resulting damage, injury, or expense. It is your responsibility to make sure that your activities comply with applicable laws and codes.