As described before in the desing of our project, we need a 12v circuit for the switches while we our pins in the GPIO works with 3.3v or 5v. To separate this 2 circuits we will use a Opto Isolator
What is an opto isolator?
It is a small component with 4 pins, 2 work in input and 2 in output.
The effect is when the current is passing from pin1 to pin2, current is allowed to pass from pin3 to pin4, when there is not current passing between the pin 1 and 2, current can not pass frompin3 to pin4.
Here you can see how it works.
There is a led connected to pin1(+) and pin2(-), so when there is current passing there is light emitted by the led. This light arrives to the sensor (phototransistor) that like a switch allows current to move from pin3(+) to pin4(-). It is not a real switch closing the circuit, it is always closed. Pay attention that, as I remember, current from pin4 to pin3 is always allowed, so in case you invert polarization, it does not work, current always goes through.
As you can see there is a total isolation between the circuit on the left and the one on the right, they can not damage each other even in case of physical failures. It looks like a small IC
Lets start to design how we want to use it:
There is a 12v power supply to provide power to the opto isolator circuit and make possible to have a very very long cable between our controller and the switch. To protect the led inside the opto isolator we need a resistor R1.
We will use pinX to understand if the Switch is close or not, when closed the pinX is connected to ground.
On the other side, the application inside the controller can give power to the pinY. This will give the signal to the relay to close the circuit and connect the main house power to the light, turning it on.
The 12v power supply will be used for
– the opto isolator circuit
– to provide power to the Controller
– to provide power to the relay, because even the relay need power: what is coming from the controller is used only as signal then there is the need of electric power to run the internal magnetic switch to control mail 125/220v switch.
Let’s speak about the components more in deep.
As Opto Isolator we took a PS2501 (20pieces x 1.28$), it is one of the most common, here you can find all detailed specs: ps2501.pdf
From this document we can see the led works with a maximum 80mA (40mA are good enough) and a forward voltage of 1.15v. Using 12v to power it, the suggested resistor is 135 Ohm. I was more conservative, considering lower mA and a power supply less stable, so I choose a 510 Ohm. From this point of view these components are really flexible, use any resistor from 150 to 600 Ohm and it works fine. I have tested with cable till 10m with 510 Ohm without any problem, maybe in case of a much longer cable you can reduce the resistor.
About the Relays, due to our configuration I suggest to use a relay module with optical coupling isolation working with 12v because we have 12v available in our circuit so we do not need other sources.
They usually have 4 pins, 2 for the signal and 2 for the power needed by the relay to activate its magnetic component and pilot the internal switch.
if they is a common GRD for both signal and Vcc, there will be only 3 pins.
As controller we will use Esp8266 wifi soc commonly named Nodemcu. It is small, very cheap, powerful board with Wifi integrated capabilities.
Nodemcu is the opensource firmware available for this small board, even often this name is used to define the board itself.
With a single board of this type we will be able to control 3-4 switch and 2-3 relays, thats because there are few pins available in the GPIO and many of these are reserved. Node MCU can be powere with any voltage from 5v to 24v, so it is perfect for our design where we will have 12v available.