8 Electronics: Power and Transistor Switching
More
Depth: Practitioner
Progress saves as you read. Mark sections complete as you go; your progress appears in the sidebar.
8.1 Start With the Decision
This section: Start With the Decision — GPIO means general-purpose input and output; its pins carry control signals.
GPIO means general-purpose input and output; its pins carry control signals. A transistor can switch a larger load using that signal. Load power is the supply that drives the valve itself. 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 section: Route Overview — This is part 1 of 2.
This is part 1 of 2. Continue with Electronics: Controller Design and Review.
8.3 Part Objectives
This section: Part Objectives
- Calculate base or gate drive for a switched load.
- Choose protection for inductive current and supply faults.
8.4 Start Simple
This section: 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
This section: 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 section: Overview — This book is the bridge between basic electricity and practical IoT hardware design.
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
This section: 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
This section: 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
This section: Smart Light from 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.
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
This section: 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
This section: 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
This section: 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 the P2N2222A datasheet’s 0.3V maximum saturation drop at its stated 150mA/15mA test point, a 33Ω resistor gives LED current at that assumed drop. Verify the transistor’s actual drive and the LED’s current rating.
For a 100mA collector current, use forced gain near 10: about 10mA base drive, if the GPIO’s guaranteed source current and high-level voltage permit it; otherwise use a suitable MOSFET or driver. Choose the base resistor using the minimum GPIO high voltage and maximum VBE(sat). The P2N2222A datasheet guarantees VCE(sat) up to 0.3V at 150mA/15mA, so 0.2V is an assumption, not a limit.
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: choose a Schottky or suitable fast-recovery diode whose repetitive current, surge and thermal ratings cover the motor’s running and stall-current PWM conditions; 1N5819 suitability is not established for the stated 2A stall case.
- 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 |
| Suitable rated flyback diode | Across motor, cathode to +12V | Repetitive current, surge and thermal ratings must cover running and stall-current PWM conditions; 1N5819 suitability is not established for the stated 2A stall case |
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 the 10V-gate datasheet value. The 0.05Ω, 3.3V estimate would give 12.5mW conditionally; the datasheet does not guarantee that resistance at 3.3V.
- Drain switching loss (transitions): Depends on drain-voltage/current overlap and transition times
Gate Charge Calculation:
- IRLZ44N gate charge: Qg = 63nC (from datasheet)
- PWM frequency: f = 1 kHz
- Average gate-charge current: I = Q × f = 63nC × 1000 = 63µA using the datasheet gate-charge figure illustratively; actual gate charge at 3.3V must be checked.
- Approximate gate-drive power: P_gate ≈ Qg × Vdrive × f. Using the datasheet’s 63nC gate-charge figure illustratively gives 63nC × 3.3V × 1000 ≈ 0.2mW; actual gate charge at 3.3V must be checked. This is driver power, not MOSFET drain switching loss, which depends on drain voltage, current and transition times.
Total Power Dissipation: The 5.5mW figure uses 10V-gate on-resistance although the GPIO supplies 3.3V; the 0.2mW figure is gate-drive power, not drain switching loss. Verify on-resistance at 3.3V and transition losses before deciding whether a heatsink is needed.
If using higher PWM frequency (20 kHz for quieter operation):
- Approximate gate-drive power: 63nC × 3.3V × 20,000 = 4.2mW; actual gate charge at 3.3V must be checked. This is not drain switching loss. Verify on-resistance at the applied gate voltage and drain transition losses before estimating total MOSFET dissipation.
Motor Characteristics:
- PWM duty cycle 50% → average applied voltage about 6V; actual motor speed depends on motor characteristics and load and must be measured.
- 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)
Expected result in this motor example: ESP32 PWM (0–100% duty) → MOSFET switches a modeled 500 mA load → motor speed may vary with duty cycle; verify under load. 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 requires duty-weighted conduction and drain-transition estimates at the approved gate voltage before thermal sign-off
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.
This section:
8.14 Continue to the Next Part
This section: Continue to the Next Part — Carry this evidence into Electronics: Controller Design and Review, which begins with Checkpoint: Switching Loads.
Carry this evidence into Electronics: Controller Design and Review, which begins with Checkpoint: Switching Loads.
Find help
3 reviewed answers.
Where should I start in a module?
Open the module page from the sidebar to see its chapters in order. Use the module map to return to a topic you need.
Modules and pathsReviewed
What can I try when a project does not work?
Check the symptom, then work through the diagnostic steps. Keep the error message and the smallest example that reproduces it.
When you are stuckReviewed
How do interactive examples work?
Use the interactive guide to check how examples and simulations work before changing your project.
Interactive examples guideReviewed
