Chapters

29 Sampling Design: Product Scenarios

electronics-controller-design
analog
digital
nyquist
adc
mixed-signal
signal-chain
binary
sensors
firmware

29.1 Start With the Decision

A doorbell, thermometer, and ECG monitor do not need the same sample rate. Each product must preserve its fastest useful change without wasting power or storage.

29.2 Route Overview

This is part 2 of 3. Review Sampling and Aliasing: Nyquist and Filters for the preceding evidence.

29.3 Learning Objectives

  • Choose sample rates for doorbell, temperature, and ECG signals.
  • Adapt multi-channel sampling while preserving anti-alias margins.

29.4 Chapter Roadmap

  • For Kids: Meet the Sensor Squad!
  • Doorbell Sampling Design
  • Sampling Rate Examples
  • Beginner: Temperature Sensor Sampling
  • Intermediate: ECG Heart Monitor Sampling
  • Deep dive: Adaptive Multi-Channel Sampling
  • Checkpoint: Product Scenarios
  • Key Takeaway
  • Label the Diagram
  • Code Challenge
  • Aliasing Filter Contracts

Sampling is like taking photos of a moving object — take photos too slowly and you miss the action!

29.4.1 Speed Camera Story

Temperature Terry was watching a spinning wheel. “I can see it spinning smoothly, going round and round!”

the microcontroller said, “I cannot watch continuously like you, Sammy. I can only take snapshots — like photographs. Tell me what you see at each snapshot!”

So Max took a photo every second. But the wheel was spinning 10 times per second! In Max’s photos, the wheel looked like it was barely moving — or even going backwards!

the LED was confused. “That is like when car wheels look like they spin backwards in movies!”

“Exactly!” said ADC Andy. “That is called ALIASING. Max is not taking photos fast enough, so the fast spinning LOOKS like slow spinning.”

the battery asked, “How fast does Max need to take photos?”

Andy did some math. “The wheel spins 10 times per second. Max needs to take at LEAST 20 photos per second — that is the Nyquist Rule: at least TWICE as fast as the thing is changing!”

Max tried again at 25 photos per second. Now the wheel looked perfect in his snapshots!

“Even better,” Andy added, “take 50 photos per second for extra safety. We call that oversampling!”

29.4.2 Key Words for Kids

WordWhat It Means
SamplingTaking snapshots of something that changes continuously
Nyquist RuleYou need at least 2x as many snapshots as the speed of change
AliasingWhen too few snapshots make fast things look slow (or wrong)
OversamplingTaking extra snapshots for safety (2.5-5x is common)
Anti-aliasingA filter that removes changes too fast for your snapshot speed

29.4.3 Try This at Home!

The Spinning Wheel Experiment!

  1. Ask someone to wave their hand back and forth quickly
  2. Blink your eyes slowly (once per second) — can you track their hand? (No! Aliasing!)
  3. Now keep your eyes open normally — you see smooth movement (high sample rate!)
  4. Try blinking faster and faster until you can track the hand again

What you learned: Your normal vision is like a high sample rate camera (about 60 frames per second). Blinking slowly is like undersampling — you miss what is really happening!

Now apply the same rule to memory, flash, and filter choices.

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)

