It looks like you need a current limiting resistor between your output pin and the transistor base (assuming you didn't omit it on purpose, for brevity). Without the resistor, when you set the output pin to HIGH, you are causing a short-circuit to ground. That may damage the pin, if it hasn't done so already.
I would guess that a 10K resistor would do it. That's what I use with my ATmegas and ATminis. But check your MCU datasheet for appropriate values.
It's quite a common mistake to think that the base-emitter junction already has some sort of current limiting property, but it doesn't. It has very low impedance. I usually think of it as a plain wire. That helps me avoid these kinds of mistakes. Same applies to collector-emitter junction.
By the way, your collector-emitter path has no current limiting resistor or load. Watch out!Â
I am assuming you have a schematic like below:
simulate this circuit – Schematic created using CircuitLab
In this case, when the switch is not pressed, the circuit is completed and the diode lights up. When you press the switch, SW1 opens and the LED turns off.
Yes, an inverting setup is the most common type of NPN single stage amplifier. It is called 'common-emitter'.
To reverse this using an NPN, you can do the following:
simulate this circuit
The common-emitter configuration for an NPN BJT works by amplifying base->emitter current to collector->base current. The NPN BJT consists of a base->emitter diode. When this is turned on (the voltage across it is greater than about .6V), any current injected into the base will be amplified by the 'beta' of the transistor, typically ~100.
So for our circuit, when the switch is disconnected, the entire V1 is across the resistor R2 and the base->emitter diode of the NPN. This turns the diode on, to 0.7V. The rest of the voltage goes across the resistor R2. This creates a current, due to Ohm's law, of
\$ I = V/R = (V1-0.7)/(R1) \$.
Let's assume V1 = 5V and R2 = 10k as in the circuit above.
\$ I_b = (5-0.7)/(10000) = 4.3/10000 = .00043A = 0.43mA.
\$
When the NC (normally-closed) switch is not pressed, there is voltage division between R2/R3. Voltage at the base is (500/10500)*V1; this is so low that Q1 is off and the LED gets no current. However, when you press the switch, the lower branch of the voltage divider is cut off and now you have the ~threshold voltage (about 0.7 V) at the base of the BJT. This leaves (V1 - 0.7V)/R2 current flowing into the base to be amplified through the LED.
For an NPN, it will typically amplify the base current by 'beta' unless it cannot anymore. One common reason for this is that the collector->emitter voltage becomes too low. This is called saturation, and the potential difference between collector and emitter when saturation occurs is called the 'saturation voltage'. It is typically 0.2V.
This is important because the NPN common emitter amplifier is often 'driven into saturation'. This means you give it more base current than it can amplify, which will force the circuit into a very predictable state. This is easy to used in design. In our example, assume the saturation voltage is 0.2V. Also, LEDs usually have a forward voltage of about 2.2V.
If this is the case:
\$ V1 = V_{R1} + V_{LED} + V_{sat} = 5V = V_{R1} + 2.2V + 0.2V
=> V_{R1} = 3.6V
=> I_{R1} = I_{LED} = 3.6V / R1 =~ 16.3mA \$
You can verify this is in saturation, because the base current of 0.43mA is only amplified ~37.7x (instead of normally much higher, ~100). 16.2mA is a reasonable amount to drive an LED with as well.
Switch Edits below:
I would guess you have the DPST switch shown below. In this case you probably don't even need a transistor to get the desired switch action. You just need to figure out which part of the switch is normally connected and which is normally unconnected. You really only need these 2 terminals. See example circuit below:
simulate this circuit
Now when the switch is not pressed, the LED gets no current. When you push the switch it turns on. But, you might have fun with the NPN.
Best Answer
When you close the switch, you are applying 5.2 volts across the base/emitter junction, which normally doesn't like more than 0.7 volts - this will destroy the transistor.
One way to use a switch and transistor to contol an LED is:
simulate this circuit – Schematic created using CircuitLab
R2 will limit the base current when thet switch is closed. R3 pulls the base low when the switch is open, to ensure the transistor is not conducting.
Pressing the switch will provide base current through R2, allowing the transistor to conduct, drawing current through the LED and R1. R1 limits the LED current to a safe value.