19  ADC/DAC Worked Examples

In 60 Seconds

This chapter walks through real-world ADC and DAC calculations step by step: converting sensor voltages to digital values, matching reference voltages to sensor ranges for optimal resolution, analyzing quantization error, applying Nyquist sampling rates for audio and vibration, optimizing ESP32 ADC configuration for soil moisture sensing, choosing timers over ADCs for ultrasonic distance measurement, controlling motor speed with DAC output, and implementing gamma correction for perceptually accurate LED dimming. These are the essential formulas every IoT engineer uses when interfacing sensors and actuators.

Key Concepts
  • Worked Example Structure: Each worked example follows the same format: problem statement, identify knowns and unknowns, select the appropriate formula, substitute values with units, compute result, verify the answer is physically plausible, and state the practical implication
  • Unit Consistency Checking: All quantities in a formula must use consistent SI base units before computing; convert mA to A, kOhm to Ohm, nF to F, and mV to V before substituting into formulas to prevent systematic errors by factors of 10^3
  • Voltage Divider Analysis: Vout = Vin x R2 / (R1 + R2); used in thermistor circuits, LDR circuits, and reference voltage generation; the key insight: Vout approaches Vin when R2 >> R1, and approaches 0 when R2 << R1
  • LED Current Limiting Design: R = (Vsupply - Vf) / If; typical values: Vsupply = 3.3 V, Vf = 2.0 V (red LED), If = 10 mA; R = (3.3 - 2.0) / 0.010 = 130 ohm; round up to 150 ohm standard value for safety
  • RC Filter Cutoff Calculation: fc = 1 / (2 pi R C); 10 kohm and 100 nF gives fc = 1 / (2 pi x 10000 x 100e-9) = 159 Hz; any noise above 159 Hz is attenuated; signals well below 159 Hz pass without significant attenuation
  • Battery Life Estimation: Runtime (hours) = Battery capacity (mAh) / Average current (mA); for duty-cycled devices: Iavg = Iactive x duty_cycle + Isleep x (1 - duty_cycle); a 1000 mAh battery powering a 50 mA active / 0.1 mA sleep device at 10% duty cycle: Iavg = 5 + 0.09 = 5.09 mA; runtime = 1000/5.09 = 196 hours
  • ADC Voltage Conversion: Voltage = (ADC_reading / (2^bits - 1)) x Vref; for a 12-bit ADC reading 2048 with Vref = 3.3 V: V = (2048/4095) x 3.3 = 1.65 V; verify: at maximum reading (4095), output = Vref = 3.3 V (correct)
  • Power Dissipation Check: P = V^2 / R or P = I^2 x R; a 100 ohm resistor carrying 50 mA: P = (0.050)^2 x 100 = 0.25 W; requires at least a 0.5 W rated resistor (50% safety margin); a 1/4 W (0.25 W) resistor would run at its absolute limit and fail early

19.1 Learning Objectives

By the end of this section, you will be able to:

  • Calculate ADC Output: Compute digital values from analog voltage inputs using the ADC conversion formula
  • Select Reference Voltages: Evaluate and match Vref to sensor output ranges for maximum resolution
  • Quantify Quantization Error: Derive measurement precision limits across different ADC resolutions
  • Determine Nyquist Sampling Rates: Compute minimum and practical sampling frequencies for specific signal types
  • Compute DAC Output Voltages: Derive specific analog voltages from digital input values
  • Construct Gamma Correction Tables: Build non-linear mappings for perceptually accurate LED dimming

This chapter walks through the math behind converting real-world sensor voltages into digital numbers and back again, step by step. If you have ever wondered how a temperature sensor’s voltage becomes the “23.5C” on your screen, these worked examples show exactly how that calculation happens. No advanced math is needed – just multiplication and division.

19.2 Prerequisites

Before diving into this chapter, you should be familiar with:


19.3 ADC Conversion Formula

The fundamental ADC conversion formula relates analog input voltage to digital output value:

\[\text{Digital Output} = \left\lfloor \frac{V_{in}}{V_{ref}} \times (2^n - 1) \right\rfloor\]

