Scenario: You’re designing a smart doorbell that records 5-second audio clips when the button is pressed. The microphone outputs analog audio (20 Hz - 8 kHz human voice range). You need to select the ADC sampling rate for the ESP32 and calculate storage requirements.
Given:
- Audio range: 20 Hz - 8 kHz (human voice with harmonics)
- Clip duration: 5 seconds
- ADC resolution: 12-bit (ESP32)
- Available flash: 4 MB
- Target: Store 100 clips
Step 1: Calculate Minimum Sampling Rate (Nyquist)
\[f_{sample} \geq 2 \times f_{max} = 2 \times 8000\text{ Hz} = 16,000\text{ Hz}\]
Step 2: Choose Practical Sampling Rate
Nyquist minimum is 16 kHz, but practical audio uses oversampling: - Standard rates: 8 kHz (telephone), 16 kHz (wideband), 44.1 kHz (CD quality) - Select 16 kHz (matches Nyquist minimum, adequate for doorbell voice)
Step 3: Calculate Storage Per Clip
Samples per clip: 16,000 samples/sec × 5 sec = 80,000 samples
Bytes per sample: 12-bit ADC → 2 bytes/sample (stored as 16-bit)
Storage per clip: 80,000 samples × 2 bytes = 160 KB/clip
Step 4: Verify Storage Budget
100 clips × 160 KB = 16 MB
Available: 4 MB → Insufficient!
Step 5: Optimization Options
Option A: Reduce sampling rate to 8 kHz (below Nyquist for 8 kHz signal) - Problem: Aliasing! 8 kHz components fold to 0 Hz (DC) - Result: Muffled, distorted audio
Option B: Reduce clip duration to 2 seconds - Storage: 16,000 × 2 × 2 = 64 KB/clip - 100 clips × 64 KB = 6.4 MB → Still too much
Option C: Compress audio (recommended) - Use 8-bit μ-law compression (telephone standard): 50% size reduction - 160 KB → 80 KB/clip - 100 clips × 80 KB = 8 MB → Still over budget
Option D: Hybrid solution (BEST) - Reduce sampling to 12 kHz (still above Nyquist for 6 kHz bandwidth) - Add 6 kHz anti-aliasing filter (removes >6 kHz before ADC) - Use 8-bit encoding - Storage: 12,000 × 5 × 1 byte = 60 KB/clip - 100 clips × 60 KB = 6 MB → Need to reduce to 66 clips OR add external flash
Final Design:
- Sampling rate: 12 kHz
- Anti-aliasing filter: 6 kHz cutoff (hardware RC filter)
- Encoding: 8-bit μ-law compression
- Storage: 60 KB/clip
- Clips stored: 66 clips (fits in 4 MB with margin)
Key Lesson: Sampling rate directly impacts storage. Always verify the full system budget (sample rate × duration × resolution × quantity) before finalizing the design. Hardware filters enable lower sampling rates without aliasing.