# Simple circuit with transistor using Raspberry Pi

I'm trying to use a Raspberry Pi to control a set of 4 LEDs. I'd like to control the LEDs in software by using a GPIO pin. Here is the schematic I've come up with... (please ignore the wire in the top right corner of the diagram, the transistor should make or break the circuit) I'll be using a 9V battery to power 4 LEDs in series. Each LED has a voltage drop of 2V and a current rating of 20mA. Therefore my resistor should have the value of 50 Ohms (1V / 20 x 10^-3A). The diagram has an incorrect resistor value shown.

My problem is that I'm not sure of what will happen when I connect the Pi's pin to the base of the transistor. Will it mean that the pin becomes connected to the 9V circuit and will draw 20mA after I set the pin to high? Or does the pins voltage have its own influence? (the pin operates at 3.3V).

There is probably a better way to do this but I'm trying to learn about transistors so I don't want to remove the transistor.

• Do you understand that the collector and the emitter are shorted on the schematic? – Eugene Sh. Aug 9 '16 at 19:16
• @EugeneSh. Yes, I noted in my question that the wire in the top right corner is erroneous and should be ignored – CS Student Aug 9 '16 at 19:29
• And the reason you're not spending the same amount of energy on properly drawing your intentions as you expect people here to put into answering, is what? – Asmyldof Aug 9 '16 at 22:28

This is how you should connect the NPN transistor: R_GPIO must limit base current to about ten times LED current divided by transistor gain. Assuming a gain of 150 at the specified collector current:

$$R_{GPIO} = {{3.3 V - 0.7 V} \over {10 \cdot {20 mA \over 150}}} \approx 2 k$$

• Does the formula you have used have a name? I've never seen it before and would like to know more about it – CS Student Aug 9 '16 at 21:50
• The formula is just a slightly disguised form of Ohm's Law: R = V/I. The voltage across the base resistor is the GPIO voltage minus the transistor's base-emitter voltage. The required base current to put the transistor in saturation is 10 times [LED current over the transistor's gain]. – Peter Bennett Aug 10 '16 at 5:33

This circuit would not work, even if the collector and emitter were not shorted. Assuming around a 2V voltage drop across each diode when they're on, the emitter would thus be sitting at about 8V -- even if the resistor were shorted out. The base needs to be about 0.7V higher than then the emitter for the transistor to stay on -- about 8.7V. The Pi has 3.3V outputs, and thus isn't big enough to keep the transistor turned on

• Ah I wasn't aware of the 0.7V difference needed, I just thought some kind of current was needed to trigger the transistor but didn't know any exact values. Is there any way for me to somehow get the 3.3V output and step it up to the 8.7V trigger needed? – CS Student Aug 9 '16 at 19:31
• @CSStudent Yes, Peter Bennet posted the right way, which is to put the led's on the high side. – Scott Seidman Aug 9 '16 at 20:27

A correct way to do this is: simulate this circuit – Schematic created using CircuitLab

In an NPN transistor, the voltage between emitter and base will never be more than about 0.7 volts. In your circuit, if a High from the Pi is 3.3 volts, that would leave the emitter at 2.6 volts - not enough to turn on the LEDs.

My way, the collector/emitter voltage should be about 0.3 volts when the Pi output is High. The base resistor limits the current drawn from the Pi output when it is High.

• Second-to-last paragraph doesn't follow logically - the voltage can't be more than 0.7 volts, but you didn't say it can't be less - if it was -5.7V or less then that would be enough for the LEDs to light. – immibis Aug 9 '16 at 21:01
• Unfortunately I'm quite confused by your answer. When you say the collector/emitter should be at around 0.3V have you calculated that by subtracting the voltages across the LEDs and resistor from the batteries 9 volts? Also why is the transistor connected to both grounds? I'm unsure as to what happens at the point the emitter pin contacts the battery and Pi ground wire (what happens to the voltage / current?) – CS Student Aug 9 '16 at 21:48
• @CSStudent -- minimum Vce gets approached as collection current grows. It is a property of transistors, and the number is listed in the data sheet. Generally, though, it is around 0.3. All you need to do is drive the transistor hard enough to get it there – Scott Seidman Aug 9 '16 at 22:21
• @immibis In order to get a -5.7V, you would need to connect the +9V terminal to the +3.3V net of the RPi, and you could certainly light up the LEDs that way.... but you wouldn't be able to turn them off. – W5VO Aug 10 '16 at 4:12
• @W5VO I didn't say it wasn't true; I said it didn't follow logically. (And actually, if you did that, you would be able to turn them mostly off by applying logic low = 5.7V to the base) – immibis Aug 10 '16 at 4:57