Where: - \(V_{in}\) = Analog input voltage (volts) - \(V_{ref}\) = Reference voltage (maximum measurable voltage) - \(n\) = ADC resolution (number of bits) - \(\lfloor \rfloor\) = Floor function (round down to nearest integer)

ADC conversion graph illustrating step-wise quantization of continuous analog voltage signal into discrete digital values, showing voltage levels on vertical axis and digital output codes on horizontal axis with quantization error bands
Figure 19.1: ADC conversion showing how continuous analog voltage is sampled and quantized into discrete digital steps

19.4 Worked Example 1: Temperature Sensor (10-bit ADC)

Given:

  • ADC Resolution: 10 bits (Arduino Uno)
  • \(V_{ref}\) = 3.3V
  • \(V_{in}\) = 1.65V (sensor output at 25C)

Calculate Digital Output:

\[\text{Digital Output} = \left\lfloor \frac{1.65V}{3.3V} \times (2^{10} - 1) \right\rfloor\]

\[= \left\lfloor 0.5 \times 1023 \right\rfloor\]

\[= \left\lfloor 511.5 \right\rfloor\]

\[= 511\]

Result: The ADC outputs 511 (binary: 0111111111)

Verification:

  • 511.5 is exactly half of 1023, so the floor gives 511, confirming \(V_{in}\) is half of \(V_{ref}\)
  • This digital value would be read by analogRead(A0) as 511

19.5 Worked Example 2: LM35 Temperature Sensor

The LM35 is a popular precision temperature sensor that outputs 10mV per degree Celsius.

LM35 Specifications:

  • Output: 10mV/C
  • Temperature Range: 0-100C
  • Voltage Range: 0-1.0V

Scenario: Measuring room temperature (25C) with 10-bit ADC

Step 1: Calculate Sensor Output Voltage

\[V_{sensor} = \text{Temperature} \times 10\text{mV/C}\]

\[V_{sensor} = 25C \times 10\text{mV/C} = 250\text{mV} = 0.250V\]

Step 2: ADC Configuration

For optimal resolution with 0-1V sensor range: - Use \(V_{ref}\) = 1.0V (external reference or ATmega internal 1.1V) - Resolution: 10-bit → 1024 discrete values

Step 3: Calculate Digital Output

\[\text{Digital Output} = \left\lfloor \frac{0.250V}{1.0V} \times 1023 \right\rfloor\]

\[= \left\lfloor 255.75 \right\rfloor = 255\]

Step 4: Calculate Resolution (Temperature per ADC Count)

\[\text{Resolution} = \frac{\text{Temperature Range}}{\text{ADC Counts}} = \frac{100C}{1024} = 0.0977C \text{ per count}\]

This means each ADC step represents approximately 0.1C change.

Step 5: Convert ADC Reading Back to Temperature

\[\text{Temperature} = \frac{\text{ADC Value}}{1023} \times 100C\]

\[\text{Temperature} = \frac{255}{1023} \times 100C = 24.93C \approx 25.0C\]

Key Insight: By matching \(V_{ref}\) to sensor range (1V instead of 5V), we maximize resolution and minimize wasted ADC range.

Comparison: 5V Reference vs 1V Reference

Configuration Resolution Wasted Range Temperature Precision
5V Vref 4.88mV/count 80% (1V used, 4V unused) 0.488C/count
1V Vref 0.977mV/count 0% (1V used, 1V available) 0.098C/count

Using 1V reference gives 5x better temperature resolution by fully utilizing ADC range!


19.6 Worked Example 3: Quantization Error Analysis

Quantization error is the inherent inaccuracy introduced when converting continuous analog signals to discrete digital values.

Quantization Error Formula:

\[\text{Max Error} = \pm \frac{1}{2} \text{LSB} = \pm \frac{V_{ref}}{2^{n+1}}\]

Where LSB (Least Significant Bit) is the smallest voltage change the ADC can detect.

10-bit ADC with 3.3V Reference:

\[\text{LSB Step Size} = \frac{3.3V}{2^{10}} = \frac{3.3V}{1024} = 3.22\text{mV}\]

\[\text{Max Quantization Error} = \pm \frac{3.22\text{mV}}{2} = \pm 1.61\text{mV}\]

