In this lesson, you will learn how the Successive Approximation Register (SAR) ADC uses a binary search algorithm to systematically narrow down the value of an analog signal into a digital word.
The Binary Search Logic
Unlike flash ADCs that compare all levels at once, or ramp ADCs that count up slowly, the SAR ADC acts like a person playing a game of "Higher or Lower" with a scale of 0 to 100. Instead of guessing randomly, you start by asking, "Is it higher than 50?" Then, depending on the answer, you ask, "Is it higher than 75?" or "Is it higher than 25?"
In an embedded system, the SAR controller performs this same logic across N clock cycles for an N-bit converter. It uses a Successive Approximation Register (SAR) to hold the result as it tests each bit, starting from the Most Significant Bit (MSB).
The Step-by-Step Execution
- Initialize: Clear the SAR and set the MSB to
1(the midpoint of the range). - Compare: The internal DAC converts this digital value to an analog voltage and compares it to the input signal using a comparator.
- Decide:
- If the input is higher, the bit remains
1. - If the input is lower, the bit is cleared to
0.
- If the input is higher, the bit remains
- Iterate: The controller moves to the next bit (the next most significant) and repeats the process.
This process can be visualized as setting bits in a register one by one:
// Simplified logic for an 8-bit SAR conversion
uint8_t sar_register = 0;
for (int i = 7; i >= 0; i--) {
sar_register |= (1 << i); // Test the current bit
if (dac_voltage(sar_register) > analog_input) {
sar_register &= ~(1 << i); // Bit was too high, turn it off
}
}
// sar_register now holds the digital representation
By the end of the final cycle, the SAR contains the precise binary representation of the input voltage. Because it determines one bit per clock cycle, an 8-bit conversion requires exactly 8 comparison steps, offering an excellent balance between speed and hardware complexity.
Key Takeaway: The SAR ADC converts analog signals to digital by using a binary search algorithm, testing each bit from MSB to LSB and keeping the results that do not exceed the actual input voltage.