Electronic – ESP8266 not working to drive relay

esp8266npnrelaysolid-state-relaytransistors

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

Best Answer

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.