Transistor (BJT)
A bipolar transistor is a current-controlled switch. A trickle into the base lets a much larger current flow from the collector to the emitter — or, for a PNP part, the other way round. It costs $0.30 and is how a 3.3 V GPIO gets to switch a 5 V or 12 V load that needs more current than a pin can give: the pin drives the base through a resistor, and the load hangs off the collector. Unlike a relay it has no moving parts, so it can switch thousands of times a second — but it drops a little voltage while on, and it cannot isolate the load from the Pi the way a relay's contacts do.
Pins
| Pin | What it is | Type |
|---|---|---|
| C | Collector | passive |
| B | Base | passive |
| E | Emitter | passive |
| Setting | Type | Range | Default |
|---|---|---|---|
model | enum | 2N2222A, 2N3904, 2N3906, BC547B, BC557B, SS8550, TIP41 | 2N2222A |
Transistor("2N2222A")— Transistor (BJT) — $0.30- Pins: 3
- Aliases:
collector→C,base→B,emitter→E
E is the emitter, B the base and C the collector, in the TO-92 order E–B–C facing the flat side. The Part number picks a real device: polarity, gain and limits all come from it. NPN parts (2N2222A, 2N3904, BC547B) switch the low side — emitter to ground, load between the supply and the collector. PNP parts (2N3906, BC557B, SS8550) switch the high side.
Wiring it in circuit.py
An NPN switching an LED from 3V3, driven from GPIO17:
from wiring import *
pi = Pi()
q1 = Transistor("2N2222A") # NPN: emitter to ground, load above the collector
r1 = Resistor(1000) # the base is a diode: always through a resistor
r2 = Resistor(330)
led = LED("red")
pi.GPIO17 >> r1 >> q1.base
q1.emitter >> pi.GND
pi.V3_3 >> r2 >> led.anode
led.cathode >> q1.collector # the transistor sinks the LED currentThe base-emitter junction behaves exactly like a diode: without r1, the GPIO would try to drive 0.7 V into a short and source far more than its safe 16 mA. With 1 kΩ, the base takes about 2.6 mA — and that is enough to sink tens of milliamps through the collector.
Driving it in main.py
from gpiozero import OutputDevice
from time import sleep
switch = OutputDevice(17) # NPN: high = conducting
while True:
switch.on()
sleep(0.5)
switch.off()
sleep(0.5)Electrical notes
- The base-emitter drop is about 0.7 V while conducting. Base current is (Vpin − 0.7) ÷ R, so 1 kΩ from 3.3 V gives ~2.6 mA.
- Collector current is the base current times the gain (β, hundreds for these parts) — until the load stops it: in saturation the transistor drops only a few tens of millivolts, which is why it can sink more than the base current alone would suggest.
- The models are the SPICE Gummel-Poon cards for each part number: gain, Early effect and high-current roll-off are all there. Junction capacitances and base resistance are not modelled yet, so switching edges are idealised.
- Over its collector-current, power or voltage rating the part warns, and with the feed's On overload set to
burn outit fails open for the rest of the run — the same convention as the LED. - A Darlington like the TIP120 is two transistors with resistors inside; it arrives as a composite, not as one of these models.
Simulator vs real hardware
- Real TO-92 pinouts vary between part numbers; the simulator wires by pin name (
q1.base), so the drawing is a guide, not a datasheet. - On a breadboard the base resistor is the part you will get wrong first: too small and the GPIO is overloaded, too large and the transistor stays half-on and gets hot. The simulator's pin-damage and overload warnings catch both.