Transistors
Every digital chip — including the ESP32 and ATtiny85 — is built from billions of transistors. This project also uses one discrete transistor: the PN2222 NPN, which sits between the ATtiny's output pin and the active piezo buzzer in the backup circuit. This page explains what transistors are, how a BJT works as a switch, and derives the exact component values used in that circuit.
What Is a Transistor?
A transistor is a three-terminal semiconductor device that uses a small signal at one terminal to control a much larger current between the other two. There are two broad families:
- BJT (Bipolar Junction Transistor) — current-controlled. A small current into the control terminal causes a proportionally larger current to flow between the other two.
- MOSFET (Metal-Oxide-Semiconductor FET) — voltage-controlled. A voltage on the control terminal switches the current path. MOSFETs dominate in power electronics and are inside every logic chip.
This project uses a BJT. The PN2222 is an NPN BJT — the most common type for switching small loads from a logic-level output pin.
Anatomy of an NPN BJT
An NPN transistor has three terminals:
| Terminal | Symbol | Role |
|---|---|---|
| Base | B | Control input. A small current here switches the device. |
| Collector | C | The "high side" of the switch — connects to the load. |
| Emitter | E | The "low side" — typically connects to GND. |
VCC (load supply)
│
[Load] ← the thing you want to switch (buzzer, LED, relay…)
│
Collector (C)
│
GPIO ─[R]─ Base (B) ← small base current controls the switch
│
Emitter (E)
│
GND
Current flows from collector to emitter when the base is driven. No base current → no collector current. The emitter arrow on a schematic symbol points outward for NPN ("Never Points iN"), indicating conventional current flowing out of the emitter to GND.
How It Works as a Switch
BJTs have three operating regions. For digital switching you only need two of them:
Cutoff (switch open)
When no base current flows (), the transistor is off. The collector-emitter path is essentially an open circuit and no current flows through the load.
Condition: . Below this threshold the base-emitter junction does not conduct — exactly like the forward voltage of a diode.
Saturation (switch closed)
When enough base current flows, the transistor is fully on. The collector-emitter voltage drops to a small residual (V_CE(sat) ≈ 0.1–0.2 V), and the load sees nearly the full supply voltage. The transistor behaves as a closed switch.
Condition: is large enough that — the transistor is driven harder than strictly necessary, ensuring it stays fully on despite manufacturing variation in h_FE.
The middle region — active/linear — is where BJTs amplify analog signals. You do not use this region for digital switching. A transistor stuck in the active region when used as a switch is usually the result of an undersized base resistor.
Key Parameters
| Parameter | Symbol | PN2222 value | Meaning |
|---|---|---|---|
| Base-emitter voltage | V_BE | ~0.7 V | Threshold to turn on (like a diode Vf) |
| DC current gain | h_FE (or β) | 75–300 | Ratio I_C / I_B |
| Collector-emitter saturation voltage | V_CE(sat) | ~0.2 V | Voltage drop when fully on |
| Max collector current | I_C | 600 mA | Absolute maximum |
| Max collector-emitter voltage | V_CEO | 40 V | Absolute maximum |
h_FE varies widely. The datasheet gives a range (75 minimum, up to 300+) across temperature and process variation. Design for the minimum so the circuit works with any part from any batch — never assume the typical value.
The software analogy for h_FE: it is a compression ratio. Put 1 unit of base current in, get ≥75 units of collector current out. The base current is the small control signal; the collector current is the switched output.
Why the GPIO Cannot Drive the Load Directly
The ATtiny85 GPIO can source or sink a maximum of 40 mA, and should be run well below that for continuous use. An active piezo buzzer draws 30–50 mA.
Running the GPIO at its absolute maximum rating is a bad idea:
- Voltage droop: At 40 mA the pin voltage sags below the supply rail. The buzzer gets less voltage; the GPIO output may fall below valid logic levels for anything else on the pin.
- Thermal stress: Maximum-rated current causes internal heating that shortens the chip's life, particularly on a device intended to run for years on AA batteries.
- No headroom: Any variation in the buzzer (temperature, manufacturing tolerance) could push current above the limit.
The PN2222 separates the control path (GPIO → base, a few milliamps) from the power path (collector → buzzer, 30–50 mA). The GPIO never sees the buzzer current.
The software analogy: the GPIO is a userspace function with limited privileges; the transistor is a system call with access to the full supply rail.
Worked Example: The Buzzer Driver
The backup circuit uses:
- Supply: 3.0 V (2× AA alkaline batteries)
- Base resistor: ~1 kΩ (from DESIGN.md)
- Transistor: PN2222, h_FE(min) = 75
- Load: active piezo buzzer, 30–50 mA
Step 1 — Calculate base current
Step 2 — Calculate maximum switchable collector current
Step 3 — Check against load
The transistor is driven 3.4× harder than needed to conduct the full buzzer current. It is well into saturation — a firmly closed switch. The GPIO sources only 2.3 mA, well within the ATtiny's rating.
The full backup circuit (transistor, buzzer, ATtiny, DS3231 trigger) is shown in the Battery Backup and Power Management page.
Choosing the Base Resistor
The base resistor is the one value you must calculate when adding a transistor switch to a new circuit. The goal: drive the transistor deep into saturation without exceeding the GPIO's current rating.
Minimum base current to reach saturation:
Practical design target — apply a saturation factor S (typically 5–10×) to ensure robust switching across component variation:
For S = 5, I_C(load) = 50 mA, h_FE(min) = 75:
Base resistor value:
Both work. 1 kΩ gives I_B = 2.3 mA (3.4× margin); 680 Ω gives 3.4 mA (5.1× margin). Either is fine for a piezo buzzer — the difference is inaudible and both keep the GPIO well within its current rating.
Upper limit on base current: the GPIO's maximum output current (~40 mA). With R_base = 1 kΩ the base current is 2.3 mA — a comfortable 17× margin below the limit.
Package and Pinout (TO-92)
The PN2222 and PN2222 come in a TO-92 package: a small D-shaped black plastic body roughly 5 mm across with three leads.
Standard pinout when facing the flat side with leads pointing down:
┌─────────┐
│ PN2222 │ ← flat face toward you
└─┬──┬──┬─┘
E B C
Left: Emitter
Middle: Base
Right: Collector
Always verify against the datasheet. Some manufacturers use a different lead order for visually identical packages. A miswired transistor typically results in no switching action rather than component damage, but check before soldering.
A quick multimeter sanity check in diode mode: the base-emitter and base-collector junctions both behave as forward-biased diodes (~0.6–0.7 V). The emitter-collector path should not conduct in either direction.
MOSFETs: The Other Type
BJTs are current-controlled: current into the base switches the collector path. MOSFETs are voltage-controlled: a voltage on the gate switches the drain-source path, drawing essentially no gate current.
For switching small loads (< a few hundred mA) from a 3.3 V GPIO, both types work. MOSFETs are often preferred in new designs because:
- No sustained gate current → GPIO drives only the gate capacitance (negligible steady-state current)
- Lower on-resistance → less voltage dropped across the switch when conducting
- Easier to parallel for higher currents
The key constraint is the gate threshold voltage (V_th). Power MOSFETs often require 8–10 V on the gate to fully open — too high for a 3.3 V GPIO. Logic-level N-channel MOSFETs (e.g. 2N7000, BS170, IRLZ44N) have V_th of 1–2 V and turn on fully at 3.3 V. If you substitute one for the PN2222 here, connect the gate directly to the GPIO pin; add a small gate resistor (100 Ω) only if the wiring is long enough to ring.
For this project the PN2222 is the simpler choice: it needs only a standard resistor, costs less than $0.50, and is universally available in TO-92.
Summary
| Concept | Key takeaway |
|---|---|
| NPN BJT | Three terminals: Base (control), Collector (high side), Emitter (to GND) |
| V_BE | ~0.7 V to turn on; below that, transistor is fully off |
| h_FE | DC current gain: I_C = h_FE × I_B. Design for the minimum datasheet value. |
| Saturation | Fully on; V_CE ≈ 0.2 V. Requires I_B large enough that h_FE × I_B >> I_C(load). |
| Cutoff | Fully off; no base current, no collector current. |
| Why use one | GPIO current limits (~40 mA) cannot safely drive inductive or high-current loads directly. The transistor moves the load current off the GPIO. |
| Base resistor | R = (V_GPIO − 0.7 V) / I_B(target). Target I_B for 5–10× saturation margin. |
| TO-92 pinout | Facing flat side, leads down: E – B – C left to right. Verify with datasheet. |
| MOSFET alternative | Voltage-controlled, no gate current. Use logic-level types (2N7000, BS170) for 3.3 V switching. |