fsample2×fmax=2×8000 Hz=16,000 Hzf_{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.

29.5 Sampling Rate Examples

Scenario: You’re reading a DS18B20 temperature sensor for a home automation system. Room temperature changes slowly over minutes/hours.

Given:

  • Temperature range: 20-30°C
  • Typical change rate: 0.5°C per hour
  • Sensor accuracy: ±0.5°C

Solution:

  • Maximum meaningful frequency: ~0.001 Hz (one change per 1000 seconds)
  • Nyquist minimum: 2 × 0.001 Hz = 0.002 Hz (one sample per 500 seconds)
  • Practical choice: 0.1 Hz (one sample every 10 seconds) = 100× oversampling
  • Power consumption: Minimal (0.005 mW with ESP32 deep sleep between samples)

Result: 0.1 Hz sampling captures all temperature variations while maximizing battery life.

Scenario: Design a wearable ECG monitor for arrhythmia detection. The QRS complex (the sharp peak in a heartbeat) contains clinically significant features up to 150 Hz, though most energy is below 40 Hz.

Given:

  • Clinically relevant bandwidth: 0.05-150 Hz (AHA/ACC diagnostic standard)
  • ADC: ESP32 12-bit @ max 83 kHz
  • Power: 250 mAh LiPo battery, target 24-hour continuous recording

Solution:

  • Maximum frequency: 150 Hz
  • Nyquist minimum: 2 x 150 Hz = 300 Hz
  • Practical choice: 500 Hz (3.3x oversampling)
    • Standard clinical ECG rate (AHA recommendation)
    • Captures QRS morphology detail needed for arrhythmia classification
    • Allows anti-aliasing filter with gradual roll-off above 150 Hz
  • Anti-aliasing filter: 2nd-order Butterworth, cutoff = 200 Hz (before 500 Hz sampling)
  • Storage: 500 samples/s x 2 bytes x 86,400 s = 86.4 MB/day (fits on 1 GB flash with margin)

Result: 500 Hz sampling meets clinical diagnostic requirements while keeping power draw under 5 mA for 24+ hour battery life.

Scenario: Design a wildlife audio recorder with 4 microphone channels. Different animals vocalize at different frequencies. The system must optimize storage/power by adapting sampling rates.

Given:

  • Channel 1: Infrasound (elephants, 5-20 Hz)
  • Channel 2: Birdsong (2-8 kHz)
  • Channel 3: Ultrasound (bats, 20-80 kHz)
  • Channel 4: Ambient (full spectrum, 20 Hz - 20 kHz)
  • Storage: 256 GB SD card, 30-day deployment
  • Power: Solar + 10,000 mAh battery

Solution:

Step 1: Calculate Nyquist minimums per channel

  • Ch1 (infrasound): 2 × 20 Hz = 40 Hz
  • Ch2 (birdsong): 2 × 8 kHz = 16 kHz
  • Ch3 (ultrasound): 2 × 80 kHz = 160 kHz
  • Ch4 (ambient): 2 × 20 kHz = 40 kHz

Step 2: Select practical rates (2.5× oversampling)

  • Ch1: 100 Hz
  • Ch2: 40 kHz
  • Ch3: 200 kHz (ESP32 limit = 83 kHz → requires external ADC)
  • Ch4: 100 kHz (requires external ADC)

Step 3: Calculate storage budget

  • Ch1: 100 samples/s × 2 bytes × 86,400 s/day = 17.3 MB/day
  • Ch2: 40,000 × 2 × 86,400 = 6.9 GB/day
  • Ch3: 200,000 × 2 × 86,400 = 34.6 GB/day (exceeds 30-day budget!)
  • Ch4: 100,000 × 2 × 86,400 = 17.3 GB/day

Step 4: Optimization strategies

  • Triggered recording: Only record when sound detected (reduces duty cycle 90%)
  • Compression: Use 8-bit μ-law (50% reduction) for Ch2/Ch4
  • Variable rate: Ch3 records only at night (bats), Ch2 only at dawn/dusk (birds)
  • Anti-aliasing filters: Per-channel analog filters (Ch1: 40 Hz, Ch2: 16 kHz, Ch3: 80 kHz, Ch4: 40 kHz)

Final Design:

  • Ch1: 100 Hz, continuous, 8-bit → 8.6 MB/day
  • Ch2: 40 kHz, 2 hours/day (dawn/dusk), 8-bit → 576 MB/day
  • Ch3: 200 kHz, 8 hours/day (night), 16-bit → 11.5 GB/day
  • Ch4: 48 kHz (CD-quality), event-triggered (10% duty cycle), 16-bit → 1.7 GB/day
  • Total: ~13.8 GB/day × 30 days = 414 GB → Still over budget!

Final optimization: Use lossless compression (FLAC) achieving 40% size reduction → 248 GB fits with margin on 256 GB SD card.

Key lesson: Multi-channel systems with diverse sampling rates require careful storage/power budgeting. Anti-aliasing filters are mandatory for each channel. Adaptive/triggered recording is essential for battery-powered deployments.

Voltage VeraCheckpoint: Product Scenarios

You now know:

  • A 5-second, 16 kHz, 12-bit doorbell clip needs 160 kB before optimization.
  • A 500 Hz ECG design produces about 86.4 MB/day at 2 bytes per sample.
  • A four-channel wildlife design can reach 414 GB for 30 days before compression.

Key Takeaway

The Nyquist theorem is the single most important rule for digitizing analog signals: always sample at least twice the highest frequency present. Violating this rule causes aliasing, where high-frequency content masquerades as lower frequencies, producing silently corrupted data that looks plausible but is wrong. In battery-powered IoT devices, balancing sampling rate against power consumption is a critical design decision.

29.6 Aliasing Filter Contracts

The main chapter above stays focused on sampling-rate choice, Nyquist intuition, aliasing examples, power trade-offs, and practical calculators. For the deeper design contract behind folded-frequency math, irreversible aliases, analog pre-ADC filtering, oversampling headroom, and reconstruction-filter boundaries, continue to Aliasing and Anti-Alias Filter Contracts.

29.7 Continue to the Next Part

Carry this evidence into Analog-Digital Boundaries: Signal Contracts, which begins with Analog-Digital Signal Boundary Contracts.