I'm looking to drive a relay on a 5V rail from an Arduino 3.3V pin. Obviously the Arduino I'm using doesn't have the voltage to drive the 5V relay, so that's where the 2N2222 comes in. To test out the circuit, I figured I'd substitute an LED for the time being. My circuit looks like this.
Using a standard, run of the mill, red LED, we say the voltage drop is 2V, and requires 20mA. Using Ohm's law, that gives us a resistor of 150 ohms, correct so far?
For the sake of easy math, we'll say hfe is 100. My understanding is that Ic is Ib x hfe. So we have 20mA = Ib * 100. Giving us Ib of 0.2mA. Using Ohm's law again, and factoring in the 0.7v drop from Vb to Ve we have 12k Ohms. Is that math all good so far?
Right, so plug in the Arduino into where 3.3V is, and set the pin to HIGH, and LED is on. Set it to LOW, and LED is still on. That makes no sense.
Remove the Arduino and make the circuit really simple.
I have a breadboard power supply that outputs 5V and 3.3V, so I take a lead and touch 3.3V to the Rb and LED comes on. Remove the lead, LED goes off.
So success right?
I go back to the Arduino and check, when the pin is set to LOW, my multi-meter sees no voltage. When it's set to HIGH, I see ~3.3V as expected.
Why would this not work?
I also noticed simply touching the resistor Rb also triggers the LED, albeit quite dim.
What am I missing? Or doing wrong?
Edit: I should add, I did stumble across this post, and even copied the resistor values from that, and came up with the same issues. Arduino driven LED stays on, touching resistor makes LED come on, and 3.3V line comes on as expected.
This handy YouTube video also helped me understand some of the logic or what I think is the logic.