What This Means:

  • Any voltage between 1.650V and 1.653V will round to the same digital value (512)
  • Worst-case error: +/-1.61mV from actual voltage

Comparison Across ADC Resolutions:

Resolution LSB Step (3.3V) Max Error Example Use Case
8-bit 12.9mV +/-6.45mV Low-cost light sensors
10-bit 3.22mV +/-1.61mV Arduino Uno (general purpose)
12-bit 0.805mV +/-0.403mV ESP32 (precision sensors)
16-bit 0.0503mV +/-0.0252mV Thermocouples, strain gauges

Reducing Quantization Error:

  1. Use Higher Resolution ADC
    • 12-bit vs 10-bit: 4x reduction in error (1.61mV → 0.403mV)
    • 16-bit vs 10-bit: 64x reduction in error
  2. Oversampling and Averaging
    • Average N samples to gain extra resolution
    • Effective bits increase by log2(N)/2
    • Example: Average 16 samples with 10-bit ADC → 12-bit effective resolution
  3. Match Vref to Signal Range
    • If measuring 0-1V signal, use 1V reference (not 3.3V or 5V)
    • Maximizes use of ADC’s available range


19.7 Worked Example 4: Nyquist Theorem - Audio Sampling

Given:

  • Human voice frequency range: 300 Hz to 3.4 kHz
  • Highest frequency: \(f_{max}\) = 3.4 kHz

Calculate Minimum Sampling Rate:

\[f_{sample} \geq 2 \times 3400\text{ Hz} = 6800\text{ Hz}\]

Practical Implementation:

  • Telephone systems: 8 kHz (provides margin above 6.8 kHz minimum)
  • VoIP codecs: 8-16 kHz
  • Audio recording: 44.1-48 kHz (for full 20 kHz human hearing range)

19.8 Worked Example 5: Accelerometer - Vibration Monitoring

Given:

  • Machine vibration events: 0-100 Hz
  • Highest frequency: \(f_{max}\) = 100 Hz

Calculate Minimum Sampling Rate:

\[f_{sample} \geq 2 \times 100\text{ Hz} = 200\text{ Hz}\]

Practical Implementation:

  • Industrial IoT: 500-1000 Hz (2.5-5x oversampling for safety margin)
  • Allows detection of unexpected high-frequency faults
  • Ensures anti-aliasing filter has gradual roll-off

Real-World Sampling Rates:

Application Signal Bandwidth Nyquist Minimum Practical Rate Oversampling Factor
Audio (telephone) 3.4 kHz 6.8 kHz 8 kHz 1.2x (minimal)
Audio (music) 20 kHz 40 kHz 44.1-48 kHz 1.1-1.2x
Accelerometer 100 Hz 200 Hz 500-1000 Hz 2.5-5x
ECG (heartbeat) 150 Hz 300 Hz 500-1000 Hz 1.7-3.3x
Ultrasonic distance 40 kHz 80 kHz 200 kHz+ 2.5x

19.9 Worked Example 6: Soil Moisture Sensor (ESP32)

Worked Example: Soil Moisture Sensor ADC Optimization for Precision Agriculture

Scenario: A smart irrigation system uses a capacitive soil moisture sensor that outputs 1.2V (dry) to 2.8V (saturated). You need to determine optimal ADC configuration on an ESP32 to detect 1% moisture changes for water-efficient farming.

Given:

  • Sensor output range: 1.2V (dry/0%) to 2.8V (saturated/100%)
  • ESP32 ADC: 12-bit, configurable attenuation (0dB, 2.5dB, 6dB, 11dB)
  • Moisture measurement range: 0-100%
  • Required resolution: 1% moisture change detection

