2
\$\begingroup\$

I am trying to drive a 5V Relay using ESP8266-01 module.

Following image is the circuit diagram i have uploaded.

My sketch is about connecting the esp to the mentioned Access Point and controlling the relay from esp.

When i power my device ( same as the connections given in the image below ), the esp turns on (blue led is turned on on esp) but it does not work according to the sketch.

When i remove the connection between GPIO2 (of esp) and R3, the esp works according to the program logic but in that case i wont be able to control the relay from GPIO2 (of esp).

Can anyone guide me resolving this issue ?

Schematic

\$\endgroup\$
14
  • \$\begingroup\$ Have you tried powering the ESP and relay separately to ensure your power supply isn't part of the problem? \$\endgroup\$
    – ccolton
    Commented Mar 26, 2020 at 20:38
  • \$\begingroup\$ Have you tried connecting the input of R3 alternately to 3.3V and GND instead of GPIO2? Have you measured the current the relay takes by connecting A2 to GND with an ammeter? \$\endgroup\$
    – jonathanjo
    Commented Mar 26, 2020 at 20:46
  • 1
    \$\begingroup\$ What is ESP rate I/O high current. || GPIO2 may not allow 4+ mA - try R3 = 10K. || Power supply may not support relay current. Use an LED and resistor instead of relay to test. || Does relay pulse on and off then stop wkg? || Did it work? \$\endgroup\$
    – Russell McMahon
    Commented Mar 26, 2020 at 20:58
  • 1
    \$\begingroup\$ @javabee PLEASE answer all questions: As above (lightly edited): Q!: What is ESP rated I/O high current - UNANSWERED . || Q2: GPIO2 may not allow 4+ mA - try R3 = 10K. Result? UNANSWERED**|| Q3: Power supply may not support relay current. - **ANSWERED Q4: Use an LED and resistor instead of relay to test. Result? **UNANSWERED**|| Q5: CRUCIALLY - Does relay pulse on and off then stop wkg? **UNANSWERED**|| Did it work? -> \$\endgroup\$
    – Russell McMahon
    Commented Mar 27, 2020 at 5:50
  • 2
    \$\begingroup\$ @javabee You could have saved yourself and us significant time and effort if you had properly described what happened. Until now there was no indication that "...does not work..." means that the relay operates. | It is reasonably likely but not certain that the relay is interfering with the processor in some way. | Q6: Is the diode mounted close to the relay coil contacts ? Q7: Is the diode the polarity shown and Q8: Is the diode intact electrically. eg if the diode was reverse poilarity it COULD have blown O/C (unlikely) and there would be no inductive spike protection. ... \$\endgroup\$
    – Russell McMahon
    Commented Mar 28, 2020 at 2:15

2 Answers 2

3
\$\begingroup\$

One problem that I can see with your circuit is that the 2N2222 transistor may not have sufficient current gain to turn the relay on (and keep it on).
The relay may need anywhere from 60 to 100mA, the transistor gain is guaranteed around 50 at around 100mA AND (this is VERY important) 1.0V across the collector-emitter junction.
This means that you might have only 4V available to the relay, and some 5V relays need at least 4.5V to turn on.
It is not clear from your question whether by the circuit "not working" you mean the relay or the IC?
I would suggest using a low voltage MOSFET (even a regular one should work since it needs to drive a small current) instead of a bipolar transistor like 2N2222, as that would solve both problems at once: it would not load the IC output and it would have almost no voltage drop (giving the relay the full 5V for its operation).
Check the voltage on the output pin when the transistor is connected, and check the voltage on the collector pin when the transistor is on, and let us know what you got!

