The ESP8266 has a current limit of 12ma, so we have to take care when driving relays.
First I start with a 817C Optoisolator. The datasheet says says the IR LED drops 1.2v, so I plug 3.3v source voltage, 1.2v forward voltage, and 12ma forward current into the calculator at http://led.linear1.org/led.wiz and I get a 180 ohm resistor as the optimum value.
Now, according to the datasheet for the 817C, the Current transfer ratio at 12ma is around 120%, so I can only conduct around 14-15ma through the photo transistor, not nearly enough to trigger a relay.
Let’s have the photo transistor trigger an additional transistor, one that can allow the full current needed by the coil of the relay. I have chosen a 2N5551, since the datasheet says it can handle 600ma and 5v.
Now the relay. I chose a HK19F 5v 125ohm relay which, according to Ohm’s law and the datasheet, draws 40ma. It’s a DPDT relay (most commercial modules use a SPDT), but the contacts are only rated for 1A or so.
If you want, you could add a LED / 150 ohm resistor across the coil of the relay as a visual indicator.
Now a simple
makes it all happen!
UPDATE: 3v MOSFET instead of optoisolator. Simpler and cheaper!
Become the Maker you were born to be. Try Arduino Academy for FREE!