ESP8266 doesn't work correctly when IO2 is connected to the base of a transistor

Hi! I am trying to use an ESP8266-01 microcontroller to actuate a a solenoid valve that works on 12V. To power the ESP I've used a 3.3 voltage regulator ( AWS1117-33) that has 12V as input. The GPIO2 is connected to the base of a 2N2222 transistor that should act as a switch to open the valve (I will attach the diagram of the circuit below). The ESP8266 is programmed to turn off the LED and turn on 3.3V at the GPIO2 pin when a button on my phone is pressed. When just the 3.3V pin is connected to the output of the regulator and the GND pin is connected to "-", all works fine. Unfortunately, when I connect the GPIO2 to the base of the transistor and try to turn off the LED, it doesn't work. The LED is on and the valve doesn't open. What could be the problem here?

You must put a resistor in series with the base of the transistor to limit the current, maybe 1k or something.

You must put a reverse biassed diode across the solenoid to protect the transistor from the back EMF. You might have already killed the transistor.

PerryBebbington:
You must put a resistor in series with the base of the transistor to limit the current, maybe 1k or something.

You must put a reverse biassed diode across the solenoid to protect the transistor from the back EMF. You might have already killed the transistor.

Thank you a lot for advices!

Your diagram is missing the LED to which you referred.

You cannot connect either GPIO0 or GPIO2 to ground (which means the base of a transistor, with or without the resistor) as this will prevent the ESP8266 from booting your program. You must allow it to pull up to your 3.3 V when it boots.

In your setup, you should use the "TX" line (GPIO1) of the ESP-01 instead to control the solenoid and instead of a 2N2222 whcih I suspect is not adequate, you should be using a logic-level FET.

Sorry, you need the right parts and you need to do it properly so wait until you get the right parts. What is the DC resistance of the solenoid?

Why is there 150R in series with the regulator?

Paul__B:
Your diagram is missing the LED to which you referred.

You cannot connect either GPIO0 or GPIO2 to ground (which means the base of a transistor, with or without the resistor) as this will prevent the ESP8266 from booting your program. You must allow it to pull up to your 3.3 V when it boots.

In your setup, you should use the "TX" line (GPIO1) of the ESP-01 instead to control the solenoid and instead of a 2N2222 whcih I suspect is not adequate, you should be using a logic-level FET.

Sorry, you need the right parts and you need to do it properly so wait until you get the right parts. What is the DC resistance of the solenoid?

Thank you for the reply! The LED I am speaking about is the built-in LED of the ESP8266-01. The resistance of the solenoid is 20Ohm.

PaulRB:
Why is there 150R in series with the regulator?

As far as I understand, the ESP8266-01 works with 0.08A. I have a 12V source so I thought that a 150Ohm resistor should reduce the current to 0.08A.

andstef:
As far as I understand, the ESP8266-01 works with 0.08A. I have a 12V source so I thought that a 150Ohm resistor should reduce the current to 0.08A.

At the risk of being rude that’s nonsense, electricity doesn’t work like that. You supply the correct voltage and the device you are supplying takes the current it needs.

You need to understand Ohm’s Law and Kirchhoff’s circuit laws
These laws are fundamental to all electrical circuits and if you learn them a lot of the mysteries of electricity will become clear. In many cases applying both laws to a circuit will provide whatever answer you are looking for. The explanation you need comes from an understanding of Ohms law.

andstef:
As far as I understand, the ESP8266-01 works with 0.08A. I have a 12V source so I thought that a 150Ohm resistor should reduce the current to 0.08A.

Very roughly speaking, it's correct that the esp draws 80mA. Let's imagine the esp consumes exactly 80mA at all times. The 150R resistor would drop 0.08 x 150 = 12V, so there would be no voltage left for the esp, and so it could not run and could not consume 80mA. So that has to, must be, wrong. The esp needs 3.3V as you probably know. Therefore you need your resistor to drop 12 - 3.3 = 8.7V. You would need the resistor to be 8.7 / 0.08 = 109R.

But you have a voltage regulator in there as well. To output 3.3V, the regulator is going to need probably 1.5V more than that as it's input. It needs some voltage to operate itself. But with 109R resistor, it only gets 3.3V, not the 4.8V it needs. So it would be unable to maintain that 3.3V output, and the esp won't operate correctly.

But it's more complex than that in practice. The esp only consumes 80mA on average. That's what you would measure with a multimeter. But if you had an oscilloscope, you would see that the esp consumes only around 20mA, but with frequent spikes of 200mA or more as it communicates with your WiFi router. So it's never actually consuming 80mA at any point in time, and 109R series resistor is never right, never drops the correct voltage.

That's why voltage regulators were invented. They supply the desired output voltage, whatever current is being drawn at any instant in time. But if you put a resistor in series with them, they can't do their job.

Does that make sense to you now?

andstef:
The resistance of the solenoid is 20 Ohm.

Space provided for clarity!

OK, the solenoid will draw 12 / 20 or 600 mA. That is the absolute maximum rating for the 2N2222 so it is clearly the wrong transistor. The 2N3904 that is cited in your diagram is rated only for 200 mA which is even more inappropriate. :roll_eyes:

So, you need a logic-level FET, Something modern such as an AO4484 perhaps (I know, but you can put them on breakout boards :grinning: ).