2
\$\begingroup\$

I want to control an 5V relay board with an ESP8266 (which operates on 3.3V). Schematic of the board:

enter image description here

IN2 is normally on 5V and must be pulled to 0V for enabling the relay. I saw people using an NPN transistor like this (second image, the relay board has the same pins but is not the Keyes_SRly) and also people connecting IN2 directly to an GPIO pin. Which one is safer/the right way?

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Your first circuit lacks a supply for the relay, thus it won't work. The second is more likely to do the job but since you didn't describe all circuit components you'll have to make sure they are correctly dimensioned. \$\endgroup\$ – PDuarte Mar 2 '16 at 23:01
  • \$\begingroup\$ Questions about the first schematic. Where is IN1 of the schematic? What is the model (part number) for U3? What is the voltage of VCC? \$\endgroup\$ – Nick Alexeev Mar 2 '16 at 23:10
  • \$\begingroup\$ Is the first diagram the circuit of your relay board? It looks wrong: an opto-isolator drives a transistor which drives a relay. \$\endgroup\$ – Steve G Mar 2 '16 at 23:11
  • \$\begingroup\$ VCC and JD-VCC are 5V. I don't know the part number for U3, the it is not labeled on the board. IN1 and IN2 are the same, I'll rename it in the question \$\endgroup\$ – expet Mar 2 '16 at 23:11
  • \$\begingroup\$ yes, its the circuit of the relay board \$\endgroup\$ – expet Mar 2 '16 at 23:12
2
\$\begingroup\$

The ESP does not have 5V tolerant gpio. You may have a breakout board that includes 5V tolerant GPIO, in that case you can directly connect it, but if you don't the transistor method is better. If you do, the transistor method doesn't hurt either. The transistor will work on both the same way, with a slight difference in current draw if you don't resize the resistor.

So in both case, the use of a transistor to control the optocoupler is preferred. At a few cents for a small signal transistor like the 2n3904 or 2n2222 and a resistor, you ensure your five dollar ESP doesn't fry itself. The trade off is space, but a TO-92 and a 1/4th Watt resistor are tiny. There is really no downside to a transistor.

Update: There is some discrepancy between the schematic shown and the module shown. They are not the same, maybe. The first schematic has an active low optocoupler setup. They look like and can sometimes be powered by a different voltage than the signal voltage.
enter image description hereenter image description here

The Keyes_SRLy pictured is simpler, no optocoupler (i.e. no isolation). It's schematic is supposedly:
enter image description hereenter image description here

In which case the transistor base is directly broken out. This can be directly connected to a simple GPIO.

You need to figure out which one you have. The transistor setup shown won't work on the simpler Keyes_SRly relay module.

\$\endgroup\$
  • \$\begingroup\$ Except that his relay module does not have an opto-isolator. The OP has sent everyone down the wrong path with an incorrect schematic. \$\endgroup\$ – Steve G Mar 2 '16 at 23:57
  • \$\begingroup\$ @SteveG yes, i just edited to address this. \$\endgroup\$ – Passerby Mar 2 '16 at 23:58
  • \$\begingroup\$ I have the first module \$\endgroup\$ – expet Mar 3 '16 at 1:08
  • \$\begingroup\$ @expet then use any small transistor. Good luck. \$\endgroup\$ – Passerby Mar 3 '16 at 1:13
1
\$\begingroup\$

Your schematic is wrong: the Keyes SRly relay module does not have an opto-isolator (U3). It has the transistor (Q3) and the resistor (R6), and the relay etc. Try googling "keyes SRly relay" and you can easily find the schematic.

You can connect this directly to an output pin of the ESP8266. There is no need for opto-isolators or extra transistors.

\$\endgroup\$
0
\$\begingroup\$

This is probably going to work for you. It is similar to your second circuit.

When the GPIO is high, it turns on the transistor which activates the relay. When the GPIO is low, the base of the resistor will be around 0V, so you don't have to worry about the 5V getting back into the wifi module.

The diode helps dissipate the magnetic field in the relay when it is turned off (and prevents damage to the transistor).

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
  • \$\begingroup\$ Any explanation on the downvotes? \$\endgroup\$ – Daniel Mar 3 '16 at 19:05
  • \$\begingroup\$ it helped me out :) \$\endgroup\$ – AlexS Jan 31 '17 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.