不想购买昂贵的变压器或转换器来运行您简单的电路,您可以试试不使用变压器供电的方法。Tom建造了一个开关,来控制厨房的灯。当门打开时,处理器感知簧片传达的信息并为电源插座供电。三分钟后,如果门没有关,电源插座将断电。
Instead of buying expensive transformers or converters to run your
simple circuits, try using a transformerless power supply. Tom used
one to construct a switch that controls lights in a pantry. When
the door is opened, a processor senses a reed relay and powers up
the outlet. Three minutes later, if the door is not closed, the
outlet shuts off.
Do you need a low-cost way to run a simple 5-V circuit from 120-VAC
power without bulky and expensive transformers or converters? If
so, a transformerless power supply may be just what you require.
Figure 1—This is the basic dangerous circuit.
Before I begin, review the circuit in Figure 1. Do you see
something of concern? Do you understand why your mother would warn
you against building this circuit?
If you said “no,” stop right here and read no further. This is by
no means a beginner’s circuit. It should be considered an
experimental design. I make no claim that this design is safe. You
are responsible for knowing and implementing all of the necessary
safety precautions when working with this or any circuit connected
directly to mains voltages. Now, make your mother happy by being
safe and responsible. Have fun and let’s continue.
THE PROBLEMThis design started from my dissatisfaction with both the Smart
Home Systems X10 and Insteon light switch controllers. I never
could get a reliable signal out to all of the modules. Plus, I
really needed only a simple open/close sensor and maximum on-timer
functionality. I thought there should be an easier solution. Having
done some recent work with an Atmel AVR processor, it seemed a
simple answer would be to use an AVR processor monitoring a switch
and controlling a TRIAC.
Photo 1—This AVR switch is mounted next to the receptacle that it controls.
Opening the door powers up the receptacle and turns on the lights
inside the kitchen pantry.
Here’s a quick preview of the end of the story just to give you an
idea of the potential applications. I built an AVR switch with my
transformerless power supply. My AVR switch controls lights I
placed inside a pantry (see Photo 1). The AVR switch resides inside
an outlet box in the inside wall just over the door. The outlets on
the right half of the box go to small under-cabinet lights mounted
along the inner pantry wall. The AVR switch monitors a reed relay
embedded in the door frame directly below the outlet box. When the
door is opened, the AVR processor senses the reed relay and powers
up the outlet. Three minutes later, if the door is not closed, the
outlet is turned off.
Photo 2—In this variation, the AVR switch turns the lights on when the push
button is pressed and automatically turns the lights off if the
maximum on time is exceeded.
Refer to the simple timed light switch in Photo 2. On the left side
of the wall plate, this AVR switch configuration has a push button
and a small LED nightlight.
Pushing the button turns on the light. Pushing it again turns off
the light. If I leave it on for longer than 45 minutes, the light
automatically turns off. As a result, my lights no longer stay on
all day and my electric bill is under control.
Photo 3—The low parts count enables the transformerless power supply,
processor, and TRIAC to all fit in a 1.75” × 2.5” enclosure.
As you can see in Photo 3, the AVR switch is compact. The AVR
processor is at the top. The transformerless power supply
components are at the bottom.
THE SEARCH FOR A SOLUTIONAt the beginning of this project, the idea was to use a low-power
AVR processor to control a TRIAC. A quick Internet search turned up
numerous examples of how to use a microcontroller with a TRIAC to
control a 120-VAC load. One example is presented in Microchip
Technology application note TB094, “Dimming AC Incandescent Lamps
Using a PIC10F20.” I thought this would be easy, so I started
listing components: a processor, a TRIAC, a transformer, a
rectifier, a capacitor, a zero-crossing detector, a TRIAC
optoisolator, and the list went on. It quickly became apparent that
this would never fit in the 1.5” × 2.5” × 1” area inside a standard
wall outlet box. I needed something smaller and simpler.
The largest components obviously compromised the power supply. A
typical wall-wart power supply, with its transformer and rectifier,
would have taken up all the space by itself. Thus, the problem
became how to make a tiny power supply. I then remembered I had a
handful of unused cell phone mini-chargers lying around. They were
small, so I thought perhaps there was an answer inside. A victim
was selected so I could find out. The circuit board from the
mini-charger is shown in Photo 4.
As I expected, there was no large transformer or rectifier
involved; instead, it was a miniature switching power supply.
Unfortunately, even with the smallish inductor, the overall size
was still too large for the space I had to work with. Thus, after
estimating the size of a complete solution, the miniswitcher was
removed from the list.
Photo 4—This is a TinySwitch-II miniswitcher.
If you are still interested in a mini-switcher for your own
project, check out the TinySwitch-II family of parts from Power
Integrations. (A TNY266PN is shown on the left in Photo 4.) A small
4- to 15-W power supply is possible.
Another interesting part I ran across during my Internet searches
was a Supertex SR086/87 adjustable off-line inductorless switching
regulator. This is a true inductorless power supply that works by
switching a transistor on or off when the rectified AC is below or
above the desired output voltage. This part can source only 100 mA,
which was sufficient for my purposes, but it had one major problem.
The circuit called for a rectifier that would cause the DC outputs
to float relative to the AC mains. Therefore, I would not be able
to drive a TRIAC directly from the processor outputs.
Using an optoisolator to drive the TRIAC would have been possible,
but it would have increased the parts count and thus the overall
physical volume. Still striving for the minimum size and parts
count, my search
continued.
Finally, I wondered how the original X10 modules fit a complete
power supply into such a small space. This
seemed like a potential path to a solution, so I decided to dissect
one (see Photo 5)!
Photo 5—This is an opened Smart Home Systems X10 light switch.
Inside the X10 module was a notable absence of any type of
transformer, power regulator, or rectifier; it included mainly
resistors and capacitors. It appeared that the inductor on the
right (see Photo 5) was used only for reducing the output noise
caused by the X10 dimmer functionality. Few components were left to
make up the actual power supply. The X10 module was the essence of
a minimal design. So, the question became, How did X10 build this
transformerless power supply?
It turned out that the phrase “transformerless power supply” was a
good Internet search term. I found various designs that all focused
on a simple capacitor used to siphon power from the AC mains. One
find was a tutorial on transformerless power supply design
published as a Microchip technical brief, “TB008: Tranformerless
Power Supply.” I also found a Microchip application note, “AN954:
Transformerless Power Supplies: Resistive and Capacitive,” that
actually went into the calculations for the various component
sizes. This finally appeared to be a solution. I now had the
beginnings of my experimental transformerless power supply.
MAINS-POWERED SUPPLYMy first objective was to use the calculations in application note
AN954 to determine the required component sizes. I based the design
on the high-voltage capacitor because its physical size, cost, and
availability would control the overall design. For example, an X2
class capacitor is relatively large for its small farad value.
Higher value X2 capacitors increase rapidly in both size and cost,
which greatly limits the available component choices. Application
note AN954 also makes some safety suggestions, which I incorporated
into the basic design. Finally, note that while not currently
Underwriters Laboratories-approved, I knew it would be possible to
produce an enhanced design to meet UL approval requirements. Read
the “Other Considerations” section in the application note or visit
the UL web page (
www.ul.com) for more information.
Figure 2—This is a basic capacitive transformerless power supply.
After crunching some numbers, I defined the basic circuit in Figure
2. By design, this circuit can provide only about 10 mA of
continuous current. Drawing any more current will cause severe
output voltage sag. Unfortunately, I found that attempting to
create a higher current design increased only the size of the X2
capacitor even more rapidly.
I read about alternative designs with higher available currents in
the application note. One such alternative involved adding a
rectifier to the front end of this design. However, as before, I
knew that using a rectifier would make it impossible to drive a
TRIAC gate from a processor output directly.
Another alternative was a resistive design, rather than a
capacitive one. The problem with the resistive design was that it
required a 10-W resistor, which would result in both size and power
dissipation issues. Ultimately, I determined that the 10-mA
capacitive design was a reasonable compromise between available
current and component size.
To minimize exposure to high voltages during construction, I
decided to build the transformerless power supply prototype as a
unit separate from the logic circuit prototype. The idea was to
allow for the troubleshooting of the logic section from a normal
5-VDC supply and risk only high-voltage exposure when
troubleshooting the power supply itself. However, at some point, I
knew both sections would have to be integrated, but being built as
separate prototype units helped minimize risk.
Once I had an assembled prototype of a transformerless power
supply, the first issue was trying to determine how well the power
supply was working. The problem was that the DC ground for the
circuit design floated just 5 V below the hot line of the 120-VAC
mains. Thus, if I had tried to measure the 5-DC output with my
mains-powered oscilloscope, and if I was extremely lucky, I might
have seen a 115-VAC signal. However, it was much more likely that I
would have simply fried everything. The dilemma was that the signal
ground on the oscilloscope was at Earth ground. If I had tried to
connect both grounds together, the 115-VAC potential difference
between the two grounds would have resulted in substantial current.
The solution would have been an isolation transformer. This would
have effectively kept the two grounds separated and enabled me to
connect the oscilloscope signal ground to the circuit DC ground and
observe the 5-VDC output. However, even with the isolation
transformer in place, there would still be 120 VAC in the circuit.
So, it still would be a dangerous circuit.
Figure 3—This is a simple isolation transformer.
Unfortunately, I didn’t have an isolation transformer handy, so I
had to improvise by wiring two step-down transformers
secondary-to-secondary (see Figure 3). With a pair of 120-/24-VAC
transformers wired backto-back, the 120-VAC input was converted to
24 VAC and then back to 120 VAC on the output side. More
importantly, no DC current could flow between the input and output.
With my improvised isolation transformer, I could then verify the
proper operation of my transformerless power supply prototype.
If you build this isolation transformer, think the component
selection through and don’t overload the transformers. Remember 100
mA at 120 VAC becomes 0.5 A in the 24-VAC winding, so plan
accordingly. This can really become an issue if you plan on testing
your circuit while it’s actually controlling a load! A 60-W bulb
could pull over 2.5 A through the 24-VAC windings. Fuse
appropriately and check the VA rating of the transformers. Be
careful. Check your numbers and double-check your setup before you
proceed. Remember that you still have 120-VAC potentials in the
circuit even with the isolation transformer!
AVR POWER SWITCHWith my experimental transformerless power supply designed, I
needed to add an AVR processor and a TRIAC to finish my AVR power
switch. Because this was intended to be an experimental platform, I
added a few expansion options to the circuit—that is, at least as
much as I could shoehorn into such a small space.
I chose an Atmel ATtiny26L. The “L” variant had an operating range
down to 2.7 V with an idle current of 0.18 mA. Again, low power
consumption was critical because the transformerless power supply
could provide only a small amount of continuous current. Another
benefit was that with the wide operating range, it was possible to
draw a little more current from the power supply and still survive
the resulting voltage sag.
I then selected an isolated gate TRIAC. For smaller loads, the
TRIAC could operate without a heatsink; but for larger loads, it
would need a heatsink. I placed my final design in a plastic box
with a metal lid. The metal lid made a good heatsink. But because
of the transformerless power supply, the TRIAC had to be an
isolated tab version. Otherwise, the TRIAC tab and thus the
heatsink/metal lid would have also been energized.
With the processor and TRIAC in place, I included two digital
switch inputs in the circuit, one zero-crossing interrupt, one
analog input with an adjustable potentiometer, one digital output
with a display LED, and the basics of a two-wire interface. Because
the entire circuit was at mains potential, any wiring leaving the
circuit was also at mains potential. So, to help improve the safety
of the switch inputs, I placed current-limiting resistors on both
sides of the switch inputs. The chosen resistor values were the
highest values possible that still allowed the processor to detect
the switch closure. Putting everything together resulted in the
circuit shown in Figure 4. Remember: Even with these precautions,
you still need to install the box and circuit so that no one can
come into direct contact with potentially energized components.
Figure 4—The completed circuit contains the transformerless power supply, a
TRIAC, an optional zero-crossing signal, and various other I/O
options. The end result is a simple experimental platform, which
can be easily configured to support many different uses.
One of this transformerless power supply’s advantages was that it
had a built-in zero-crossing signal. However, with the capacitive
version of the design, the zero-crossing signal had a substantial
phase shift. I was not planning to use any dimming functionality,
so the zero-crossing signal wasn’t necessary. However, because this
was an experimental platform and the signal was available, I
included it anyway. One result of that decision was that I could
also use the zero-crossing signal to shorten the time the TRIAC
gate needed to be powered. As a result, the overall circuit current
demand was reduced. To implement the TRIAC control, the software
was designed to turn on the TRIAC immediately after the
zero-crossing and to leave it turned on only until sufficient
current was being conducted through the TRIAC to latch the gate. At
that point, the TRIAC gate could be released and the TRIAC would
stay energized.
A second issue associated with the zero-crossing signal was a
tendency to ring when there were switching transients present on
the incoming 120-VAC line. I tried to compensate for this. But if
the circuit is used in a noisy environment, there may be some false
triggering of the zerocrossing interrupt.
SOFTWAREThe sample software for the AVR power switch platform was written
in WinAVR C. It was relatively straightforward to write. Code size
and performance were not really issues with this application.
The entire software package comprised several interrupt-driven
functions. The first routine, Main, was nothing more than an empty
loop. The IOInit routine did just that; it took care of
initializing the processor. OnTime was a simple routine to look at
the ADC value and select the appropriate “maximum on time.” A
minimum ADC value equated to a 5-minute delay while increasing
values gave a longer delay up to a maximum of 59 minutes.
The interrupt service routine (INT0_vect) was the heart of the
software. It was driven by the zero-crossing signal from the
transformerless power supply. The routine was responsible for
determining the remaining “on time” and debouncing the two switch
inputs. It also used the current TRIAC state, the input switch
states, and the remaining maximum on time to determine the next
TRIAC state. If the routine determined that the TRIAC should be on,
it started Timer1 with the necessary delay time to compensate for
the zero-crossing detector’s phase shift. Then the Timer1 compare
interrupt service routine (TIMER1_CMPB_vect) turned on the TRIAC
and restarted Timer1 for the TRIAC latching delay. When the Timer1
compare routine triggered again, the TRIAC was released. Remember,
at this point, the TRIAC would stay latched by itself until the
next zero crossing. This also meant that in the future, if I wanted
to modify the software to support dimming, the basics were there.
It would just involve lengthening the phase-shift delay as required
to control the TRIAC turn-on point.
Input ports PA0 and PA1 were defined as the inputs for the
switches. PA0 was intended to act as a door open/door close switch.
This means shorting PA0 to DC ground moves the TRIAC state to off
and floating PA0 moves the TRIAC state to on.
Switch input PA1 was intended to act as an On/Off push button. Each
press and release of a push button would toggle the TRIAC state.
Yes, the software was designed so that both PA0 and PA1 could be
used at the same time.
ADC2 was used to sample the potentiometer to determine the maximum
on time. Output port PA4 was designed to control the indicator LED,
while output port PA7 was used to drive the TRIAC gate.
If you decide to try your hand at building this circuit after
reading this article, there are a few other considerations to keep
in mind. Skip in-circuit programming capability; otherwise, I
guarantee that at some point you will accidentally hook-up your
in-circuit programmer while the circuit is connected to the mains.
I’m not sure if you and your PC will survive that connection.
Always use your isolation transformer if there is even the
slightest possibility that you or your equipment could come into
contact with the circuit. It’s still a dangerous circuit.
Remember to measure! Your voltmeter and oscilloscope are your
friends. Before you make a connection, measure the potential
between the connection points. Is it really 5 VDC, or did the 120
VAC sneak in? It’s much better to measure often rather than smoke
parts.
Construct your circuit so that no external component can be
touched. For example, use in-wall magnetic reed-relay switches,
such as those found in alarm systems, for external switches.
Remember that despite the fact that the processor is running at 5
VDC, it is actually at a 120-VAC potential.
For any external push buttons, make absolutely sure that there are
no exposed grounded or other metal surfaces. Those metal surfaces
will most likely be at circuit ground, not Earth ground, and thus
at 120-VAC potential! Also be aware of your push buttons’ failure
mode. Do the plastic tops pop off to expose metal underneath?
Keep the unfused areas as small as possible. That way, if you short
something out, there’s a better chance that what you shorted will
be behind a fuse. Also, keep the 120-VAC side of the circuit
together and away from the 5-VDC side as much as possible. This
helps reduce the voltage differential between components in case
something shorts out.
FUTURE PROJECTSI plan to include a daylight sensor and a clock in the next version
of my AVR switch. But what ultimately makes it into the next
version will depend on the project’s space and power consumption
requirements.
If you’d like to experiment with your own AVR switch, I have
circuit boards and part kits. You’ll have to assemble everything,
and more importantly, determine for yourself if this circuit is
safe and appropriate.
I hope you find this information as useful and as interesting as I
have. Maybe your next project will also incorporate a
transformerless power supply!
Author’s note: Your safety is your own responsibility. You must use
equipment and safety gear properly, and determine whether you have
adequate skill and experience. Power tools, electricity, and the
other resources used for these projects are dangerous, unless used
correctly and with adequate precautions, including protective gear.
Some illustrative photos do not depict safety precautions or
equipment, in order to show the project steps more clearly. Use the
instructions and suggestions listed here at your own risk. It is
your responsibility to ensure that your activities comply with
applicable laws. You can download the sample code at
www.JenRathbun.com/Electronics/AVRSwitch.html.
Tom Struzik (
tpstruzik@earthlink.net) has been building and taking things apart from an early age. He
built his first Heathkit project at 12 and sold his first computer
program at 16. Tom has a BSEE from Purdue University, and currently
works for a Fortune-100 chemical company in its engineering systems
organization as an IT systems architect. He continues to build
software and hardware projects at home to “keep his hands dirty.”
One of Tom’s current projects, the “Cat Faucet,” was recently
covered by Engadget.com.
PROJECT FILESTo download code, go to
ftp://ftp.circuitcellar.com/pub/Circuit_Cellar/2009/226.
RESOURCESR. Condit, “AN954: Transformerless Power Supplies: Resistive and
Capacitive,” Microchip Technology, Inc., DS00954A, 2004.
S. D’Souza, “TB008: Transformerless Power Supply,” Microchip
Technology, Inc., DS91008C, 2008.
Microchip Technology, Inc., “TB094: Dimming AC Incandescent Lamps
Using a PIC10F20,” DS91094A, 2005.
More information on kits and discussion forums on this project:
Sixerdoodle Electronics, “AVR Switch,”
www.JenRathbun.com/Electronics/AVRSwitch.html.
Underwriters Laboratories, Inc.,
www.ul.com.
SOURCESATtiny26L Microcontroller
Atmel Corp. |
www.atmel.comTNY266PN TinySwitch II
Power Integrations, Inc. |
www.powerint.comSR086 Switching regulator
Supertex, Inc. |
www.supertex.com