In this lesson, you will learn how to design input stages that maintain a stable voltage level during an analog-to-digital conversion to prevent signal fluctuations from corrupting your data.
The Problem of Moving Targets
When your microcontroller’s Successive Approximation Register (SAR) converter begins a conversion, it doesn't happen instantly. It takes several clock cycles to compare the input voltage against internal reference levels to determine each bit of the digital output. If the input signal changes while this process is underway, the result will be a "mixed" value, leading to significant conversion errors. To solve this, we use a Sample and Hold (S&H) circuit.
How Sample and Hold Works
Think of the S&H circuit as a gatekeeper for the ADC. It consists of three main components: a buffer, a capacitor, and an electronic switch.
- Sampling Mode: The switch is closed. The input signal charges the capacitor so that the voltage across it matches the external signal.
- Hold Mode: The switch opens. Because the capacitor is now disconnected from the source, it "holds" the charge it just acquired. Even if the external signal continues to fluctuate, the voltage presented to the ADC remains locked at the exact value it had when the switch opened.
Implementation Logic
In many embedded systems, this is handled by hardware within the ADC peripheral. When you trigger an ADC conversion, the microcontroller automatically cycles the switch. You can often configure the "Sample Time" in your code to ensure the capacitor has enough time to charge fully based on the impedance of your signal source:
// Example configuration for an ADC sample time
ADC_Config.SampleTime = ADC_SAMPLETIME_15CYCLES;
// 15 cycles gives the capacitor sufficient time to charge
// before the internal switch opens to "Hold" the voltage.
If your input signal is high-impedance, a longer sample time is required because the capacitor needs more current to reach the target voltage. By ensuring the "Hold" phase is stable, you guarantee that the SAR logic is working on a static target, resulting in accurate digital representation of the signal.
Key Takeaway: The Sample and Hold circuit acts as a temporary voltage memory, freezing a dynamic input signal so the ADC has a stable value to process without errors caused by high-frequency noise or signal movement.