Files
esp32-dev/electrical-constraints.md
T
2026-08-05 17:25:09 +08:00

360 lines
13 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Electrical Constraints Reference
> **类别:reference** | **主题:电气约束通用规范** | **信源:** 多平台汇总
## Table of Contents
- [Platform Comparison Summary](#platform-comparison-summary)
- [Raspberry Pi Electrical Details](#raspberry-pi-electrical-details)
- [Voltage Levels](#voltage-levels)
- [Current Limits](#current-limits)
- [Power Rails](#power-rails)
- [Internal Pull Resistors](#internal-pull-resistors)
- [Safe Driving Patterns](#safe-driving-patterns)
- [ESP32 Electrical Details](#esp32-electrical-details)
- [Voltage Levels](#voltage-levels)
- [Current Limits](#current-limits)
- [Power Consumption](#power-consumption)
- [Internal Pull Resistors](#internal-pull-resistors)
- [Drive Strength Configuration](#drive-strength-configuration)
- [Pull-up and Pull-down Resistors](#pull-up-and-pull-down-resistors)
- [When Required](#when-required)
- [Calculation Formula](#calculation-formula)
- [Strength Guidelines](#strength-guidelines)
- [Consequences of Wrong Value](#consequences-of-wrong-value)
- [Level Shifting](#level-shifting)
- [When Required](#when-required)
- [Method 1: Voltage Divider (5V → 3.3V, Unidirectional)](#method-1-voltage-divider-5v--33v-unidirectional)
- [Method 2: N-Channel MOSFET (Bidirectional)](#method-2-n-channel-mosfet-bidirectional)
- [Method 3: Dedicated Level Shifter ICs](#method-3-dedicated-level-shifter-ics)
- [Method 4: Direct Connection (3.3V → 5V Input)](#method-4-direct-connection-33v--5v-input)
- [Common Mistakes and Warnings](#common-mistakes-and-warnings)
- [NEVER Do This](#never-do-this)
- [ALWAYS Do This](#always-do-this)
- [Quick Reference Card](#quick-reference-card)
- [Formulas](#formulas)
- [Quick Values](#quick-values)
- [Current Limits Summary](#current-limits-summary)
- [Voltage Summary](#voltage-summary)
- [Common Pin Restrictions](#common-pin-restrictions)
---
## Platform Comparison Summary
- Logic voltageRaspberry Pi=3.3V, ESP32=3.3V
- Max per-pin currentRaspberry Pi=16mA source/sink, ESP32=40mA max (20mA recommended)
- Aggregate GPIO currentRaspberry Pi=**50mA total**, ESP32=~1200mA total (chip limit)
- 5V tolerantRaspberry Pi=**NO**, ESP32=**NO**
- Internal pull-upRaspberry Pi=~50kΩ, ESP32=~45kΩ typical
- Internal pull-downRaspberry Pi=~50kΩ, ESP32=~45kΩ typical
- Drive strengthRaspberry Pi=Fixed, ESP32=Configurable (5-40mA)
- Input threshold (VIH)Raspberry Pi=~1.8V, ESP32=~2.0V
- Input threshold (VIL)Raspberry Pi=~0.8V, ESP32=~0.8V
---
## Raspberry Pi Electrical Details
### Voltage Levels
- **All GPIO pins operate at 3.3V ONLY**
- **5V on any GPIO pin WILL PERMANENTLY DAMAGE the SoC**
- No built-in overvoltage protection
- No built-in ESD protection (handle with care)
### Current Limits
- Per-pin source16mA, Consequence of Exceeding=Voltage droop, pin damage
- Per-pin sink16mA, Consequence of Exceeding=Voltage rise, pin damage
- **Total GPIO****50mA**, Consequence of Exceeding=Instability, crashes, permanent damage
**Critical:** The 50mA limit is across ALL GPIO pins combined, not per-bank.
### Power Rails
- 3.3VSource=Onboard regulator, Available Current=~50mA for peripherals, Notes=Shared with Pi's 3.3V needs
- 5VSource=USB/PSU direct, Available Current=1-2A minus Pi consumption, Notes=No regulation, direct pass-through
- GNDSource=Common ground, Available Current=N/A, Notes=8 ground pins on header
### Internal Pull Resistors
- GPIO0-8Default State=Pull-UP, Resistance=~50kΩ
- GPIO9-27Default State=Pull-DOWN, Resistance=~50kΩ
**Note:** Internal pulls are too weak for I2C (need 4.7kΩ external).
### Safe Driving Patterns
- Single LEDMethod=330Ω-1kΩ series resistor, Notes=3-10mA safe
- Multiple LEDsMethod=Transistor driver, Notes=If total >50mA
- RelayMethod=Transistor/MOSFET + flyback diode, Notes=Never direct from GPIO
- MotorMethod=Motor driver IC (L298N, DRV8833), Notes=Never direct from GPIO
- Buzzer (passive)Method=Transistor driver, Notes=Inductive load
- Buzzer (active)Method=Direct if <16mA, Notes=Check current draw
---
## ESP32 Electrical Details
### Voltage Levels
- **All GPIO pins operate at 3.3V ONLY**
- **5V on any GPIO pin WILL DAMAGE the chip**
- No built-in overvoltage protection
- Some ESD protection but don't rely on it
### Current Limits
- Per-pin max40mA, Notes=Absolute maximum
- Per-pin recommended20mA, Notes=For reliability/longevity
- Total chip~1200mA, Notes=Includes WiFi, BT, CPU
### Power Consumption
- Active + WiFi TXCurrent Draw=80-240mA, Notes=Peaks during transmission
- Active + WiFi idleCurrent Draw=20-68mA, Notes=Connected but not transmitting
- Active, no radioCurrent Draw=20-68mA, Notes=CPU running
- Modem sleepCurrent Draw=3-20mA, Notes=WiFi paused, CPU active
- Light sleepCurrent Draw=0.8mA, Notes=CPU paused, RTC running
- Deep sleepCurrent Draw=10-150µA, Notes=Only RTC + ULP available
### Internal Pull Resistors
- Standard GPIOPull-up=Yes, Pull-down=Yes, Resistance=~45kΩ
- GPIO34-39Pull-up=**NO**, Pull-down=**NO**, Resistance=N/A (input-only)
**Note:** Internal pulls are too weak for I2C (need 4.7kΩ external).
### Drive Strength Configuration
- 5mACurrent=Weakest, Use Case=Low power, slow signals
- 10mACurrent=Low, Use Case=General purpose
- 20mACurrent=Default, Use Case=Most applications
- 40mACurrent=Maximum, Use Case=Fast edges, heavy loads
Higher drive strength = faster edges but more EMI/noise.
---
## Pull-up and Pull-down Resistors
### When Required
- I2C bus (SDA)Pull Type=Pull-UP, Typical Value=**4.7kΩ**
- I2C bus (SCL)Pull Type=Pull-UP, Typical Value=**4.7kΩ**
- 1-Wire bus (DQ)Pull Type=Pull-UP, Typical Value=**4.7kΩ**
- Button to GNDPull Type=Pull-UP, Typical Value=10kΩ
- Button to VCCPull Type=Pull-DOWN, Typical Value=10kΩ
- SPI CS linePull Type=Pull-UP, Typical Value=10kΩ
- Open-drain outputPull Type=Pull-UP, Typical Value=1-10kΩ
- UART RX (optional)Pull Type=Pull-UP, Typical Value=10kΩ (noise immunity)
- Reset linePull Type=Pull-UP, Typical Value=10kΩ
### Calculation Formula
```
R = (VCC - VOL) / IOL
Where:
VCC = Supply voltage (3.3V)
VOL = Output low voltage (~0.4V)
IOL = Required sink current (3mA for I2C)
Example (I2C):
R = (3.3V - 0.4V) / 3mA = 967Ω minimum
Typical choice: 4.7kΩ (provides margin)
```
### Strength Guidelines
- 1kΩ:Use Case=Long wires, high capacitance, fast I2C, Notes=Strong pull, higher current
- 4.7kΩ:Use Case=Standard I2C, 1-Wire, general purpose, Notes=Most common choice
- 10kΩ:Use Case=Buttons, CS lines, low-power, Notes=Standard digital pull
- 47-100kΩ:Use Case=Wake-up inputs, ultra-low power, Notes=Very weak, slow rise time
### Consequences of Wrong Value
**Too high (weak pull):**
- Slow signal rise times
- Noise susceptibility
- Communication errors at higher speeds
- May not reach valid HIGH level
**Too low (strong pull):**
- Excessive current consumption
- Device may not be able to pull line LOW
- Wasted power in battery applications
---
## Level Shifting
### When Required
- 5V logic output → 3.3V GPIO input
- 3.3V GPIO output → 5V input (if device doesn't recognize 3.3V as HIGH)
- Bidirectional communication between 3.3V and 5V systems
### Method 1: Voltage Divider (5V → 3.3V, Unidirectional)
**Use for:** Slow signals (<100kHz), input direction only
```
5V Signal ──[1kΩ]──┬──> 3.3V GPIO Input
[2kΩ]
GND
Output: 5V × (2kΩ / 3kΩ) = 3.33V
```
- Simple, cheapCons=Input direction only
- 2 resistorsCons=Slow (RC time constant)
- No active componentsCons=Loads the signal
### Method 2: N-Channel MOSFET (Bidirectional)
**Use for:** I2C, 1-Wire, open-drain signals up to 400kHz
```
3.3V Side 5V Side
│ │
[4.7kΩ] [4.7kΩ]
│ │
├────────┬──────────────────┬──────────┤
│ │ │ │
SDA Source Drain SDA
(3.3V) └───── BSS138 ─────┘ (5V)
Gate
3.3V
```
**Operation:**
- Gate tied to LOW side voltage (3.3V)
- When LOW side pulls down, MOSFET conducts, pulling HIGH side down
- When HIGH side pulls down, body diode conducts, pulling LOW side down
- Pull-ups restore HIGH state on both sides
- MOSFETSpecification=BSS138, 2N7000 (through-hole)
- Pull-upsSpecification=4.7kΩ on each side
- VoltageSpecification=3.3V on gate, low side; 5V on high side
### Method 3: Dedicated Level Shifter ICs
- TXB0104Channels=4, Type=Auto-direction, Speed=100 Mbps, I2C Safe?=**NO**
- TXB0108Channels=8, Type=Auto-direction, Speed=100 Mbps, I2C Safe?=**NO**
- PCA9306Channels=2, Type=I2C-specific, Speed=1 MHz, I2C Safe?=**YES**
- PCA9517Channels=2, Type=I2C buffer, Speed=400 kHz, I2C Safe?=**YES**
- 74LVC245Channels=8, Type=Unidirectional, Speed=100 MHz, I2C Safe?=N/A (direction pin)
- BSS138 modulesChannels=4, Type=Bidirectional, Speed=400 kHz, I2C Safe?=**YES**
**CRITICAL WARNING:** TXB-series level shifters do **NOT** work reliably with open-drain protocols (I2C, 1-Wire). They fight the pull-up resistors and cause communication errors. Use BSS138-based modules or PCA9306 for I2C.
### Method 4: Direct Connection (3.3V → 5V Input)
Many 5V devices recognize 3.3V as logic HIGH:
- VIH (HIGH threshold)Typical 5V TTL=2.0V, Typical 5V CMOS=3.5V
- VIL (LOW threshold)Typical 5V TTL=0.8V, Typical 5V CMOS=1.5V
**Check datasheet for VIH.** If VIH < 3.0V, direct connection usually works.
**NEVER** connect 5V output directly to 3.3V input — level shift or divide required.
---
## Common Mistakes and Warnings
### NEVER Do This
- Connect 5V directly to any GPIOConsequence=**Permanent chip damage**
- Drive relay coil directly from GPIOConsequence=Inductive kickback damages GPIO
- Drive motor directly from GPIOConsequence=Overcurrent, voltage spikes
- Exceed 50mA total on RPi GPIOConsequence=Voltage instability, damage
- Use ESP32 GPIO6-11 (WROOM)Consequence=Flash pins — chip crashes
- Pull GPIO12 HIGH at boot (ESP32)Consequence=**Flash voltage brick**
- Forget pull-ups on I2CConsequence=Communication failure
- Forget pull-up on 1-WireConsequence=Bus doesn't work
- Use TXB-series for I2CConsequence=Unreliable communication
- Assume GPIO is 5V tolerantConsequence=It's not — damage results
### ALWAYS Do This
- Use current-limiting resistor for LEDsReason=Prevents overcurrent (220-330Ω)
- Use flyback diode with relays/motorsReason=Catches inductive voltage spike
- Use level shifter for 5V ↔ 3.3VReason=Protects GPIO from overvoltage
- Check total current drawReason=Prevent exceeding limits
- Verify I2C addresses before wiringReason=Detect conflicts early
- Add 100nF decoupling capacitor near ICsReason=Reduces noise, improves stability
- Use external pull-ups for I2C (4.7kΩ)Reason=Internal pulls too weak
- Check ESP32 pin restrictionsReason=Strapping, flash, input-only
- Use transistor for loads >16mAReason=Protects GPIO
- Add ESD protection for external connectorsReason=Protects against static
---
## Quick Reference Card
### Formulas
**LED Resistor:**
```
R = (VCC - Vf) / If
R = (3.3V - 2.0V) / 10mA = 130Ω minimum
Recommended: 220-330Ω (5-10mA, plenty bright)
```
**Voltage Divider:**
```
Vout = Vin × (R2 / (R1 + R2))
For 5V → 3.3V: R1=1kΩ, R2=2kΩ
```
**Pull-up Resistor:**
```
R = (VCC - VOL) / IOL
Standard: 4.7kΩ for I2C/1-Wire, 10kΩ for buttons
```
### Quick Values
- LED resistor (3.3V, red/green)220-330Ω
- LED resistor (3.3V, blue/white)100-150Ω
- I2C pull-up**4.7kΩ** to 3.3V
- 1-Wire pull-up**4.7kΩ** to 3.3V
- Button pull-up/down10kΩ
- SPI CS pull-up10kΩ
- 5V → 3.3V divider1kΩ + 2kΩ
- Flyback diode1N4148 or 1N4007
- Decoupling capacitor100nF ceramic
### Current Limits Summary
- Raspberry PiPer Pin=16mA, Total GPIO=**50mA**
- ESP32Per Pin=20mA recommended, Total GPIO=~1200mA chip total
### Voltage Summary
- Logic HIGHRPi=3.3V, ESP32=3.3V
- Logic LOWRPi=0V, ESP32=0V
- Max inputRPi=3.3V, ESP32=3.3V
- 5V tolerantRPi=**NO**, ESP32=**NO**
### Common Pin Restrictions
**Raspberry Pi:**
- GPIO0/1: Reserved (HAT EEPROM)
- GPIO14/15: UART/BT conflict (Pi 3/4/Zero2W)
**ESP32:**
- GPIO6-11: Flash pins — **NEVER USE**
- GPIO12: Strapping — **DANGER** (flash voltage)
- GPIO16-17: PSRAM (WROVER only)
- GPIO34-39: Input only, no pulls
---