Steps:

  1. Calculate voltage per percent moisture: \[\Delta V = \frac{V_{max} - V_{min}}{\text{Moisture Range}} = \frac{2.8V - 1.2V}{100\%} = 16mV/\%\]

  2. Determine required ADC resolution:

    • To detect 1% change, need LSB < 16mV
    • With 11dB attenuation: range = 0-3.6V, LSB = 3.6V/4096 = 0.88mV (sufficient)
    • With 6dB attenuation: range = 0-2.2V (cannot read 2.8V - too narrow)
    • Select 11dB attenuation (0-3.6V range covers sensor output)
  3. Calculate actual resolution achieved: \[\text{ADC steps per } 1\% = \frac{16mV}{0.88mV} = 18.2 \text{ steps}\]

    • Theoretical moisture resolution: 1/18.2 = 0.055% per ADC step
  4. Account for ESP32 ADC nonlinearity:

    • ESP32 ADC has known nonlinearity at extremes (0-0.1V and >3.0V)
    • Sensor range (1.2-2.8V) falls in linear region - acceptable
    • Consider factory calibration values in eFuse for better accuracy
  5. Implement oversampling for noise reduction:

    • Average 64 samples to gain ~3 effective bits
    • Effective resolution: 12 + 3 = 15 bits equivalent
    • New moisture resolution: 0.055% / 8 = 0.007% per averaged reading

Result:

  • Raw ADC values: 1365 (dry) to 3185 (saturated) with 11dB attenuation
  • Moisture formula: moisture_percent = (adc_value - 1365) / 18.2
  • With 64-sample averaging: <0.01% moisture resolution

Key Insight: Proper attenuation selection is critical. Using 0dB attenuation (0-1.1V range) would clip the signal, while 11dB provides headroom. The ESP32’s ADC nonlinearity is most pronounced at range extremes - design sensor output to fall in the 0.5-2.5V linear region when possible.


19.10 Worked Example 7: Ultrasonic Distance Measurement

Worked Example: Ultrasonic Distance Measurement Timing with ADC Sampling

Scenario: An autonomous robot uses an HC-SR04 ultrasonic sensor for obstacle detection. The echo pulse width (10us to 25ms) must be measured accurately. You need to determine if ADC-based measurement or timer-based capture is appropriate.

Given:

  • HC-SR04 echo pulse: 10us (2cm) to 25ms (4m)
  • Speed of sound: 343 m/s at 20C
  • Required distance accuracy: 1cm
  • ESP32 ADC sampling rate: 2 MSPS maximum (but limited in practice)
  • ESP32 timer resolution: 12.5ns (80MHz clock)

Steps:

  1. Calculate time per centimeter of distance: \[t_{1cm} = \frac{2 \times 0.01m}{343 m/s} = 58.3\mu s\] (Factor of 2 because sound travels to object and back)

  2. Evaluate ADC-based approach:

    • If we sample analog echo signal at 100kHz: 10us between samples
    • Time resolution: 10us corresponds to 0.17cm
    • Problem: HC-SR04 outputs digital pulse, not analog - ADC not appropriate
  3. Evaluate timer-based approach (correct method):

    • ESP32 timer at 80MHz: 12.5ns resolution
    • Theoretical distance resolution: 0.5 x 343 m/s x 12.5ns = 0.002mm
    • Far exceeds 1cm requirement
  4. Implement with pulseIn() timing (try on Wokwi):

    duration_us = pulseIn(ECHO_PIN, HIGH);  // Measures pulse width
    distance_cm = duration_us / 58.3;       // Convert to distance
  5. Consider temperature compensation:

    • Speed of sound: \(v = 331.3 + 0.606 \times T_{celsius}\) m/s
    • At 0C: v = 331.3 m/s (time factor = 60.3 us/cm)
    • At 40C: v = 355.5 m/s (time factor = 56.2 us/cm)
    • 7% error if temperature not compensated over 0-40C range

Result:

  • Use hardware timer capture, not ADC, for pulse width measurement
  • Distance calculation: distance_cm = duration_us * speed_of_sound / 20000
  • With temperature compensation: <1cm accuracy achievable

Key Insight: ADCs are for converting analog voltage levels to digital values. For timing measurements (pulse width, frequency), use hardware timers or input capture peripherals.


19.11 Worked Example 8: DAC Motor Speed Control (8-bit)

Given:

  • DAC Resolution: 8 bits (0-255)
  • \(V_{ref}\) = 5V
  • Motor controller accepts 0-5V (0V = stopped, 5V = full speed)

DAC Formula:

\[V_{out} = V_{ref} \times \frac{\text{Digital Input}}{2^n - 1}\]

Calculate Output Voltages:

