Basic components: resistors, capacitors, diodes and transistors
Objectives
Section titled “Objectives”By the end of this lesson you will:
- Compute an LED’s current-limiting resistor from the supply voltage, the LED’s forward voltage, and the target current
- Explain what decoupling capacitors and flyback diodes do in a circuit driving an inductive load
- Choose a transistor or MOSFET as a switch when a load needs more current than a microcontroller pin can supply
Before you start
Section titled “Before you start”- You can already use Ohm’s law, series circuits and voltage dividers (the previous two lessons)
- For the lab: a leaded red LED, a 470 Ω resistor and a 1 kΩ resistor, a 100 kΩ resistor, a 100 µF electrolytic capacitor (rated at least 6.3 V), a stopwatch (a phone works), a multimeter, and a board
- A magnifying glass or a phone camera for looking closely at the board
Safety. Electrolytic capacitors are polarised — the longer lead is positive, and the side with a coloured stripe is negative. Connecting one backwards can make it heat up, bulge, or explode. This lesson only uses 3.3 V, but build the habit of checking polarity every time, starting now.
See it work first
Section titled “See it work first”Look closely at the board with a magnifying glass or a phone camera. Take a photo and zoom in.
- Around every chip there are several small brown or grey rectangular parts, sitting right next to the chip’s pins — most are capacitors
- Next to every LED there is usually one small resistor
Ask yourself two questions: why does every chip need its own capacitor sitting right next to its pins, and why must an LED always come paired with a resistor? This lesson answers both.
Concepts
Section titled “Concepts”1. The LED and its current-limiting resistor
Section titled “1. The LED and its current-limiting resistor”An LED is a kind of diode. When conducting, it has a fairly constant forward voltage (V_f), depending on its colour and part number. A red LED is usually around 1.8 to 2.2 V; blue, bright green and white are usually around 2.8 to 3.3 V. Always check the real value in the specific part’s datasheet.
The problem is that once the voltage exceeds V_f by even a little, the LED’s current shoots up very fast. So a resistor is needed to absorb the remaining voltage and set the current.
R = (V_s − V_f) / IExample: a 3.3 V supply, a red LED with V_f = 2.0 V, needing 5 mA
R = (3.3 − 2.0) V / 5 mA = 1.3 V / 5 mA = 260 ΩThe nearest standard value at or above this is 270 Ω → I = 1.3 V / 270 Ω = 4.81 mAPower in the resistor = 1.3 V × 4.81 mA = 6.26 mWAlways round up to the next standard value — the current comes out a little below target, which is the safer direction.
The trap with a blue LED at 3.3 V. With V_f = 3.0 V, only 0.3 V is left for the resistor. Choosing 150 Ω gives 2 mA in theory. But each LED’s real V_f varies by ±0.2 V, so the real current swings anywhere from 0.1 V / 150 Ω = 0.67 mA to 0.5 V / 150 Ω = 3.33 mA — a five-fold range. When headroom is small, brightness becomes uncontrollable. The fix is to feed the LED from a higher supply and use a transistor as a switch (section 3).
Microcontroller pins have limits too. Each pin can source or sink no more than the value stated in the chip’s datasheet, and the whole port together has a further combined limit. Before wiring an LED straight to a pin, always check the GPIO DC specifications table in the PSoC Edge E84’s datasheet (or whichever chip you are using) first.
2. Capacitors and diodes: the helpers that get forgotten
Section titled “2. Capacitors and diodes: the helpers that get forgotten”A capacitor (C) stores charge, Q = C × V. When it charges through a resistor, the voltage rises exponentially with a time constant τ = R × C.
Example: R = 10 kΩ, C = 100 nF, a 3.3 V supply
τ = 10 kΩ × 100 nF = 1 msAt t = 1τ (1 ms), voltage = 3.3 × (1 − e^−1) = 3.3 × 0.632 = 2.09 VAt t = 5τ (5 ms), it reaches about 99.3% — treated as fully charged in practiceDecoupling. When logic inside a chip switches state, it draws a short current spike. A long trace running from the voltage regulator has inductance, and cannot supply current that fast. Suppose a chip suddenly draws an extra 50 mA within 2 ns, through a trace with 10 nH of inductance (a reasonable assumed value).
V = L × di/dt = 10 nH × (50 mA / 2 ns) = 0.25 V a momentary dip of almost 8% of 3.3 VIf a 100 nF capacitor sits right next to the power pin, a 50 mA spike lasting 10 ns draws 50 mA × 10 ns = 0.5 nC of charge from that capacitor.
ΔV = Q / C = 0.5 nC / 100 nF = 5 mVOnly a 5 mV dip. This is exactly why every chip has its own decoupling capacitor, which must sit as close to the power pin as possible — because the trace between the capacitor and the pin has inductance too. A typical board has a small one (e.g. 100 nF) right next to every power pin, plus a few larger ones (e.g. 10 µF) for slower changes. We will come back to this in the lesson PCB and EMC basics.
A diode lets current flow one way only. A silicon diode drops about 0.6 to 0.7 V; a Schottky diode drops about 0.2 to 0.4 V.
A flyback diode matters for inductive loads such as a relay or motor coil, which will not let its current change instantly. When a switch cuts the current, the coil generates its own high voltage, following V = L × di/dt. Suppose a 100 mH coil carrying 50 mA is cut off within 1 µs.
V = 100 mH × (50 mA / 1 µs) = 5,000 V (in theory; in reality the voltage spikes until the transistor breaks down or a spark jumps first)A diode connected across the coil (cathode toward the positive supply side) gives the current somewhere to keep circulating, so the voltage at the switching point is limited to about the supply voltage plus 0.7 V. The current then decays with a time constant of L / R. If the coil’s own resistance is 100 Ω, that gives τ = 100 mH / 100 Ω = 1 ms.
3. Transistors and MOSFETs as switches
Section titled “3. Transistors and MOSFETs as switches”When a load needs more current or voltage than a microcontroller pin can give, have the pin “command” a switch instead, and let the switch connect the load to its own supply. The most common approach is a low-side switch, connected between the load and GND.
An NPN transistor (BJT) is controlled by base current. As a switch, we deliberately oversupply base current to drive it into saturation, usually assuming a “forced β” of around 10 to 20. A 5 V relay coil drawing 70 mA, with a forced β of 20, needs a base current of 70 / 20 = 3.5 mA. The base resistor = (3.3 − 0.7) V / 3.5 mA = 743 Ω; use 750 Ω. This works fine if the pin can easily supply 3.5 mA, but a 500 mA load would need tens of mA of base current, far more than a pin can provide.
An N-channel MOSFET is controlled by gate voltage (V_GS), draws almost no current while held on (only a brief pulse to charge the gate), and suits high-current loads. The thing to watch is V_GS(th) in the datasheet: this is the voltage where the MOSFET “just starts” conducting, not where it is fully on. Check that R_DS(on) is specified at a V_GS equal to or below what your pin can provide (e.g. 2.5 V for a 3.3 V pin). This kind of MOSFET is called logic-level.
Example: a 12 V LED strip drawing 500 mA, with a MOSFET whose R_DS(on) = 0.05 Ω at V_GS = 2.5 V
Voltage across the MOSFET = 0.5 A × 0.05 Ω = 25 mVPower in the MOSFET = (0.5 A)² × 0.05 Ω = 12.5 mW no heatsink neededAn NPN with the same load, at a forced β of 10, would need 50 mA of base current — far beyond a microcontroller pin.
| Load | Choice |
|---|---|
| Current within what the pin supplies, voltage within the pin’s own — e.g. a small LED | Direct from the pin, through a resistor |
| Current up to around 100 mA — e.g. a buzzer, a small relay | An NPN with a base resistor, or a small MOSFET |
| High current or a voltage higher than the board’s — e.g. a 12 V LED strip, a motor | A low-side, logic-level N-channel MOSFET |
| The load is a coil (relay, motor, solenoid) | Any of the above, plus a flyback diode |
Worked example
Section titled “Worked example”Problem: drive a 5 V relay (coil drawing 70 mA, coil resistance about 5 V / 70 mA = 71 Ω) from the board’s 3.3 V GPIO pin.
- Can the pin drive it directly? No — both the voltage (needs 5 V) and the current (70 mA) exceed what a GPIO pin should supply.
- Choose a switch. A logic-level, low-side N-channel MOSFET. Choose a part specifying R_DS(on) at V_GS = 2.5 V or lower; assume 0.1 Ω. Voltage drop = 70 mA × 0.1 Ω = 7 mV, and power = (70 mA)² × 0.1 Ω = 0.49 mW.
- A 100 Ω gate resistor limits the momentary charging surge to no more than 3.3 V / 100 Ω = 33 mA. If the gate capacitance is 500 pF (an assumed value — check C_iss in the datasheet), the time constant is 100 Ω × 500 pF = 50 ns, fast enough for a relay many times over.
- A 100 kΩ gate pull-down resistor. While the chip is resetting or has not yet configured the pin, it may float. This resistor keeps the MOSFET reliably off, drawing only 3.3 V / 100 kΩ = 33 µA while active.
- A flyback diode, such as a 1N4148, across the coil, cathode toward the 5 V side and anode toward the MOSFET’s drain side. A small signal diode like this is fine for a small relay whose coil current is only a few tens of mA.
- Decoupling for the 5 V rail: add 100 nF right next to the relay, plus one more 10 µF, because the relay draws a current pulse every time it switches.
Notice we have used every idea from this lesson: Ohm’s law (coil current, gate current), time constants (gate, coil), decoupling, and diodes.
Practice
Section titled “Practice”- A 5 V supply, a red LED with V_f = 2.0 V, needing 10 mA. Find R, the standard E12 value to use, the real current, and the power in the resistor.
- A 3.3 V supply, a white LED with V_f = 2.9 V, needing 2 mA. Find R. If the real LED has V_f = 3.1 V, what current results?
- R = 47 kΩ, C = 1 µF, a 3.3 V supply. Find τ, the time considered “fully charged” (5τ), and the voltage at t = τ.
- A chip draws a 200 mA spike lasting 5 ns from a decoupling capacitor. What is the voltage dip if the capacitor is (a) 100 nF, (b) 10 nF?
- A 12 V LED strip drawing 1 A is commanded from a 3.3 V GPIO pin. Which kind of switch should you choose? If R_DS(on) = 0.03 Ω, what is the power in the switch? Does it need a flyback diode?
- A 5 V relay with a 400 Ω coil is driven by an NPN with a forced β of 20. Find the coil current, the base current, and a standard E12 base resistor.
Solution
Section titled “Solution”- R = 3.0 V / 10 mA = 300 Ω. Use 330 Ω (the nearest E12 value above it). Current = 3.0 / 330 = 9.09 mA. Power = 3.0 V × 9.09 mA = 27.3 mW.
- R = 0.4 V / 2 mA = 200 Ω. If V_f = 3.1 V, the current becomes 0.2 V / 200 Ω = 1.0 mA — half of the design value. This is exactly the headroom problem.
- τ = 47 kΩ × 1 µF = 47 ms. Fully charged at 5τ = 235 ms. Voltage at t = τ is 3.3 × 0.632 = 2.09 V.
- Charge = 200 mA × 5 ns = 1 nC. (a) 1 nC / 100 nF = 10 mV (b) 1 nC / 10 nF = 100 mV. A capacitor ten times smaller gives a voltage dip ten times larger.
- A low-side, logic-level N-channel MOSFET. Power = (1 A)² × 0.03 Ω = 30 mW. An LED strip is a resistive load, not an inductive one, so it does not need a flyback diode.
- Coil current = 5 V / 400 Ω = 12.5 mA. Base current = 12.5 / 20 = 0.625 mA. Base resistor = (3.3 − 0.7) / 0.625 mA = 4.16 kΩ. The nearest E12 value below that is 3.9 kΩ (giving a base current of 0.67 mA, which guarantees saturation), and a flyback diode is still needed across the coil.
Check your understanding
Section titled “Check your understanding”Answer at least 4 of the 5 questions in quiz.yaml correctly, then try explaining to a friend, without looking at this page, what happens when a relay switches off if you remove the flyback diode from its circuit.
Part A: an LED and a resistor
- With USB unplugged, wire 3V3 → 470 Ω → a red LED (the longer lead is the anode, connect it to the resistor side) → GND.
- Guess: if V_f ≈ 2.0 V, the current should be (3.3 − 2.0) / 470 = 2.77 mA.
- Plug in USB, and measure the voltage across the resistor (V_R) and the voltage across the LED (V_f) with a multimeter. Check that V_R + V_f ≈ your measured 3V3 voltage.
- Compute the real current I = V_R / R (using the resistor value you measured while powered off), and compare against your guess.
- Change to 1 kΩ, and repeat. Notice the brightness, and record how much V_f changes as the current drops (usually a small drop, not perfectly constant as the formula assumes).
| R | V_R | V_f | I = V_R / R | Brightness (by eye) |
|---|---|---|---|---|
| 470 Ω | ||||
| 1 kΩ |
Part B: a time constant you can watch with your own eyes
- With USB unplugged, wire 3V3 → 100 kΩ → the positive lead of a 100 µF capacitor, negative lead to GND. Check polarity before applying power.
- Fully discharge the capacitor first, by touching a 1 kΩ resistor across its two leads for a few seconds.
- Guess: τ = 100 kΩ × 100 µF = 10 s. At 10 s, you should read about 63% of the final voltage.
- Place the meter leads across the capacitor first, then plug in USB and start the stopwatch at the same moment. Record the voltage every 5 s up to 60 s.
- Find the time at which the voltage reaches 63% of its final value — that is your measured τ.
Interpreting the results: the meter’s 10 MΩ input resistance sits in parallel with the capacitor, so the final voltage becomes 3.3 × 10 M / 10.1 M ≈ 3.27 V, and τ drops to about 9.9 s. This effect from the meter is small. What matters more is the electrolytic capacitor’s own tolerance, usually ±20%, so a measured τ anywhere from about 8 to 12 s is entirely normal. If you measure well outside that range, check the resistor’s value and the capacitor’s polarity.
Going further
Section titled “Going further”The next module moves into Logic levels and basic gates. We will look at which voltages a chip counts as 0 or 1, and why a board that has both a 1.8 V system and a 3.3 V system needs care when connecting external devices.
Reflect
Section titled “Reflect”Which component in this lesson had you overlooked before, when you saw it on a board — and how will you look at it differently now?
References
Section titled “References”- Lessons In Electric Circuits by Tony R. Kuphaldt (open book)
- AIoT in Action: examples/s03/03_led_brightness.py
- RC time constant (Wikipedia)
- Decoupling capacitor (Wikipedia)
- Flyback diode (Wikipedia)
Review questions
Answer on your own first, then open the answer.
-
A 3.3 V supply, an LED with V_f = 2.1 V, and a 4 mA target. What resistance do you calculate? (Objective 1)
- 300 Ω
- 825 Ω
- 525 Ω
- 3 Ω
Show answer
Answer: A. 300 Ω
R = (3.3 − 2.1) V / 4 mA = 1.2 V / 4 mA = 300 Ω ตัวต้านทานรับเฉพาะแรงดันส่วนที่เหลือจาก V_f ข้อ 825 Ω ลืมหัก V_f ออก
-
Why should you not connect an LED from a 3.3 V pin to GND without a resistor? (Objective 1)
- เพราะ LED จะไม่ติดเลย
- เพราะกระแสจะถูกจำกัดด้วยความต้านทานภายในของขาและ LED เท่านั้น อาจเกินพิกัดของทั้งสองอย่าง
- เพราะ LED จะกลับขั้วเอง
- ไม่มีปัญหา LED จำกัดกระแสได้เองเสมอ
Show answer
Answer: B. เพราะกระแสจะถูกจำกัดด้วยความต้านทานภายในของขาและ LED เท่านั้น อาจเกินพิกัดของทั้งสองอย่าง
เมื่อแรงดันเกิน V_f กระแสของ LED พุ่งขึ้นเร็วมาก ไม่มีอะไรกำหนดกระแสนอกจากความต้านทานภายในเล็ก ๆ ผลคือขาของชิปหรือ LED ทำงานเกินพิกัดและเสื่อมหรือพัง
-
What is the main job of a 100 nF decoupling capacitor placed right at a chip's power pin? (Objective 2)
- กรองสัญญาณข้อมูลที่ขา I/O
- จ่ายกระแสกระชากช่วงสั้น ๆ ให้ชิปจากตรงนั้นเลย ไม่ให้แรงดันไฟเลี้ยงตก
- เพิ่มแรงดันไฟเลี้ยงให้สูงกว่าแหล่งจ่าย
- ป้องกันการต่อไฟกลับขั้ว
Show answer
Answer: B. จ่ายกระแสกระชากช่วงสั้น ๆ ให้ชิปจากตรงนั้นเลย ไม่ให้แรงดันไฟเลี้ยงตก
ลายวงจรยาวจากตัวควบคุมแรงดันมีความเหนี่ยวนำ จ่ายกระแสเร็วไม่ทัน ตัวเก็บประจุที่อยู่ชิดขาจ่ายประจุให้แทน แรงดันจึงตกน้อย ยิ่งวางใกล้ยิ่งได้ผล
-
How must a flyback diode across a low-side-switched relay coil be oriented? (Objective 2)
- แคโทดทางฝั่งไฟบวก แอโนดทางฝั่งสวิตช์
- แอโนดทางฝั่งไฟบวก แคโทดทางฝั่งสวิตช์
- หันทางไหนก็ได้
- ต่อไดโอดอนุกรมกับขดลวดแทนการต่อคร่อม
Show answer
Answer: A. แคโทดทางฝั่งไฟบวก แอโนดทางฝั่งสวิตช์
ขณะทำงานปกติไดโอดต้องไม่นำไฟ (ถูกไบแอสกลับ) พอสวิตช์ตัด แรงดันที่ปลายฝั่งสวิตช์พุ่งสูงกว่าไฟบวก ไดโอดจึงนำไฟและให้กระแสไหลวนผ่านขดลวด ถ้าหันกลับ ไดโอดจะลัดวงจรไฟเลี้ยงทันทีที่เปิดสวิตช์
-
You must switch a 12 V, 800 mA motor from a 3.3 V GPIO. Which approach fits best? (Objective 3)
- ต่อมอเตอร์กับขา GPIO โดยตรง
- NPN ตัวเล็กกับตัวต้านทานเบส 330 Ω โดยไม่ต้องมีไดโอด
- MOSFET ชนิด N แบบ logic-level ต่อด้านล่าง พร้อมไดโอดกันไฟย้อนคร่อมมอเตอร์
- ต่อตัวต้านทาน 15 Ω อนุกรมกับมอเตอร์แล้วต่อเข้าขา GPIO
Show answer
Answer: C. MOSFET ชนิด N แบบ logic-level ต่อด้านล่าง พร้อมไดโอดกันไฟย้อนคร่อมมอเตอร์
ขาให้ได้ทั้งแรงดันและกระแสไม่พอ MOSFET แบบ logic-level เปิดได้เต็มที่ด้วย V_GS ต่ำและแทบไม่กินกระแสเกต ส่วนมอเตอร์เป็นขดลวดจึงต้องมีไดโอดกันไฟย้อน NPN จะต้องการกระแสเบสหลายสิบ mA ซึ่งเกินขา
Cite this lesson
If you teach from this lesson or reuse it in slides or documents, credit it with the text below. If you changed it, add (adapted) after the title.
"Basic components: resistors, capacitors, diodes and transistors" from TESA Open Knowledge by the Thai Embedded Systems Association (TESA), https://github.com/tesaiot/tesa-qualification-program, licensed under CC BY-NC 4.0
Thai attribution: "ชิ้นส่วนพื้นฐาน: ตัวต้านทาน ตัวเก็บประจุ ไดโอด และทรานซิสเตอร์" จาก TESA Open Knowledge โดยสมาคมสมองกลฝังตัวไทย (Thai Embedded Systems Association: TESA) https://github.com/tesaiot/tesa-qualification-program สัญญาอนุญาต CC BY-NC 4.0
TESA Open Knowledge · © 2026 สมาคมสมองกลฝังตัวไทย (TESA) · CC BY-NC 4.0
Content is licensed CC BY-NC 4.0. Reuse it non-commercially and credit the Thai Embedded Systems Association (TESA) every time. · How to cite TESA