I am having trouble turning ON a relay from the ESP8266 output pin. I have wired the relay as shown in the picture below. The only change is that the transistor is BC556. Other specs of components are: Relay 5V , running this on 5V power supply. Here are some more facts: - I have wired the exact same circuit on the bread board and it works perfect. - I am using GPIO2 as the output PIN and apart from driving this relay , it also has a pull up resistor of 10K connected to GPIO2. - The relay works fine sometimes but many times it either does not work or is very slow in reacting. For eg. when my output tuns LOW, it soemtimes takes several seconds to turn ON the relay. - I measured the voltage across the transistor Vce = 1.1 V which means the voltage across the relay pins is 3.9V. I thought this is the reason why the relay is not able to trigger but then I have exact measurements on my breadboard circuit and it works fine. I even tried switching the relays but I get the same result. - From BC556 datasheets it says saturation Vce is .09 - .25V but I get 1.1 voltage drop Is this normal? I tried BC 557 with the same result. I even varied the base resistor to decrease it till 100 ohms but the Vce improve by 0.1 V only. Any suggestion on what should I do to troubleshoot this further would be helpful. Thanks
One thing that limits what you can do is that the ESP chip is 3.3V and your supply is 5V so you cannot use just a PNP transistor to do what you want. Here is one way to accomplish the task without changing the logic (low = on).
Your circuit has two problems - the voltage across the relay coil will only go between about 4.3V and 1.1V- neither fully on nor fully off. That is because it is an emitter follower and has a 0.6~0.7V offset and it cannot amplify the 3.3V coming from the ESP chip.
The 1.1V is particularly dangerous since it is a grey zone of relay operation and some relays may stay pulled in (depending on temperature, whether they have been tapped, etc.) and some may drop out. The 4.3 means you are not getting full voltage on the coil so it might not pull in at very high temperatures. With the circuit above, the relay voltage will go from about 4.9V to 0V, as it should.
The problem is that you are likely not fully turning the transistor on and off. You mention in the comments that the relay is taking 13 mA when off. This is obviously because of your setup.
One, the ESP is 3.3V logic. Even with a 5V pull up, you create a voltage divider between 5V and 3.3V, so the transistor is still conducting. You may also have fried the ESP, just saying.
Two, the 1 kohm resistor may not be enough to saturate the transistor. It's 5 mA maybe, or less.
Three, with a PNP transistor, wiring the relay on the emitter, like a low side driver is not recommended. The emitter will never be a diode drop lower than its collector.
Solution, replace the PNP with a NPN. Remove the pull up, and change your code to invert the logic (1 = on).
Second solution, switch the PNP and relay around, and use a NPN as a PNP driver, because of the 3.3V logic of the ESP.
In such a circuit supplying a low switching voltage usually turns on the transistor. And supplying a high usually turns off the transistor.
However, consider the case of supplying the PNP transistor's base with a 3.3V high signal. It might be expected the transistor is turned off. But if we look closely at the question we see that the relay is run at 5V. Not at the same voltage as the micro processor is running at.
If 5V is supplied to the relay and 3.3V to the transistor's base, there could be as much as 5 - 3.3 or 1.7V difference between the emitter and base. Enough to activate the transistor even when it might be expected the transistor is turned off.
It might be that the relay is never turned off. So that it appears never to switch. Perhaps even thought of as defective.
To be clear, here is your circuit being discussed:
You need a NPN transistor there, not a PNP. The base should be wired as shown, the emitter to ground, and the collector to the relay.
Otherwise, you have to make sure the transistor can support the relay current. Let's say the digital signal is 3.3 V when high. Figure the B-E drop will be 700 mV, so that leaves 2.6 V across the base resistor. With the 1 kΩ resistor as shown, that will result in 2.6 mA of base current. Verify that the digital output can source that. Most can, but you should check anyway. Of course it can't be a open drain output if used this way since those can't source any current.
Let's say you use a transistor with a minimum guaranteed gain of 50. That means it can support up to 130 mA collector current. The relay must be rated for that or less.
Normally, an NPN transistor is used, with the emitter grounded and collector to the relay. That way, the transistor will be saturated when on, so the voltage across it will be about 0.2 volts.
That will invert the logic, so a High from the ESP8266 will turn on the relay.
What is the relay?
Quite often small low voltage relays have a polarised coil (It saves power by having a permanent magnet biasing the field), and will not switch if you connect the coil the wrong way around. This is usually in the datasheets, but is often not so obvious as you would like (Guess who was had by this one....). Look at the pinout diagram on the datasheet for a '+' by one terminal, they mean it, both Omron and TE are buggers for this.
Do respect the 'must operate' and 'must release' ratings from the datasheet, these are temperature sensitive and you can set yourself up for a lot of reliability pain if you ignore these.