NPN transistor switch
A 2N2222 switching an LED from a slide switch through a 1 k base resistor, with real Gummel-Poon 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 About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $5.45. On Mokxi they're free.
What you'll learn
- A transistor as a switch
- Base, collector and emitter
- Why the base needs a resistor
- Saturation: fully on versus amplifying
- Difficulty: Beginner
- Time: About 10 minutes
- Parts: 7
How it works
An NPN transistor switches a red LED. The LED and its 220 ohm resistor run from 5 V to the transistor's collector, and the emitter goes to ground.
A slide switch sends 5 V or ground to the base through a 1k resistor. At 5 V, about (5 - 0.7) / 1000, or 4.3 mA, flows into the base. That is far more than the LED needs, so the transistor turns fully on and the LED lights.
The base resistor is needed because the base-emitter junction acts like a diode. Without it, 5 V straight across would draw as much current as the supply could give.
Parts list
7 parts, plus the jumper wires. They retail for about $5.45; on Mokxi they're free.
- 1 × Full-size breadboard
- 1 × Power, 5 V
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Transistor (NPN or PNP)In LED Cube Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Slide switchIn Logic Lab Kit, Spaceship Panel Kit, 555 Timer Kit.
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 220 Ω pin 1 | 5 V |
| Slide switch pin 3 | 5 V |
| Transistor (NPN or PNP) pin E | Ground |
| Slide switch pin 1 | Ground |
| LED, red pin A | Resistor, 220 Ω pin 2 |
| Transistor (NPN or PNP) pin C | LED, red pin C |
| Resistor, 1k Ω pin 1 | Transistor (NPN or PNP) pin B |
| Slide switch pin 2 | Resistor, 1k Ω pin 2 |
The notes that come with it
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.
// 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.
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED never lights. This project
Check the LED cathode goes to the collector, the emitter goes to ground, and the base goes to the switch through the 1k resistor. - The LED is dim after changing the base resistor. This project
A very large base resistor, like 100k, gives too little base current. The transistor leaves saturation and the LED dims. Go back to 1k. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - The load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong. - The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.