On this page
Common Peripheral Circuits
After learning components and protocols, the final step is to build a functional circuit. Below are the peripheral modules most commonly encountered in projects.
Buttons and Debouncing
Problem
Mechanical button presses are not clean make/break events:
V
│ ┌─┐ ┌──┐
│ │ │ │ │ ← Contact bounce
│ │ └──┘ └──────
└──────────────── t
↑ ↑
Pressed Stable
Bounce time: Typically 5~20ms
No debouncing → One button press triggers dozens of events
Hardware Debouncing (RC)
Vcc
│
Rpullup (10k)
│
├────── GPIO
│
├── R1 (1k) ──┬── Button ── GND
│
C1 (0.1μF)
│
GND
RC time constant τ = R1×C1 = 100μs (Too short)
Need τ ≈ Bounce time ≈ 10ms
→ R1=10k, C1=1μF → τ=10ms
Disadvantages: Uses extra components, but reliable
Advantages: Does not consume CPU, suitable for interrupt wake-up scenarios
Software Debouncing (Common)
// Simplest and most effective method
if
// Better: State machine + timer, non-blocking
// Record the time of the last change; only consider it valid if the interval > 20ms
Using MOSFETs as Load Switches
Why use a MOSFET instead of driving directly with a GPIO
GPIO max current: Typically 8~20mA
Loads to drive: LED strips (1A), Relays (100mA), Motors (>1A)
→ GPIO controls MOSFET, MOSFET drives the load
Low-side switch (N-MOSFET, load between Drain and Vcc):
Vcc ── Load ──┬── Drain
│
GPIO ── Rg ──── Gate
│
Source ── GND
Advantages: Simple, N-MOSFET Rds(on) is low
Disadvantages: Load is not grounded (not suitable for some scenarios)
High-side switch (P-MOSFET, load between Drain and GND):
Source ── Vcc
│
GPIO ──┬─ Rg ── Gate
│
NPN/ NMOS (Level shifting, because GPIO 3.3V cannot fully turn off P-MOS)
│
GND
Drain ── Load ── GND
Advantages: Load is grounded on one end (safer)
Disadvantages: Requires level shifting, P-MOS Rds(on) is higher
Gate Protection (Mandatory!)
Rg (100Ω~1k)
GPIO ───┤├─────── Gate
│
Rgs (10k~100k)
│
GND
Rg: Limits gate charging/discharging current, suppresses oscillation
Rgs: Ensures Gate=GND (MOSFET OFF!) during GPIO uninitialized/reset periods
Without Rgs → Floating Gate at power-up → MOSFET may turn on inadvertently
High voltage / inductive loads: Add a TVS or Zener diode from Gate to Ground
(Prevents drain spikes from coupling back to the gate via Cgd → breaking down the gate oxide)
Optocoupler Isolation
When is an optocoupler needed
- Between high voltage and low voltage (Mains 220V ↔ MCU 3.3V)
- Long-distance signal transmission (large ground potential difference)
- Need to eliminate ground loop noise
- Industrial environments (surges/lightning)
Scenarios where optocouplers are NOT needed:
- Different voltage domains on the same board → Use level shifter ICs
- On-board I2C/SPI → No isolation needed (unless industrial)
Basic Circuit
MCU Side (3.3V) Controlled Side (12V/24V)
│ │
GPIO R (Current Limit)
│ │
R (Limit LED Current) ┌──┴──┐
│ │Opto │
┌┴┐ │Output│
│LED (Internal) │ │
└┬┘ └──┬──┘
│ │
GND GND (Controlled Side)
Typical: PC817 (Cheap, Low Speed), 6N137 (High Speed, 10Mbps)
LED Side: If = 5~20mA, R = (3.3V-1.2V)/If
Output Side: Calculate current limit based on load
Relay Driving
Flyback Diode — Absolutely Essential!
Vcc
│
┌─┴─┐ ▸├ (Flyback Diode)
│Relay│ │
└─┬─┘ │
│ │
┌─┴─┐ │
│MOSFET│ │
└─┬─┘ │
│ │
GND────┘
At turn-off: Coil current cannot change instantly → Generates reverse high voltage (L×dI/dt)
Can exceed 100V! → Without flyback diode → MOSFET breakdown
Diode Direction: Reverse parallel (Cathode to Vcc, Anode to MOSFET Drain)
Normally reverse-biased and non-conducting
When MOSFET turns off, provides a current path
Diode Selection: 1N4148 (Small relays), 1N4007 (Large relays)
Fast recovery or Schottky (Reduces EMI)
Complete Circuit
Vcc (5V/12V/24V)
│
┌┴┐
│▸├ (Flyback)
└┬┘
│
┌┴┐
│Relay│
└┬┘
│
Drain
GPIO ──Rg─── Gate N-MOSFET
│ (Select low Rds(on), Vds > Vcc×2)
Source
│
GND
Relay Coil Current: I = Vcc/R_coil
Example: 5V Relay 70Ω → 71mA
MOSFET Selection:
Vds > Vcc × 1.5
Id > I_coil × 1.5
Vgs(th) < GPIO High Level (For 3.3V systems, select < 2.5V)
Recommended: AO3400 (30V/5.8A, Vth<1V, Cheap and easy to use)
LED Driving
| Method | Circuit | Application |
|---|---|---|
| Current Limiting Resistor | R = (Vcc-Vf)/I_LED | Indicator LEDs |
| Constant Current IC | e.g., CAT4101, BCR401 | High Power / Lighting |
| PWM + MOS | GPIO PWM → MOS → LED | Dimming |
| Constant Current LED Driver | e.g., WS2812B/SK6812 | Addressable RGB |
Constant Current vs. Current Limiting Resistor
Current Limiting Resistor:
I_LED = (Vcc - Vf) / R
Vcc fluctuation → Current fluctuation
Vf temperature drift → Current drift
Severe resistor heating at high power
Constant Current Source:
Current remains constant, independent of Vcc/Vf changes
Can connect multiple LEDs in series (Voltage adds up, current remains constant)
High efficiency
Watchdog
Why is it needed
Embedded device runs for 3 months → Freezes → User has to unplug power
Causes: Memory leaks, dangling pointers, EMI interference, rare bugs
Watchdog = Independent Timer
Firmware normal: Periodically "feed the dog" (reset timer)
Firmware stuck: No feed → Timeout → Hardware reset
Key Usage Points
❌ Feeding the watchdog in the main loop (Still feeding if stuck in a sub-function)
✅ Feed the watchdog in an independent monitoring task
Or: Multiple tasks must all check in before feeding the watchdog
❌ Forgetting to feed the watchdog when using delay/sleep
✅ Break long delays into multiple short delays + feed watchdog
❌ Using a very short watchdog timeout (Might not have time to feed during normal operation)
✅ Timeout > 2 times the longest task execution time
Independent Watchdog (IWDG): Independent clock source, can reset even if system clock fails
Window Watchdog (WWDG): Can only be fed within a specific time window (Stricter)
Keywords: Debouncing, MOSFET Switch, Optocoupler, Flyback Diode, Relay, LED Driver, Watchdog, GPIO