P.S.: Some of my values above may be somewhat incorrect, but the issues mentioned are present.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ I am unable to purchase anything at the moment due to corona lockdown. Other than this i only have bc547, and an optocoupler which is used on single channel relay module. the relay which i am using is triggering if i power up its coil with 3.3v. \$\endgroup\$
    – java bee
    Commented Mar 27, 2020 at 5:35
  • \$\begingroup\$ If it turns on at 3.3V then it's probably an issue with current gain. \$\endgroup\$ Commented Mar 27, 2020 at 8:28
  • \$\begingroup\$ Can you measure your relay's coil resistance and post it here? \$\endgroup\$ Commented Mar 27, 2020 at 8:36
  • \$\begingroup\$ I have checked the datasheets for 2N2222 and 2N3904, and the Vce (collector-emitter) saturation voltage on both is specified at the current gain of 10, meaning that you would have to drive your transistor's base at 1/10 of the relay coil current. I have also checked on a breadboard with a 2N3904, and the collector voltage at ~100mA was 0.53V with 2.06mA into its base, and 0.24V with 4.12mA base current. In your case, there is maybe about 2.5mA base current. You should drop the base resistor value to 470Ω, and then 330Ω and see if it works. \$\endgroup\$ Commented Mar 27, 2020 at 9:21
  • \$\begingroup\$ Sorry about this many comments, but I have just checked the BC547 and its Vce saturation voltage is specified at a current gain of 20 (instead of 10 like the 2N2222 and 2N3904) so I would recommend you first replace the 2N2222 with the BC547, and then reduce the base resistor to 680Ω or 560Ω even if it works with 1kΩ, in order to ensure the turn on at all times. \$\endgroup\$ Commented Mar 27, 2020 at 9:37
1
\$\begingroup\$

I have checked some info on the ESP8266-01, and your problem is that BOTH the GPIO and GPIO2 need to be pulled UP for the chip to start working. The GPIO2 in your case is pulled DOWN through the base of the transistor. The chip at startup checks the GPIO pins, and then it makes them available for use as inputs and outputs. That's why your chip works without that resistor connected, and doesn't work when you connect the resistor.

I have searched and searched, but I had to come up with my own solution to your problem. Here it is:
enter image description here

You might get away with simply adding the R2 and C3 to your board.
C3 here acts as an RC delay circuit with R3, giving you a positive pulse at GPIO2 at startup. Apparently, ESP-01 pauses for 40ms during the power up sequence, sends some bytes through GPIO2 for 100ms, and then it boots into normal program. This means that you only need to hold the GPIO2 at the logic high level for more than 140ms, and this CR combination should give you more than 200ms.
The R2 is needed here to prevent the C3 from discharging through GPIO2 at more than 12mA (maximum allowable current for GPIO pins) when GPIO2 goes to logic high.

If you don't want the relay to turn on shortly when powering the circuit, you could add the C4.
R1 and R2 could be used to ensure the C3 and C4 discharge in about 5 seconds after power off, so that they function as designed if you power the circuit back on within 10 seconds (but not sooner than after 5 seconds have passed).

This RC combination will also give you a slight delay in switching the relay on and off (should be less than 0.3 seconds or 300ms), but that should be fine for most purposes.

P.S.: For anyone asking, here is a datasheet for this module from MicroChip: http://www.microchip.ua/wireless/esp01.pdf
Here is also a little more about the boot modes: https://www.forward.com.au/pfod/ESP8266/GPIOpins/index.html

P.S.: I realize that C2 could be 10uF, so just use that!

\$\endgroup\$
15
  • \$\begingroup\$ Hi. After checking all the cases and trying them on i have too reached on this same conclusion. Please let me know if u find something. \$\endgroup\$
    – java bee
    Commented Mar 27, 2020 at 11:44
  • \$\begingroup\$ Is your GPIO2 active HIGH or active LOW (when it's supposed turn the relay on)? And can you change that in the code? \$\endgroup\$ Commented Mar 27, 2020 at 14:39
  • \$\begingroup\$ to turn on the relay gpio2 has to be high. what are you suggesting . please explain. \$\endgroup\$
    – java bee
    Commented Mar 27, 2020 at 15:42
  • \$\begingroup\$ Because the GPIO2 output has to be high from start, it is more difficult to differentiate between its pulled-up high state and its output high state. \$\endgroup\$ Commented Mar 27, 2020 at 21:14
  • 1
    \$\begingroup\$ Here it is, from the maker himself: microchip.ua/wireless/esp01.pdf \$\endgroup\$ Commented Mar 31, 2020 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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