🚀 C# ile Programlamaya Giriş: Kodlamaya İlk Adımını At, Kendi Projelerini Geliştirmeye Başla!
00HOME
07GLOSSARY 08TAGS ♡MY LIST ⌕SEARCH
TRENG

The transistor switch: driving an LED and a relay with an NPN

Switch large loads from a microcontroller's weak output with an NPN transistor. Calculate the base resistor, add a flyback diode across the relay coil and verify it in Proteus.

A microcontroller board such as an Arduino can supply at most a few tens of milliamps from a pin. But the things you want to drive in the field are usually bigger: a 12 V relay coil, an interposing relay that pulls in a contactor, an LED strip, a small fan. Between the microcontroller and these loads we put a transistor switch. A small base current turns a large collector current on and off. In this lesson you will drive first an LED, then a relay, with an NPN transistor.

Theory: cut-off and saturation

An NPN transistor has three pins: base (B), collector (C) and emitter (E). As a switch, we connect the emitter to ground and put the load between the collector and the supply. There are two states:

  • Cut-off (switch open): no base current. No collector current flows, the load is off.
  • Saturation (switch closed): enough base current flows. The collector-emitter path becomes almost a short circuit (about 0.1-0.3 V), and the load sees the full supply.

A transistor's current gain (hFE or beta) varies a lot between parts; for the BC547 the datasheet shows a range from below 100 up to several hundred. So in switch design we assume a low gain. Common practice: base current = collector current / 10. That drives the transistor firmly into saturation.

Base resistor: Rb = (Vdrive - Vbe) / Ib
Vbe about 0.7 V,  Ib = Ic / 10

Example 1 - LED: 5 V supply, LED 2 V, Ic = 20 mA, Vce(sat) = 0.2 V
Rc = (5 - 2 - 0.2) / 0.020 = 140 ohm  -> standard 150 ohm
Ib = 20 mA / 10 = 2 mA
Rb = (5 - 0.7) / 0.002 = 2150 ohm    -> next standard down 1.8 kOhm
Actual Ib = 4.3 / 1800 = 2.39 mA

For the base resistor we pick the next standard value down, because a smaller resistor means a bit more base current and guarantees saturation. For the LED resistor we picked the next value up. The direction depends on which side is safe.

A transistor driving an LED in Proteus

  1. Press P and add a BC547, two RES parts, an LED-RED and a BATTERY. For the input, add an animated switch (search for something like SWITCH in Pick Devices; the exact name may depend on the library).
  2. Set the battery to 5V. Battery positive → 150 ohm → LED anode; LED cathode → transistor collector. Connect the emitter to ground.
  3. Build the input branch: battery positive → switch → 1.8 kOhm → base. Put GROUND on the battery negative.
  4. Put a DC AMMETER (milliamps) in the collector branch and another DC AMMETER (milliamps) in the base branch. Connect a DC VOLTMETER between collector and emitter.
  5. Start the simulation. Click the arrows or the body of the switch to toggle it.
Switch open  : Ib = 0 mA,   Ic = 0 mA,     Vce = 5 V       (LED off)
Switch closed: Ib = 2.4 mA, Ic = about 19 mA, Vce = 0.1-0.2 V (LED on)

The collector current comes out around 18-20 mA, depending on the LED model's forward voltage. Notice: Ic is many times Ib, but not exactly "beta x Ib". The transistor is saturated; the collector resistor and the LED now set the current. That is exactly what we want from a switch.

Driving a relay and the flyback diode

A relay coil is an inductance. If you turn the transistor off while current flows through the coil, the coil resists the sudden change and produces a very high reverse voltage spike across its terminals (V = L x di/dt). That spike lands on the transistor's collector and can destroy it. The fix is a flyback (freewheeling) diode in reverse parallel with the coil: cathode to the +12 V side, anode to the collector side.

Example 2 - Relay: 12 V coil, coil resistance 360 ohm, 5 V drive
Ic = 12 / 360 = 33.3 mA
Ib = 33.3 / 10 = 3.33 mA
Rb = (5 - 0.7) / 0.00333 = 1291 ohm  -> 1 kOhm (Ib = 4.3 mA)
Diode: 1N4148 or 1N4007, reverse parallel with the coil
  1. Search for RELAY in Pick Devices and add one. Double-click it and set the coil voltage to 12V and the coil resistance to 360 ohm (property names may vary by version).
  2. Add a second battery of 12 V. 12 V positive → relay coil → BC547 collector; emitter to ground. The negatives of both batteries must meet at a common ground.
  3. Place a 1N4007 in parallel with the coil: cathode to the 12 V side, anode to the collector side.
  4. Put 1 kOhm in the base branch and connect it to the 5 V switch branch. If you wire a separate LED circuit through the relay contact, you can also see the relay pull in.
  5. Connect channel A of the oscilloscope to the collector and toggle the switch. Then delete the diode, try again and compare the spike on the collector.

Try it with the mini simulator

In the mini simulator below, change the drive voltage, the base resistor and the load current. Watch the base current fall as the base resistor grows, until at some point the transistor leaves saturation. Note the Rb value where the "Ib = Ic/10" rule breaks; then confirm in Proteus that Vce rises at the same value.

Common mistakes

Forgetting the base resistor: if you tie the base directly to an output, the base-emitter junction acts like a diode and draws a lot of current; both the microcontroller pin and the transistor suffer.

Fitting the flyback diode backwards: with the anode on +12 V the diode conducts all the time and shorts the coil; when the transistor turns on, excessive current flows.

Keeping the grounds separate: if the negatives of the 5 V and 12 V supplies are not joined, the base current has no return path and the transistor never turns on.

Exercise

An ESP32 board running on 3.3 V will drive an interposing relay with a 24 V coil. The coil resistance is 1600 ohm. Find the collector current, the base current and a standard base resistor; state the diode direction. Verify it in Proteus with a 3.3 V drive source.

Hint: the drive voltage is now 3.3 V, not 5 V; the top term in the Rb formula changes.

Solution

Ic = 24 / 1600 = 15 mA
Ib = 15 / 10  = 1.5 mA
Rb = (3.3 - 0.7) / 0.0015 = 1733 ohm  -> next standard down 1.5 kOhm
Actual Ib = 2.6 / 1500 = 1.73 mA
Diode: 1N4007, cathode to +24 V, anode to collector

In Proteus set the drive battery to 3.3V and the relay supply to 24V; set the relay coil to 24V and 1600 ohm. When you close the switch you will see a collector current of about 15 mA and Vce falling to around 0.2 V.

In short: when using a transistor as a switch take Ib = Ic/10, and never forget the flyback diode on an inductive load. In the next lesson you will learn about time and filtering with RC circuits.

// Mini simulator

Move the sliders; the circuit and the values update instantly. Build the same circuit in Proteus and compare.

// Spot the bug +10 XP

Exactly one line below is wrong. Click the faulty line.

// Mini quiz

Click an option; you will see right away, with the reason, whether it is correct or not.

1. A board with a 5 V output will drive a load needing Ic = 50 mA through an NPN. Using Ib = Ic/10, what base resistor do you need, approximately?
2. Which is the correct direction for a flyback diode across a relay coil?
3. In a saturated NPN switch, what mainly determines the collector current?

Mark it when you finish: your progress is saved and you earn +20 XP.

Tip: ← → keys let you move between lessons.