Speed Setting Digital Input Calculation DAC Output Motor Speed
Stopped 0 \(5V \times \frac{0}{255}\) 0.00V 0%
25% Speed 64 \(5V \times \frac{64}{255}\) 1.25V 25%
50% Speed 128 \(5V \times \frac{128}{255}\) 2.51V 50%
75% Speed 192 \(5V \times \frac{192}{255}\) 3.76V 75%
Full Speed 255 \(5V \times \frac{255}{255}\) 5.00V 100%

Arduino Code Example (try on Wokwi):

// Control motor speed with DAC output
int speed_percent = 50; // 50% speed
int dac_value = map(speed_percent, 0, 100, 0, 255);

// On ESP32 with built-in DAC (8-bit, 0-3.3V output):
dacWrite(DAC1, dac_value); // Outputs ~1.65V (50% of 3.3V Vref)
// Note: For 0-5V motor control, add an op-amp gain stage after DAC

19.12 Worked Example 9: LED Dimming with Gamma Correction

Human eyes perceive brightness logarithmically, not linearly. A “50% brightness” setting needs gamma correction.

Linear DAC (appears too bright):

  • 50% digital value (128/255) → 2.5V → appears ~75% bright to human eye

Gamma-Corrected DAC (perceptually accurate):

\[\text{Corrected Value} = \left(\frac{\text{Desired Brightness}}{100}\right)^{2.2} \times 255\]

For 50% perceived brightness:

\[\text{Corrected Value} = \left(\frac{50}{100}\right)^{2.2} \times 255 = (0.5)^{2.2} \times 255 = 0.2176 \times 255 \approx 56\]

Comparison:

Desired Brightness Linear DAC Value Perceived Brightness Gamma-Corrected DAC Perceived Brightness
25% 64 ~45% (too bright) 12 25%
50% 128 ~75% (too bright) 56 50%
75% 192 ~90% (too bright) 135 75%

Arduino Gamma Correction Code:

// Gamma correction lookup table (saves computation)
const uint8_t gamma8[] = {
    0,  0,  0,  0,  0,  1,  1,  1,  1,  2,  2,  2,  3,  3,  4,  4,
    5,  5,  6,  7,  8,  8,  9, 10, 11, 12, 13, 15, 16, 17, 18, 20,
   21, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 43, 45, 48, 50, 53,
   56, 59, 62, 65, 68, 71, 75, 78, 82, 85, 89, 93, 97,101,105,110,
  114,119,123,128,133,138,143,149,154,160,165,171,177,183,189,195,
  202,208,215,222,229,236,243,250,255 // ... (256 values total)
};

void setLEDBrightness(int percent) {
    int linear = map(percent, 0, 100, 0, 255);
    int corrected = gamma8[linear]; // Apply gamma correction
    analogWrite(LED_PIN, corrected); // PWM output
}

19.13 Knowledge Check Quiz


Math helps the Sensor Squad turn real-world measurements into exact numbers!

19.13.1 The Sensor Squad Adventure: The Temperature Detective

Sammy the Sensor was measuring the temperature of a fish tank. “It is 25 degrees Celsius! I am sending out 0.250 volts.”

ADC Andy looked at the signal. “Okay, I need to turn that into a number Max can understand. My scale goes from 0 to 1023 – that is 1024 steps because I am a 10-bit ADC.”

Max the Microcontroller was curious. “How do you figure out which step number matches 0.250 volts?”

Andy pulled out his formula notebook. “Easy! The fish tank sensor goes from 0 volts (freezing) to 1 volt (boiling hot for fish). So 0.250 volts is one quarter of the way. One quarter of 1023 is about 255. So I tell Max: the reading is 255!”

Lila the LED wanted to know more. “And how does Max turn 255 back into a temperature?”

Max smiled. “I divide 255 by 1023 and multiply by 100 degrees. That gives me about 25 degrees – exactly what Sammy measured!”

Bella the Battery chimed in. “But what if you only had 256 steps instead of 1024?”

Andy nodded. “Then each step would be a bigger jump – about 0.4 degrees instead of 0.1 degrees. I would be less precise. That is why more steps (higher resolution) means better measurements!”

19.13.2 Key Words for Kids

