Description

Circuits Your Mother Told You
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 in Figure 1. This is the basic building block used below 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. Order parts and kits here.


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 right, 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 wallplate. 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.


Figure 4 – a basic
“capacitive transformerless power-supply”

Making an AVR Power Switch

Figure 12
the basic ATtiny26L controlled light switch
Component List
Quickly putting the pieces together for a platform to experiment with yields the resulting circuit shown 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.

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!

Software

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 is used 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 the TRIAC. 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.

Next steps

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.


Construction


Construction is relatively simple. I suggest installing the parts onto the circuit board working from the middle outward and starting first with the components that lay flat, then ending with the upright components. One construction hint, before you begin populating the board with components, mark where the 3 power leads need to exit the plastic box by passing a heated nail or pin thru the 3 wire lead holes and into the plastic.

The closest picture on the right shows where all the components are located. The farthest right picture shows an assembled board. Note that some of the components are optional depending on what functions you want to implement:

  • The LED and R12 (which drives the LED) are optional if you don't need any indicator functionality. This option is not present in the photo on the right.
  • R14 and R15 are for two-wire mode USI interface, but may not be necessary, Fair warning, I haven't tried this functionality yet. This option is also not present in the photo on the right.
  • If you are using the sample software, and you are not using the reed-relay input, you must jumper the reed relay pins together. The jumper is included in top right corner of the board photo.

Note that there is no in-circuit programming capability on this board. For obvious reasons you don't want to mix 120VAC and your AVR programmer. Please remove the ATtiny26L and re-program it outside of the 120VAC environment.



Component List

The wires to connect your AVR Switch to your 120VAC circuit are not included in the kit. Make sure you select wires long enough and appropriately rated for your particular installation. The wire pad under N-Fuse is for the Neutral wire and should probably be a White wire. The wire pad under the H-Fuse is for the incoming Hot wire. I suggest using Black for this and the wire pad to the right of H-Fuse and beneath the TRIAC is for the switched Hot. I suggest using Red for this wire, but follow your own local codes and regulations for wire color coding.

Once you have the 120VAC wires connected and the rest of your circuit assembled, double check all your connections, look for shorts and any other construction errors that may have occurred. Then with the ATtiny26L chip removed, power up your board by connecting the neutral (white) and hot (black) wires to your isolation transformer. At this point if things are seriously wrong, you'll pop one or both of the fuses.

Once you've successfully passed the smoke test, carefully measure the voltages between the various pins in the 20 pin socket. (remember the isolation transformer!) The highest pin to pin voltage you should find in 5Vdc. You also shouldn't read anything less than -5Vdc. If you read something larger, go back and re-check your circuit. Now, disconnect the power, give it some time to discharge the capacitors, then insert the ATtiny26L chip. Reconnect the power. If you're using the sample code, then with the reed relay pins shorted, the LED should be 'on'. Unshorted, the LED should be 'off'. If that all passes, you should be ready to try out your AVR Switch.

Note that if you need to ask why you need the isolation transformer for the above steps, then you probably aught not be building this circuit!

If you're using the AVR Switch to replace an existing light switch, a typical installation would connect the Neutral wire to the rest of the neutrals in the switch box. The Hot wire to one of the always on Hot wires and the Switched Hot to the light you are controlling. Note that this is typical but may not actually represent your situation.

One final note. The kit does NOT include a heat-sink for the TRIAC. I have run a 100W bulb with no heat sink with a 5-10 minute maximum 'on' time limit. I have also run 480 watts of lights for 60 minutes, but that does require a heat-sink and I used the metal top of the plastic box. However, if you need a heat sink, how large it needs to be and how it needs to be constructed depends entirely on the size of the load you intend to drive as well as the maximum 'on' time you've programmed. You'll probably need to get creative if you start experimenting in this space.


Download

Software and Circuit diagram


Forum

Talk about it here