Skip to main content

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:

TerminalSymbolRole
BaseBControl input. A small current here switches the device.
CollectorCThe "high side" of the switch — connects to the load.
EmitterEThe "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 (IB=0I_B = 0), the transistor is off. The collector-emitter path is essentially an open circuit and no current flows through the load.

Condition: VBE<0.6 VV_{\text{BE}} < {\sim}0.6\ \text{V}. 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: IBI_B is large enough that hFE×IB>IC(load)h_{\text{FE}} \times I_B > I_{C(\text{load})} — 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

ParameterSymbolPN2222 valueMeaning
Base-emitter voltageV_BE~0.7 VThreshold to turn on (like a diode Vf)
DC current gainh_FE (or β)75–300Ratio I_C / I_B
Collector-emitter saturation voltageV_CE(sat)~0.2 VVoltage drop when fully on
Max collector currentI_C600 mAAbsolute maximum
Max collector-emitter voltageV_CEO40 VAbsolute 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:

  1. 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.
  2. 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.
  3. 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

Vsupply=3.0 V,VBE=0.7 V,Rbase=1000 ΩIB=(VsupplyVBE)/Rbase=(3.00.7)/1000=2.3 mAV_{\text{supply}} = 3.0\ \text{V}, \quad V_{\text{BE}} = 0.7\ \text{V}, \quad R_{\text{base}} = 1000\ \Omega \\[6pt] I_B = (V_{\text{supply}} - V_{\text{BE}}) / R_{\text{base}} \\ = (3.0 - 0.7) / 1000 \\ = 2.3\ \text{mA}

Step 2 — Calculate maximum switchable collector current

IC(max)=hFE(min)×IB=75×2.3 mA=172 mAI_{C(\text{max})} = h_{\text{FE(min)}} \times I_B \\ = 75 \times 2.3\ \text{mA} \\ = 172\ \text{mA}

Step 3 — Check against load

IC(load)=50 mA,IC(max)=172 mASaturation margin=172/503.4×I_{C(\text{load})} = 50\ \text{mA}, \quad I_{C(\text{max})} = 172\ \text{mA} \\[6pt] \text{Saturation margin} = 172 / 50 \approx 3.4\times

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:

IB(min)=IC(load)/hFE(min)I_{B(\text{min})} = I_{C(\text{load})} / h_{\text{FE(min)}}

Practical design target — apply a saturation factor S (typically 5–10×) to ensure robust switching across component variation:

IB(target)=S×IC(load)/hFE(min)I_{B(\text{target})} = S \times I_{C(\text{load})} / h_{\text{FE(min)}}

For S = 5, I_C(load) = 50 mA, h_FE(min) = 75:

IB(target)=5×50 mA/75=3.3 mAI_{B(\text{target})} = 5 \times 50\ \text{mA} / 75 = 3.3\ \text{mA}

Base resistor value:

Rbase=(VGPIOVBE)/IB(target)=(3.00.7)/0.0033=697 Ωnearest standard: 680 Ω or 1 kΩR_{\text{base}} = (V_{\text{GPIO}} - V_{\text{BE}}) / I_{B(\text{target})} \\ = (3.0 - 0.7) / 0.0033 \\ = 697\ \Omega \to \text{nearest standard: } 680\ \Omega \text{ or } 1\ \text{k}\Omega

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

ConceptKey takeaway
NPN BJTThree terminals: Base (control), Collector (high side), Emitter (to GND)
V_BE~0.7 V to turn on; below that, transistor is fully off
h_FEDC current gain: I_C = h_FE × I_B. Design for the minimum datasheet value.
SaturationFully on; V_CE ≈ 0.2 V. Requires I_B large enough that h_FE × I_B >> I_C(load).
CutoffFully off; no base current, no collector current.
Why use oneGPIO current limits (~40 mA) cannot safely drive inductive or high-current loads directly. The transistor moves the load current off the GPIO.
Base resistorR = (V_GPIO − 0.7 V) / I_B(target). Target I_B for 5–10× saturation margin.
TO-92 pinoutFacing flat side, leads down: E – B – C left to right. Verify with datasheet.
MOSFET alternativeVoltage-controlled, no gate current. Use logic-level types (2N7000, BS170) for 3.3 V switching.