Word What It Means
ADC Formula The math equation that converts voltage to a number
Resolution How many steps you have (more steps = more precise)
Reference Voltage The maximum voltage the ADC can measure
Quantization Error The tiny mistake from rounding to the nearest step
Gamma Correction Special math to make LED brightness look right to human eyes

19.13.3 Try This at Home!

The Step Calculator Game!

  1. Imagine you have a ruler that only goes from 0 to 10 (11 marks)
  2. Measure something that is about 7.3 cm long
  3. With only 11 marks, you would round to 7 – that is like an ADC with low resolution!
  4. Now imagine a ruler with 0 to 100 (101 marks) – you could say 73!
  5. With 0 to 1000 (1001 marks) – you could say 730!

What you learned: More marks on your ruler (higher ADC resolution) means you can measure more precisely. The rounding you do is quantization error – it is small but always there!

Common Mistake: Forgetting ADC Input Impedance in Voltage Dividers

The Error: You design a voltage divider to scale a 5V sensor output to 3.3V for an ESP32 ADC input using 10kΩ resistors. The circuit works in simulation but reads incorrectly in hardware. What went wrong?

Why It Happens: ADC inputs are not infinite impedance. The ESP32 ADC has an internal sample-and-hold capacitor that draws charging current during conversion. High-impedance sources (like voltage dividers with large resistors) cannot supply enough current fast enough, causing the ADC to read lower voltages than expected.

The Numbers:

  • ESP32 ADC: Input impedance ~100 MΩ (DC), but sample capacitor ~6 pF
  • Voltage divider: 10kΩ + 10kΩ = 20kΩ output impedance
  • RC time constant: 20kΩ × 6pF = 120 ns
  • ESP32 ADC conversion time: ~10 µs

Problem: The 120 ns charging time seems fast, but if the source impedance is too high, the sample capacitor doesn’t fully charge during the ADC’s acquisition phase. Result: readings are 5-10% low.

Correct Design Rules:

  1. Keep source impedance low: For ESP32 ADC, keep total resistance <10kΩ
    • Use 1kΩ + 1kΩ divider instead of 10kΩ + 10kΩ
    • Trade-off: Lower resistance = more current draw (power vs accuracy)
  2. Add buffer capacitor: 0.1µF capacitor at ADC input
    • Acts as local charge reservoir
    • Reduces effect of source impedance
    • Place within 1 cm of ADC pin
  3. Use op-amp buffer: For high-impedance sensors (>10kΩ)
    • Rail-to-rail op-amp (MCP6002) between sensor and ADC
    • Input impedance: >1 GΩ (doesn’t load sensor)
    • Output impedance: ~50Ω (drives ADC easily)

Example Calculation:

5V sensor → 3.3V ADC with proper buffering:

Wrong (10kΩ divider): - R1 = 10kΩ, R2 = 20kΩ (ratio: 3.3/5 = 0.66) - Output impedance: 10kΩ || 20kΩ = 6.67kΩ - Problem: Too high for ESP32 ADC

Right (1kΩ divider): - R1 = 1kΩ, R2 = 2kΩ (same ratio) - Output impedance: 1kΩ || 2kΩ = 667Ω ✓ - Current draw: 5V / 3kΩ = 1.67 mA (acceptable for USB-powered)

Best (op-amp buffer): - High-impedance divider (100kΩ resistors, low power) - Op-amp output drives ADC directly - Total current: ~50 µA (battery-friendly)

Verification: Measure the ADC reading with a known precision voltage (e.g., voltage reference IC). If readings are consistently low, source impedance is likely the culprit.

Key Takeaway

ADC and DAC calculations are the practical math behind every sensor and actuator interface in IoT. The key formulas – Digital Output = floor((Vin/Vref) x (2^n - 1)) for ADC and Vout = Vref x (Digital Input / (2^n - 1)) for DAC – along with matching reference voltage to sensor range and applying Nyquist sampling rates, form the essential toolkit for reliable IoT hardware design.

19.14 Concept Relationships: Connecting ADC/DAC Calculations

