Built-in project · no microcontroller

NPN transistor switch

A 2N2222 switching an LED from a slide switch through a 1 k base resistor, with real Ebers-Moll currents in the base and collector. There is no microcontroller in it: the simulator solves the 5 parts as a circuit, so it runs the moment you press Run, and nothing needs compiling.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

NPN transistor switchlive0.000 s 0.00x
Press Run, then click the slide switch
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

How it works

There is no microcontroller in this one, so nothing is compiled: the simulator solves the circuit itself. The file in the editor is the notes that come with it.

sketch.ino
// An NPN transistor switch.
//
// A 2N2222 switching an LED, with its base driven from a slide switch
// through a 1 k resistor. Slide the switch to 5 V and the LED comes on.
//
// Why the base resistor: the base-emitter junction is a diode. Put 5 V
// straight across it and it would take as much current as the supply can
// give. Through 1 k it takes about (5 - 0.7) / 1000 = 4.3 mA, and with a
// current gain well above 10 that is far more than the 13 mA or so the
// LED and its 220 ohm resistor ask for, so the transistor is driven hard
// into saturation: fully on, with only a small voltage from collector to
// emitter.
//
// Things to try:
//
//   - Press Shift+O with the switch on. The DC operating point labels every
//     net: about 0.7 V on the base, and a collector close to ground.
//   - Click the transistor and look at its currents in the properties panel.
//     The collector current divided by the base current is how hard it is
//     being driven, not its beta: a saturated switch runs far below beta.
//   - Change the base resistor to 100 k. Now the base gets about 43 uA, the
//     transistor can no longer pull the whole LED current, and the LED dims.
//     That is the transistor leaving saturation and acting as an amplifier.

Parts list

7 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • 5 V: Resistor, 220 Ω pin 1; Slide switch pin 3
  • Ground: Transistor (NPN or PNP) pin E; Slide switch pin 1
  • Resistor, 220 Ω pin 2; LED, red pin A
  • LED, red pin C; Transistor (NPN or PNP) pin C
  • Transistor (NPN or PNP) pin B; Resistor, 1k Ω pin 1
  • Resistor, 1k Ω pin 2; Slide switch pin 2

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.