Subscribe to Arduino Academy and start learning today for FREE!

Driving Relays with the ESP8266

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 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.

Don’t forget a flyback diode for that relay. I used a 1N4148, which, according to the datasheet, easily handles the current and voltage of the relay.

If you want, you could add a LED / 150 ohm resistor across the coil of the relay as a visual indicator.

Now a simple

digitalWrite(5, HIGH);

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!

Notify of
1 Comment
Inline Feedbacks
View all comments
3 years ago

This only makes sens if you need electrical isolation (i.e. a separate power source to drive the relay), if not just go without the optoisolator. If yes, i'd prefer a solid state relay


Would love your thoughts, please comment.x