8 Electronics: Power and Transistor Switching
8.1 Start With the Decision
A GPIO pin can request that a valve open, but it cannot supply the valve. A transistor keeps control and load power apart.
8.2 Route Overview
This is part 1 of 2. Continue with Electronics: Controller Design and Review.
8.3 Part Objectives
- Calculate base or gate drive for a switched load.
- Choose protection for inductive current and supply faults.
8.4 Start Simple
8.4.1 Follow the Safe Power Path
A microcontroller is a small computer built to read inputs and control outputs. GPIO means general-purpose input/output and names the pins used for many of those jobs. An actuator is a part that turns an electrical command into a physical change.
Picture a plant monitor that must open a water valve. The control pin can request the action, but it cannot safely supply the valve’s power. A driver must carry the load current. A protection path must handle the energy left when the valve turns off.
Draw two paths before choosing parts. Trace the small control signal from the pin to the driver. Then trace load power from the supply through the driver and valve. Check voltage, current, heat, startup demand, common reference, and the safe state after a reset or broken wire.
This path does not explain every semiconductor or switching detail. It prevents the first dangerous mistake: treating a logic pin as a power source. The deeper sections show how diodes, transistors, ratings, and measured evidence turn the sketch into a safe circuit.
Imagine a GPIO pin asked to light an LED, read a switch, and control a small load. Electronics is the set of parts that keeps those jobs within safe voltage, current, timing, and protection limits. Start with the pin limit, the load need, and the component that makes the two compatible.
8.5 Learning Objectives
- Explain how semiconductors, diodes, and transistors form the foundation of all IoT device hardware
- Distinguish between conductors, insulators, and semiconductors and describe how doping creates N-type and P-type materials
- Compare BJT and MOSFET transistors and determine which is appropriate for a given IoT switching or amplification task
- Analyze transistor switching circuits for controlling high-power loads such as motors, relays, and LED strips from microcontroller GPIO pins
- Semiconductor: A material whose conductivity can be engineered; silicon becomes useful for electronics because doping changes how charge moves through it.
- Diode: A one-way device built from a PN junction; used for rectification, protection, LEDs, and the internal building blocks of more complex devices.
- Transistor: A controllable semiconductor switch or amplifier; BJTs use base current, while MOSFETs use gate voltage.
- Logic-level MOSFET: The default IoT power-switching device when a microcontroller must control relays, motors, solenoids, or LED strips without overstressing GPIO pins.
- Driver circuit: The interface between a low-power controller and a higher-power load; typically includes a transistor, resistor network, and protection components such as a flyback diode.
- Datasheet-first design: Good electronics decisions come from checking voltage, current, power, and switching requirements against the component datasheet instead of relying on generic part names.
8.6 Overview
This book is the bridge between basic electricity and practical IoT hardware design. It explains what semiconductors are, how diodes and transistors behave, and how to choose safe switching circuits when a microcontroller needs to control real loads.
Use this page as a guide, not as the main teaching chapter. The detailed explanations, examples, and checks live in the five linked sections below.
- Start Simple
- In 60 Seconds
- Key Concepts
- Overview
- Chapter Navigation
- Learning Path Recommendation
- How To Use This Book
- Checkpoint: Module Map
- Smart Light from Silicon
- Why This Chapter Matters for IoT
- Prerequisites
- Transistor Switching Examples
- Deep dive: LED switch numbers
8.8 Learning Path Recommendation
| Your Background | Start Here | Then Read |
|---|---|---|
| Complete beginner | Introduction → Conductors & Insulators | Practice with calculators, then move to semiconductors |
| Some electronics knowledge | Semiconductors & Doping | Deep dive into theory, then apply with transistor selection |
| Need practical guidance | Transistor Selection Guide | Jump to component selection, refer back to theory as needed |
| Quick reference | Summary & Resources | Use visual galleries and component tables |
8.9 How To Use This Book
- If you are new to electronics, do not treat the five sections as interchangeable. Read them in order.
- Read Electronics Introduction and Calculators first if you are new to hardware design.
- Read Conductors, Insulators, and Semiconductors before Semiconductors, Doping, and Diodes; the later chapter assumes you already understand the material categories and basic component vocabulary.
- Treat Transistor Selection Guide for IoT as the practical decision chapter. Return to it whenever you need to drive a real load from a GPIO pin.
- Use Electronics Summary and Resources as a capstone and review page, not as the main entry point.
- On a first pass, aim to understand what each component does and when it is used. Leave detailed selection tradeoffs and reference material for the second pass.
Checkpoint: Module Map
You now know:
- This overview points to five linked sections, from beginner introductions through intermediate transistor selection.
- The sequence starts with electricity, conductors, and semiconductors before it asks you to make switching decisions.
- The practical target is safe GPIO-to-load design: respect the 3.3V signal level, current limits, and component ratings.
8.10 Smart Light from Silicon
From Raw Silicon to a Smart LED Light
Let’s trace how electronics principles build a complete IoT device - a Wi-Fi-controlled LED light.
Step 1: Starting Material - Pure Silicon We begin with ultra-pure silicon (99.9999999% pure - “nine nines”). Silicon has 4 valence electrons, making it a semiconductor - not a great conductor, but not an insulator either.
Step 2: Doping Creates N-type and P-type Engineers add tiny amounts of impurities:
- Phosphorus (5 valence electrons) → N-type silicon (extra electrons available)
- Boron (3 valence electrons) → P-type silicon (missing electrons, creating “holes”)
Step 3: PN Junction = Diode Place N-type and P-type together → PN junction → a diode forms. Conventional current flows one way (from P-side anode to N-side cathode) but not the reverse. This becomes our power rectifier to convert AC wall power to DC.
Step 4: Three Layers = Transistor Add a third layer:
- NPN transistor: N-type, thin P-type, N-type
- Controls current flow with a tiny gate/base signal
- Millions of these make our ESP32 microcontroller
Step 5: Building the Smart Light Circuit
| Stage | What It Does |
|---|---|
| Wall AC | Supplies energy to the device |
| Diode rectifier | Converts AC into pulsing DC |
| Voltage regulator | Produces safe DC rails for electronics |
| Battery backup | Keeps the device alive during short outages |
| ESP32 and Wi-Fi module | Process the control logic and communicate wirelessly |
| GPIO output | Sends a low-power control signal |
| MOSFET | Uses that small GPIO signal to switch the LED load |
| LED + resistor | Produces light while the resistor limits current |
Component Breakdown:
- Diode bridge: 4 PN junctions convert AC to pulsing DC
- Capacitor: Smooths pulsing DC to steady DC
- Voltage regulator (contains many transistors): Drops 12V to 3.3V for ESP32
- ESP32 microcontroller: Millions of transistors executing code
- MOSFET driver (N-channel, 3 layers): Switches LED on/off from GPIO signal
- LED (another diode!): Emits light when forward-biased
- Current-limiting resistor: Protects LED from overcurrent
The Electronics Magic:
- Diodes ensure current only flows the correct direction
- Transistors in the ESP32 store your Wi-Fi settings in SRAM cells (each made of 6 transistors)
- MOSFET switches the high-current LED (1A) using only the 3.3V GPIO voltage signal — essentially zero current drawn from the GPIO pin!
- Capacitors smooth voltage ripples from the power supply
All from Doping Silicon: Every component except resistors and capacitors is made from doped silicon forming PN or NPN/PNP junctions. The entire difference between a diode, BJT, MOSFET, and microcontroller is just the arrangement and doping profile of silicon layers!
Key Insight: Electronics is applied semiconductor physics. Understanding PN junctions, doping, and transistor operation explains how millions of components work together to create intelligent devices from inert silicon.
The smart-light story turns the navigation list into a single chain: material choice creates components, components create circuits, and circuits let firmware affect the physical world. The next question is why that chain matters so much in IoT work.
8.11 Why This Chapter Matters for IoT
Critical Skills for IoT Development
Every IoT device relies on electronics:
- Microcontrollers contain millions of transistors
- Sensors require proper signal conditioning circuits
- Actuators need transistor-based switching circuits
- Power management depends on voltage regulators and MOSFETs
- Wireless modules use RF electronics
Without understanding electronics, you cannot:
- Interface sensors safely (risk of damage)
- Control high-power loads (motors, relays, LED strips)
- Optimize battery life (inefficient power circuits)
- Debug hardware issues effectively
8.12 Prerequisites
Before starting, you should understand:
- Electricity Fundamentals: Voltage, current, resistance, Ohm’s Law
- Atomic Structure: Atoms, electrons, protons, electron shells
- Circuit Analysis: Reading circuit diagrams, Kirchhoff’s laws
8.13 Transistor Switching Examples
Controlling Loads with Transistors
The examples below move from a small LED to a relay and then to a PWM motor. Read them as escalating design checks: current first, then voltage, then heat and inductive protection.
8.13.1 LED Switching with NPN BJT
Scenario: Use an ESP32 GPIO pin (3.3V, max 12mA) to control a high-brightness LED that requires 100mA.
Components:
- NPN transistor: 2N2222 (β = 100-300)
- LED: Forward voltage = 2.0V, forward current = 100mA
- Power supply: 5V
Wiring map:
| Node | Connects To | Purpose |
|---|---|---|
| +5V supply | LED and current-limiting resistor path | Provides LED power |
| LED/resistor negative side | 2N2222 collector | Lets the transistor switch the LED current |
| 2N2222 emitter | GND | Completes the low-side current path |
| ESP32 GPIO (3.3V) | 1kΩ resistor, then 2N2222 base | Provides the small base current that turns the transistor ON |
| LED current path | About 100mA when ON | GPIO controls a larger current safely through the transistor |
Calculations:
Using Ohm’s Law , with voltage across resistor = 5V - 2V (LED) - 0.2V (Vce(sat)) = 2.8V and desired current 100 mA: . Worked example: With a 33Ω standard resistor, actual current is , which is safe (below 100 mA max) and provides sufficient brightness. Note: The simplified calculation ignoring Vce(sat) gives 30Ω, which is also acceptable since a slightly higher resistance just means slightly less LED current.
-
LED resistor (if needed above LED):
- Voltage across resistor: 5V - 2V (LED drop) - 0.2V (Vce(sat)) = 2.8V
- Current: 100mA
- Resistor: R = 2.8V / 0.1A = 28Ω (use 33Ω standard for safety margin)
-
Base resistor:
- Required collector current: Ic = 100mA
- Transistor gain (minimum): β = 100
- Required base current: Ib = Ic / β = 100mA / 100 = 1mA
- Add safety factor (2x): Ib = 2mA
- Base voltage drop: Vbe = 0.7V
- Resistor: R = (3.3V - 0.7V) / 2mA = 2.6V / 0.002A = 1.3kΩ (use 1kΩ)
Result: GPIO high → 2mA into base → 100mA through collector → LED lights at full brightness (OK)
8.13.2 MOSFET Relay Switching
Scenario: Control a 12V relay (coil resistance = 400Ω, current = 30mA) to switch 240V AC mains power to a lamp.
Components:
- N-channel MOSFET: 2N7000 (logic-level, Vgs(th) = 0.8-3.0V, typical 2.1V)
- Relay: 12V coil, SPDT contacts rated 10A @ 250VAC
- Flyback diode: 1N4001 (protects MOSFET from inductive kick)
Wiring map:
| Node | Connects To | Purpose |
|---|---|---|
| +12V supply | Relay coil positive side | Provides coil power |
| Relay coil negative side | 2N7000 drain | MOSFET switches the coil current |
| 2N7000 source | GND | Low-side switch reference |
| ESP32 GPIO (3.3V) | 100Ω resistor, then MOSFET gate | Controls the relay |
| MOSFET gate | 10kΩ pull-down to GND | Keeps relay OFF during boot/reset |
| 1N4001 flyback diode | Across relay coil, cathode to +12V | Protects MOSFET from inductive voltage spikes |
Why MOSFET instead of BJT?:
- MOSFET gate current: ~0µA (vs 2mA for BJT)
- Lower power dissipation: Rds(on) = 5Ω → P = I²R = (0.03)² × 5 = 4.5mW
- BJT would dissipate: Vce(sat) × Ic = 0.2V × 30mA = 6mW (comparable, but BJT wastes GPIO current)
Flyback Protection: When relay coil de-energizes, collapsing magnetic field induces voltage spike (can reach 100V+). Diode provides discharge path: spike energy dissipates through diode instead of destroying MOSFET.
Calculations:
- Relay current: I = V / R = 12V / 400Ω = 30mA
- MOSFET Vgs = 3.3V > Vgs(th) typical 2.1V → on (OK; verify Vgs(th) max for your specific device, because worst-case 2N7000 can have Vgs(th) up to 3.0V, leaving minimal margin at 3.3V)
- Gate resistor: 100Ω limits inrush current when GPIO goes high (prevents ringing)
- Pull-down: 10kΩ ensures gate stays at GND when GPIO is floating (prevents spurious relay activation)
Result: GPIO high → MOSFET conducts → relay energizes → mains lamp turns on (OK)
8.13.3 PWM Motor with Power MOSFET
Scenario: Variable-speed control of a 12V DC motor (stall current: 2A, running current: 500mA) using PWM from ESP32.
Components:
- N-channel Power MOSFET: IRLZ44N (logic-level, Rds(on) = 0.022Ω @ Vgs=10V, ~0.028Ω @ Vgs=5V, Id(max) = 47A)
- Motor: 12V, 500mA running, 2A stall
- Flyback diode: 1N5819 Schottky (low forward voltage, fast recovery)
- Gate driver: Optional, but improves switching speed
Wiring map:
| Node | Connects To | Purpose |
|---|---|---|
| +12V supply | DC motor positive side | Provides motor power |
| DC motor negative side | IRLZ44N drain | MOSFET switches motor current |
| IRLZ44N source | GND | Low-side switch reference |
| ESP32 PWM pin | 100Ω resistor, then MOSFET gate | Controls motor speed by PWM |
| MOSFET gate | 10kΩ pull-down to GND | Keeps motor OFF during boot/reset |
| 1N5819 Schottky diode | Across motor, cathode to +12V | Handles motor flyback current during switching |
Advanced Consideration: MOSFET Switching Losses
At PWM frequency of 1 kHz (ESP32 default):
- Conduction loss (when ON): P = I² × Rds(on) = (0.5)² × 0.022 = 5.5mW (using Rds(on) at Vgs=10V; at Vgs=3.3V expect ~0.05Ω, giving ~12.5mW — still negligible)
- Switching loss (transitions): Depends on gate charge (Qg) and switching time
Gate Charge Calculation:
- IRLZ44N gate charge: Qg = 63nC (from datasheet)
- PWM frequency: f = 1 kHz
- Gate drive current (average): I = Q × f = 63nC × 1000 = 63µA (negligible!)
- Switching loss (simplified): P ≈ Qg × Vgs × f = 63nC × 3.3V × 1000 = 0.2mW
Total Power Dissipation: 5.5mW + 0.2mW = 5.7mW → no heatsink needed
If using higher PWM frequency (20 kHz for quieter operation):
- Switching loss: 63nC × 3.3V × 20,000 = 4.2mW
- Total: 5.5mW + 4.2mW = 9.7mW → still OK without heatsink
Motor Characteristics:
- PWM duty cycle 50% → average voltage 6V → motor runs at ~50% speed
- Flyback diode handles inductive kick when motor current changes
- Schottky diode chosen for fast recovery (important at high PWM frequency)
Advanced Protection:
- Add 0.1µF capacitor across motor terminals (suppresses EMI/RFI noise)
- Add 100nF ceramic cap from Vgs to GND (stabilizes gate voltage during fast switching)
Result: ESP32 PWM (0-100% duty) → MOSFET switches 500mA → motor speed varies smoothly. Power loss in MOSFET is minimal due to ultra-low Rds(on) (OK)
Key Differences from Beginner/Intermediate:
- Power MOSFET handles 2A vs 30mA (relay) or 100mA (LED)
- Switching losses become significant at high frequency
- EMI suppression needed for motor commutation noise
- Schottky diode for faster recovery than standard 1N4001
- Heat dissipation calculated to verify no heatsink needed
Design Insight: For loads >500mA, always calculate power dissipation. If P > 500mW, add heatsink. If P > 2W, consider active cooling or multiple MOSFETs in parallel.
8.14 Continue to the Next Part
Carry this evidence into Electronics: Controller Design and Review, which begins with Checkpoint: Switching Loads.
