13 KiB
Electrical Constraints Reference
类别:reference | 主题:电气约束通用规范 | 信源: 多平台汇总
Table of Contents
- Platform Comparison Summary
- Raspberry Pi Electrical Details
- ESP32 Electrical Details
- Pull-up and Pull-down Resistors
- Level Shifting
- Common Mistakes and Warnings
- Quick Reference Card
Platform Comparison Summary
- Logic voltage:Raspberry Pi=3.3V, ESP32=3.3V
- Max per-pin current:Raspberry Pi=16mA source/sink, ESP32=40mA max (20mA recommended)
- Aggregate GPIO current:Raspberry Pi=50mA total, ESP32=~1200mA total (chip limit)
- 5V tolerant:Raspberry Pi=NO, ESP32=NO
- Internal pull-up:Raspberry Pi=~50kΩ, ESP32=~45kΩ typical
- Internal pull-down:Raspberry Pi=~50kΩ, ESP32=~45kΩ typical
- Drive strength:Raspberry 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 source:16mA, Consequence of Exceeding=Voltage droop, pin damage
- Per-pin sink:16mA, 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.3V:Source=Onboard regulator, Available Current=~50mA for peripherals, Notes=Shared with Pi's 3.3V needs
- 5V:Source=USB/PSU direct, Available Current=1-2A minus Pi consumption, Notes=No regulation, direct pass-through
- GND:Source=Common ground, Available Current=N/A, Notes=8 ground pins on header
Internal Pull Resistors
- GPIO0-8:Default State=Pull-UP, Resistance=~50kΩ
- GPIO9-27:Default State=Pull-DOWN, Resistance=~50kΩ
Note: Internal pulls are too weak for I2C (need 4.7kΩ external).
Safe Driving Patterns
- Single LED:Method=330Ω-1kΩ series resistor, Notes=3-10mA safe
- Multiple LEDs:Method=Transistor driver, Notes=If total >50mA
- Relay:Method=Transistor/MOSFET + flyback diode, Notes=Never direct from GPIO
- Motor:Method=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 max:40mA, Notes=Absolute maximum
- Per-pin recommended:20mA, Notes=For reliability/longevity
- Total chip:~1200mA, Notes=Includes WiFi, BT, CPU
Power Consumption
- Active + WiFi TX:Current Draw=80-240mA, Notes=Peaks during transmission
- Active + WiFi idle:Current Draw=20-68mA, Notes=Connected but not transmitting
- Active, no radio:Current Draw=20-68mA, Notes=CPU running
- Modem sleep:Current Draw=3-20mA, Notes=WiFi paused, CPU active
- Light sleep:Current Draw=0.8mA, Notes=CPU paused, RTC running
- Deep sleep:Current Draw=10-150µA, Notes=Only RTC + ULP available
Internal Pull Resistors
- Standard GPIO:Pull-up=Yes, Pull-down=Yes, Resistance=~45kΩ
- GPIO34-39:Pull-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
- 5mA:Current=Weakest, Use Case=Low power, slow signals
- 10mA:Current=Low, Use Case=General purpose
- 20mA:Current=Default, Use Case=Most applications
- 40mA:Current=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 GND:Pull Type=Pull-UP, Typical Value=10kΩ
- Button to VCC:Pull Type=Pull-DOWN, Typical Value=10kΩ
- SPI CS line:Pull Type=Pull-UP, Typical Value=10kΩ
- Open-drain output:Pull Type=Pull-UP, Typical Value=1-10kΩ
- UART RX (optional):Pull Type=Pull-UP, Typical Value=10kΩ (noise immunity)
- Reset line:Pull 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, cheap:Cons=Input direction only
- 2 resistors:Cons=Slow (RC time constant)
- No active components:Cons=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
-
MOSFET:Specification=BSS138, 2N7000 (through-hole)
-
Pull-ups:Specification=4.7kΩ on each side
-
Voltage:Specification=3.3V on gate, low side; 5V on high side
Method 3: Dedicated Level Shifter ICs
- TXB0104:Channels=4, Type=Auto-direction, Speed=100 Mbps, I2C Safe?=NO
- TXB0108:Channels=8, Type=Auto-direction, Speed=100 Mbps, I2C Safe?=NO
- PCA9306:Channels=2, Type=I2C-specific, Speed=1 MHz, I2C Safe?=YES
- PCA9517:Channels=2, Type=I2C buffer, Speed=400 kHz, I2C Safe?=YES
- 74LVC245:Channels=8, Type=Unidirectional, Speed=100 MHz, I2C Safe?=N/A (direction pin)
- BSS138 modules:Channels=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 GPIO:Consequence=Permanent chip damage
- Drive relay coil directly from GPIO:Consequence=Inductive kickback damages GPIO
- Drive motor directly from GPIO:Consequence=Overcurrent, voltage spikes
- Exceed 50mA total on RPi GPIO:Consequence=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 I2C:Consequence=Communication failure
- Forget pull-up on 1-Wire:Consequence=Bus doesn't work
- Use TXB-series for I2C:Consequence=Unreliable communication
- Assume GPIO is 5V tolerant:Consequence=It's not — damage results
ALWAYS Do This
- Use current-limiting resistor for LEDs:Reason=Prevents overcurrent (220-330Ω)
- Use flyback diode with relays/motors:Reason=Catches inductive voltage spike
- Use level shifter for 5V ↔ 3.3V:Reason=Protects GPIO from overvoltage
- Check total current draw:Reason=Prevent exceeding limits
- Verify I2C addresses before wiring:Reason=Detect conflicts early
- Add 100nF decoupling capacitor near ICs:Reason=Reduces noise, improves stability
- Use external pull-ups for I2C (4.7kΩ):Reason=Internal pulls too weak
- Check ESP32 pin restrictions:Reason=Strapping, flash, input-only
- Use transistor for loads >16mA:Reason=Protects GPIO
- Add ESD protection for external connectors:Reason=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/down:10kΩ
- SPI CS pull-up:10kΩ
- 5V → 3.3V divider:1kΩ + 2kΩ
- Flyback diode:1N4148 or 1N4007
- Decoupling capacitor:100nF ceramic
Current Limits Summary
- Raspberry Pi:Per Pin=16mA, Total GPIO=50mA
- ESP32:Per Pin=20mA recommended, Total GPIO=~1200mA chip total
Voltage Summary
- Logic HIGH:RPi=3.3V, ESP32=3.3V
- Logic LOW:RPi=0V, ESP32=0V
- Max input:RPi=3.3V, ESP32=3.3V
- 5V tolerant:RPi=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