In the life of every smart home a logical question arises - how can a
small gentle microcontroller with its five volts be able to switch 220 volts of alternating current?
The easiest option is a relay. Here you have a galvanic isolation, and you close what you want, even though it is constant, though variable, little, a lot. But the cons of the relay is very unpleasant.
The first one is clicking. Annoying. Yes, man is such a thing, he will adapt to everything, and one can get used to it, but why get used to the bad?
')
The second one - when commuting a large current, at the moment of switching on, and even with long wires from the relay to the switched device, there is a very big hindrance. Agree, when you turn on the light in the toilet at night and the computer and the bedroom light turn on in parallel - it is unpleasant.
Therefore I want to share with you the best, in my opinion, way to switch 220 volts of alternating current - a semiconductor key of alternating current.
On Habré there was already an excellent theoretical
article on this topic, describing in detail both the triac and optocoupler.
I'll try to tell you how I did it.
So, the triac itself is already a key AC. But to control the legs of the microcontroller galvanic isolation is required. By controlling the triac directly - the controller may die. It really can. I was dying. I have already killed dozens of AVR's MK in different ways, but this topic is worthy of a separate post. To make a galvanic isolation, use optocoupler MOC3063.
Using it, we kill two birds with one stone, for it is equipped with a zero-crossing switching voltage detector, that is, the triac will open only at the moment of transition, and the voltage will increase smoothly, which will not only get rid of interference, but also prolong life, for example, lamps ( which in most cases die at the moment of inclusion).
Schematic diagram:

Divorced fee:

Price issue:
- Triac - 30 rubles
- Optocoupler - 30 rubles
- Everything else - 30 rubles
As a result, the cost of the key is about 100 rubles, which is equal to the cost of a similar relyushki.
Pros:
- Silent
- Galvanic isolation
- Does not create as much interference as a normal relay.
- More useful for switched devices
Minuses:
References:
wiringschematic diagramPS And finally, a great
backup power scheme