Calculation Type Key Formula Depends On Used In
ADC Output floor((Vin/Vref) × (2^n - 1)) Input voltage, Reference voltage, Resolution (n bits) All sensor readings
DAC Output Vref × (Digital / (2^n - 1)) Digital value, Reference voltage, Resolution Analog control signals
Step Size Vref / 2^n Reference voltage, Resolution Quantization error analysis
Quantization Error ±(Step Size / 2) Step size Sensor accuracy specs
Sampling Rate fs ≥ 2 × fmax Maximum signal frequency Nyquist compliance
PWM Duty Cycle (Desired Output / Vmax) × 100% Target voltage, Supply voltage LED dimming, Motor speed
Gamma Correction Vout = Vin^γ (γ ≈ 2.2) Linear input value LED brightness perception
Voltage Divider Vout = Vin × (R2 / (R1+R2)) Input voltage, Resistor ratio Level shifting 5V→3.3V
Source Impedance Rtotal = R1 R2

Key Insight: Every ADC calculation starts with matching Vref to the sensor’s output range to maximize resolution. The floor() function causes quantization error (±½ LSB), which sets the fundamental accuracy limit. For output, choose DAC (true analog) for audio/RF or PWM (cheaper, digital) for LED/motor control where fast switching is acceptable.


19.15 Interactive Calculators

Try the formulas from this chapter yourself. Adjust the sliders and inputs to see how ADC output, quantization error, DAC voltage, gamma correction, Nyquist sampling rates, and oversampling resolution change in real time.

19.15.1 ADC Conversion Calculator

19.15.2 DAC Output Calculator

19.15.3 Gamma Correction Calculator

19.15.4 Nyquist Sampling Rate Calculator

19.15.5 Oversampling Resolution Calculator



19.16 Summary

This chapter provided step-by-step worked examples for ADC and DAC calculations:

  • ADC Conversion Formula: Digital Output = floor((Vin / Vref) x (2^n - 1))
  • Temperature Sensor: Match Vref to sensor range for optimal resolution
  • Quantization Error: +/-(Vref / 2^(n+1)) is the inherent measurement uncertainty
  • Nyquist Calculations: f_sample >= 2 x f_max for accurate digitization
  • DAC Output: V_out = Vref x (Digital Input / (2^n - 1))
  • Gamma Correction: Apply non-linear mapping for perceptually-correct LED dimming
  • Real-World Applications: Soil moisture, ultrasonic distance, motor control

These formulas and examples form the practical foundation for sensor interfacing in IoT systems.

19.17 See Also

Within This Series:

Related Sensors:

Practical Application:

External Resources:

Common Pitfalls

A calculation using kOhm, mA, and V (mixing kilo-prefix with base units) produces an answer 1000x too large or too small. Always convert all values to SI base units (Ohm, A, V) before substituting into formulas, then convert the result back to convenient units for the answer.

A LED resistor calculation yielding 0.3 ohm should immediately raise a concern — that would allow 10 A through a standard LED, destroying it. Always sense-check computed values: resistances should be between ohms and megaohms, currents should be microamps to amps, voltages should be millivolts to tens of volts for typical IoT circuits.

Resistors have tolerances (typically +-1% to +-5%). A 10 kohm 5% resistor may be 9.5-10.5 kohm. For a voltage divider, this translates to +-5% error in the output voltage. For precision circuits (ADC references, calibration circuits), use 1% or 0.1% tolerance resistors and verify the circuit’s sensitivity to component variations.

Every current that flows into a circuit must return to the source through the ground path. Analyzing only the forward signal path while ignoring the ground return path leads to missed design issues: shared ground impedance causing crosstalk, inadequate ground plane copper causing voltage drops, and missing return current paths causing incorrect voltage measurements.

19.18 What’s Next?

Now continue to learn about Digital-to-Analog Converters (DACs) and PWM output for actuator control.

Direction Chapter Topic
Previous ADC Fundamentals Theory behind analog-to-digital conversion
Previous Nyquist Sampling Sampling rate requirements and aliasing
Current ADC/DAC Worked Examples Step-by-step conversion calculations
Next DAC and PWM Output Digital-to-analog conversion and PWM control
Next Analog-Digital Electronics Overview Complete ADC/DAC series roadmap

Continue to DAC and PWM Output →