ADC interfacing and sensor reading
ADC resolution, code conventions, ADC types, sampling and anti-aliasing, on-chip ADC timing and signal conditioning, with LM35 and 4–20 mA scaling examples.
Drafted with Aria, reviewed by the AiCanCode.org team. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Temperature, pressure, position, load and flow sensors mostly give an analog voltage or current. The ADC is where the physical world becomes a number, and its resolution, reference, sampling rate and the signal conditioning in front of it decide whether a controller sees 30.3 °C or noise. Converting raw counts back to engineering units correctly is a daily task for every mechatronics engineer.
Key ideas
What an ADC does. It samples an input voltage at discrete instants and quantises each sample into one of 2ⁿ integer codes, where n is the resolution in bits. Code 0 corresponds to 0 V (or V_ref−) and the full-scale code to V_ref.
Resolution and LSB. The step size (1 LSB) is V_ref/2ⁿ. A 10-bit ADC with 5 V reference resolves 4.88 mV. Quantisation error is at most ±½ LSB with ideal rounding (0 to −1 LSB if the converter truncates).
Code conventions. Microcontroller datasheets (e.g. AVR) define code = V_in × 2ⁿ / V_ref, truncated, with maximum code 2ⁿ − 1. Many textbooks instead use (V_in / V_ref) × (2ⁿ − 1). The two differ by less than 1 LSB; use the one your datasheet or question states.
Resolution is not accuracy. Offset error, gain error, non-linearity, reference tolerance and noise can make a 12-bit reading accurate to only 9–10 bits. A stable, low-noise reference matters as much as the bit count.
ADC types.
- SAR (successive approximation): a binary search using an internal DAC and comparator — one bit per clock, n clocks plus sampling. The standard on-chip MCU ADC (8–12 bits, up to a few MSPS).
- Flash: 2ⁿ − 1 comparators in parallel — fastest, but large and power-hungry, so low resolution.
- Sigma-delta: oversampling with noise shaping — 16–24 bits, slow; used for load cells and thermocouples.
- Dual-slope (integrating): very good noise rejection, slow; digital multimeters.
Sampling. The Nyquist criterion says the sampling rate must exceed twice the highest signal frequency (f_s > 2f_max), otherwise higher frequencies alias into false low-frequency readings. An analog low-pass (anti-aliasing) RC filter before the ADC removes noise above f_s/2. Sampling faster than needed does not improve resolution by itself, though averaging several samples reduces random noise.
On-chip ADC practice (ATmega example). 10-bit SAR, multiplexed input channels, reference selectable (AVCC, internal 1.1 V, external AREF). The ADC clock must be 50–200 kHz for full resolution, so a 16 MHz CPU uses prescaler 128 (125 kHz). A normal conversion takes 13 ADC clocks. Results are read from ADCL then ADCH.
Signal conditioning brings the sensor signal to the ADC range: amplification (op-amp, instrumentation amplifier for bridges and thermocouples), offset shifting, filtering, isolation and protection (series resistor, clamp diodes). Match the sensor's full range to V_ref to use all the codes.
Industrial signals. PLC and process instruments use 0–10 V and 4–20 mA. A 4–20 mA loop is converted to voltage with a precision shunt (250 Ω gives 1–5 V). The live zero at 4 mA lets the system detect a broken wire (0 mA) and powers two-wire transmitters.
Formulas
LSB = V_ref / 2ⁿ
- LSB = smallest voltage step (V); V_ref = reference voltage (V); n = number of bits.
D = int(V_in × 2ⁿ / V_ref) and V_in ≈ D × V_ref / 2ⁿ
- D = output code (dimensionless), 0 ≤ D ≤ 2ⁿ − 1 (datasheet convention).
Quantisation error = ±½ LSB (rounding)
f_s > 2 f_max
- f_s = sampling rate (samples/s); f_max = highest frequency in the input (Hz).
t_conv = N_clk / f_ADC
- Conversion time (s); N_clk = ADC clocks per conversion (13 for a normal ATmega conversion); f_ADC = ADC clock (Hz).
SNR_ideal = 6.02 n + 1.76 (dB)
- Ideal signal-to-quantisation-noise ratio for a full-scale sine wave.
x = x_min + (I − 4 mA) / (16 mA) × (x_max − x_min)
- Engineering value from a 4–20 mA signal I; x_min, x_max = transmitter range.
V = I × R_shunt
Worked examples
Example 1 (standard). An LM35 temperature sensor (10 mV/°C, 0 V at 0 °C) feeds a 10-bit ADC with V_ref = 5 V. The ADC reads 62. Find the temperature and the temperature resolution.
V_in = D × V_ref / 2ⁿ = 62 × 5 / 1024 = 0.3027 V.T = V_in / (10 mV/°C) = 302.7 mV / 10 mV/°C = 30.3 °C.LSB = 5 / 1024 = 4.883 mV→ 0.49 °C per count.
Answer: T ≈ 30.3 °C, resolution ≈ 0.49 °C/count. (Using the internal 1.1 V reference would improve this to about 0.11 °C/count for temperatures up to 110 °C.)
Example 2 (GATE level). A 0–10 bar pressure transmitter gives 4–20 mA, read through a 250 Ω shunt by a 12-bit ADC with V_ref = 5 V. (a) The ADC reads 2458; find the pressure. (b) Find the pressure resolution. (c) The ADC's 125 kHz clock needs 13 clocks per conversion; what is the highest signal frequency that can be sampled without aliasing?
V = 2458 × 5 / 4096 = 3.0005 V.I = V / R = 3.0005 / 250 = 12.002 mA.P = 0 + (12.002 − 4) / 16 × 10 = 5.00 bar.LSB = 5 / 4096 = 1.221 mV→ current step = 1.221 mV / 250 Ω = 4.883 µA → pressure step = 4.883 µA × (10 bar / 16 mA) = 0.00305 bar.t_conv = 13 / 125 × 10³ = 104 µs→ f_s = 9615 samples/s → f_max < f_s / 2 = 4.8 kHz.
Answers: P ≈ 5.00 bar; resolution ≈ 0.003 bar per count; signals must stay below about 4.8 kHz. Note that only codes from about 819 (1 V) up to full scale are used, roughly 80 % of the range.
Common mistakes
- Mixing the 2ⁿ and 2ⁿ − 1 conventions mid-problem — pick one and state it.
- Treating resolution as accuracy; the reference tolerance alone can exceed 1 LSB.
- Leaving inputs above V_ref or below 0 V unprotected — readings clip and the pin can be damaged.
- Forgetting the 4 mA offset in 4–20 mA scaling.
- Sampling below 2f_max, or omitting the anti-aliasing filter, so noise aliases into the reading.
- Reading ADCH before ADCL on AVR (the result locks in the wrong order).
- Using a noisy supply (AVCC without filtering) as the reference.
For GATE ME
Expect numericals on resolution (LSB size), output code for a given input, input voltage for a given code, quantisation error, SNR from bit count, Nyquist sampling rate and sensor scaling (LM35, 4–20 mA, bridge outputs), plus MCQs on ADC types and their speed–resolution trade-offs. Practise keeping mV and V straight and stating which code convention you use.
Quick check
- What is 1 LSB of an 8-bit ADC with V_ref = 3.3 V?
- What code does a 10-bit ADC (V_ref = 5 V) give for 2 V input (datasheet convention, truncated)?
- Which ADC type is fastest?
- A 4–20 mA, 0–100 °C transmitter reads 8 mA. What is the temperature?
- What is the ideal SNR of a 12-bit ADC?
Answers: 1. 12.9 mV 2. 409 3. Flash 4. 25 °C 5. ≈ 74 dB
Interview questions
All Microcontrollers, PLC and Industrial Automation interview questionsTry answering each one aloud before you open it.
1.What is an ADC and why is it important in microcontroller applications?Concept
An ADC, or Analog-to-Digital Converter, is a device that converts analog signals, which are continuous in nature, into digital signals, which are discrete. This conversion is crucial in microcontroller applications because microcontrollers process digital data. ADCs allow microcontrollers to interface with the real world by reading analog inputs from sensors and converting them into a format that the microcontroller can process.
2.Explain the basic working principle of an ADC.Concept
An ADC works by sampling an analog input signal at regular intervals and converting each sample into a digital value. The conversion involves quantization, where the continuous range of the analog signal is divided into discrete levels. The resolution of an ADC, typically measured in bits, determines how many discrete levels it can distinguish. For example, an 8-bit ADC can represent 256 different levels.
3.What are the different types of ADCs commonly used in microcontrollers?Concept
Common types of ADCs used in microcontrollers include Successive Approximation Register (SAR) ADCs, Sigma-Delta ADCs, and Flash ADCs. SAR ADCs are widely used due to their balance of speed and accuracy. Sigma-Delta ADCs are known for high accuracy and are used in applications requiring precise measurements. Flash ADCs are the fastest but are typically used in applications where speed is more critical than resolution.
4.Why is a reference voltage important in ADC operations?Application
The reference voltage in an ADC defines the range of input voltages that can be converted into digital values. It acts as a scale against which the input voltage is measured. A stable and accurate reference voltage is crucial because it directly affects the accuracy and precision of the ADC's output. If the reference voltage fluctuates, it can lead to incorrect digital representations of the analog input.
5.What happens if the input voltage to an ADC exceeds its reference voltage?Application
If the input voltage to an ADC exceeds its reference voltage, the ADC will saturate and output its maximum digital value. This is because the ADC cannot represent voltages higher than its reference voltage. As a result, any information about the actual input voltage beyond the reference voltage is lost, leading to potential errors in the system's operation.
6.How does the resolution of an ADC affect its performance?Application
Resolution n sets the number of codes, 2^n, and so the step size V_ref/2^n — a 10-bit ADC on 5 V resolves about 4.9 mV, a 12-bit one about 1.2 mV — and the ideal SNR rises about 6 dB per bit. Higher resolution does not guarantee accuracy: offset, gain and linearity errors, reference tolerance and noise can swamp the extra bits. Higher-resolution converters are also usually slower (SAR needs one clock per bit; sigma-delta trades speed for bits).
7.Why might a low-pass filter be used before an ADC in a sensor reading application?Application
A low-pass filter is often used before an ADC to remove high-frequency noise from the analog signal. This is important because noise can cause errors in the digital representation of the signal. By filtering out frequencies higher than the Nyquist frequency, the low-pass filter helps ensure that the ADC receives a clean signal, leading to more accurate and reliable sensor readings.
8.Calculate the digital output of a 10-bit ADC with a reference voltage of 5V when the input voltage is 2V.Numerical
The digital output of an ADC can be calculated using the formula: Digital Output = (Input Voltage / Reference Voltage) × (2^n - 1), where n is the number of bits. For a 10-bit ADC with a 5V reference, the calculation is: Digital Output = (2V / 5V) × (2^10 - 1) = 0.4 × 1023 = 409.2. The digital output is approximately 409.
9.A sensor outputs a voltage range of 0 to 3V. What reference voltage should be used for a 12-bit ADC to maximize its resolution?Numerical
To maximize the resolution of a 12-bit ADC, the reference voltage should match the maximum output voltage of the sensor. In this case, the sensor outputs a maximum of 3V, so the reference voltage should be set to 3V. This ensures that the full range of the ADC's resolution is utilized, providing the most accurate digital representation of the sensor's output.
10.Explain how a PLC can use ADCs to interface with analog sensors in an industrial automation system.Application
In an industrial automation system, a PLC (Programmable Logic Controller) can use ADCs to interface with analog sensors by converting the analog signals from the sensors into digital data that the PLC can process. The ADCs are typically integrated into the PLC or connected externally. Once the analog signals are converted to digital, the PLC can use this data to monitor and control processes, make decisions, and execute control algorithms based on the sensor inputs.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?