A/D and D/A converters
The world is analog and processing is digital. All modern electronics lives on the border between the two, and that border is made of two circuits: the one that translates a voltage into a number and the one that translates a number into a voltage.
01The border between the two worlds
A microphone delivers millivolts that change continuously. A microcontroller only understands numbers. Between the two there is always the same chain, and it is worth having the whole of it in your head before studying each block separately:
02Sampling and quantization
Converting means discretizing twice: in time (sampling) and in amplitude (quantization). They are two different operations, with two different limits, and confusing them is the source of almost all the misunderstandings on this topic.
To be able to reconstruct a signal you have to sample it at more than twice its highest frequency. Otherwise, the components above fs/2 appear reflected inside the useful band as false frequencies that can no longer be separated: this is aliasing.
Once the false frequency has got in, it is indistinguishable from a real signal: no digital filter can remove it. That is why before the converter there is always an analog anti-aliasing filter, a low-pass that cuts off above fs/2. It is the clearest example that filters are not decoration.
The effect can be seen every day: the wheels of a car that in the movies seem to turn backward are aliasing between the rotation frequency and the 24 frames per second of the camera.
Resolution: how many steps there are
Quantization splits the input range into 2n steps. The step is called the LSB (least significant bit) and is the smallest difference the converter can distinguish.
| Bits | Steps | LSB at 5 V | LSB at 3.3 V | Where it is used |
|---|---|---|---|---|
| 8 | 256 | 19.53 mV | 12.89 mV | Simple control, slow sensors. |
| 10 | 1024 | 4.88 mV | 3.22 mV | The typical microcontroller A/D. |
| 12 | 4096 | 1.22 mV | 0.81 mV | General-purpose instrumentation. |
| 16 | 65,536 | 76.3 µV | 50.4 µV | Audio, scales, precision measurement. |
| 24 | 16,777,216 | 0.30 µV | 0.20 µV | Load cells and thermocouples (sigma-delta). |
The LM35 delivers 10 mV per degree. With the 10-bit A/D of a microcontroller and a 5 V reference:
- LSB = 5 V / 1024 = 4.88 mV → resolution = 4.88 / 10 = 0.49 °C.
- The range goes up to 5 V / 10 mV/°C = 500 °C, which the sensor cannot even withstand: almost the whole converter is being wasted.
With the internal 1.1 V reference that many micros have: LSB = 1.1 / 1024 = 1.074 mV → resolution 0.107 °C, and the range becomes 110 °C, very reasonable. Without changing the converter the resolution improved four and a half times, just by choosing the reference well. That is the first design decision in any measurement.
There is a direct relationship between bits and signal-to-noise ratio, which is quoted all the time in audio:
03D/A converters: from number to voltage
They are the easiest to understand and so they are studied first, even though in the chain they come last. The idea is always the same: add weighted currents or voltages, each one with the weight of its bit.
Weighted network (fan-out)
Each bit drives a resistor of value R, 2R, 4R, 8R… all connected to the inverting input of an operational amplifier that works as a summer. The current contributed by each bit is half that of the previous one.
With 8 bits, the ratio between the smallest and the largest resistor is 1 to 128. If R is 10 kΩ, the last bit’s is 1.28 MΩ. And all of them have to be accurate to 0.2% so that the most significant bit does not swamp the least significant one. Making resistors that different and that precise on a chip is very expensive. It works for 4 bits and not much more.
R-2R network (ladder)
It solves the problem at the root: it uses only two values, R and 2R, repeated. The ladder makes the current divide by exactly two at every node, so the binary weight comes from the topology and not from the precision of each resistor.
- Settling time: how long the output takes to settle within ±½ LSB of the final value. It sets the maximum update rate.
- Monotonicity: the output never goes down when the code goes up. A non-monotonic D/A inside a control loop makes it oscillate.
- Differential linearity error (DNL) and integral (INL): how far each step is from the ideal, and how far the whole curve is from the straight line.
The PWM output of a microcontroller, filtered with an R-C, is a low-cost D/A converter: the average value of the square wave is proportional to the duty cycle. It is enough to regulate the brightness of a lamp or the speed of a motor, which is what is seen in simulation and in the peripherals of Year 6. It is not enough for quality audio, because the filter has to be very slow and so the response is too.
04A/D converters: the four architectures
All of them compare the input against an internally generated voltage. What changes is how they search for the value, and from that come the speed, the component count and the price.
a) Flash (parallel)
A resistor ladder generates all the levels at once and a comparator watches each one. The output of the comparators goes through a priority encoder and that is it: the conversion comes out in a single step.
b) Ramp or counter type
A counter feeds a D/A and its output climbs in a staircase until it exceeds the input. At that moment the comparator freezes the counter: what is left inside is the result. It is cheap —it uses what was already seen in counters— but the conversion time depends on the measured value: up to 2n pulses.
c) Successive approximation (SAR)
It is the one found in almost all microcontrollers. Instead of counting one by one, it tests bit by bit, from the most significant to the least, like weighing on a balance with weights of 1/2, 1/4, 1/8 of the range. It always takes n comparisons, whatever the value.
d) Dual slope (integrator)
It charges a capacitor with the input voltage for a fixed time and then discharges it with a reference, measuring how long it takes. It is slow —tens of milliseconds— but incredibly stable, and it has a virtue none of the others has: it averages, so if the integration time is a multiple of the mains period, the 50 Hz hum cancels itself. It is the converter in every bench multimeter, and it is covered in digital measuring instruments.
| Architecture | Speed | Typical resolution | Where it is found |
|---|---|---|---|
| Flash | 1 ns to 100 ns | 6 to 8 bits | Digital oscilloscopes, video, RF. |
| Ramp / counter | µs to ms | 8 to 12 bits | Educational, discrete circuits. |
| Successive approximation | 1 µs to 100 µs | 8 to 16 bits | Microcontrollers, general data acquisition. |
| Dual slope | 10 ms to 400 ms | 3½ to 6½ digits | Multimeters, scales, thermometers. |
| Sigma-delta | ms | 16 to 24 bits | Audio, load cells, thermocouples. |
A 10-bit SAR takes a few microseconds to decide. If during that time the input moves more than ½ LSB, the bits already decided correspond to one value and the remaining ones to another: the result is neither of the two. That is why the input is frozen with a sample & hold (a capacitor and an analog switch) throughout the conversion. In microcontrollers the S/H is inside, and what you have to respect is its acquisition time: if the sensor has a high output impedance, the capacitor does not manage to charge and the reading comes out low. The fix is a voltage follower before the A/D.
05Getting the data out: RS232, RS422 and RS485
Once the signal is a number, it has to be transported. The three standards of the EIA/TIA family solve the same problem with different criteria. They are electrical standards: they say nothing about the meaning of the data, only about voltages, cables and connectors.
| RS232 | RS422 | RS485 | |
|---|---|---|---|
| Signaling | Single-ended, referenced to ground | Differential | Differential |
| Levels | ±3 V to ±15 V (a 1 is negative) | ±2 V to ±6 V between A and B | ±1.5 V to ±6 V between A and B |
| Distance | 15 m | 1200 m | 1200 m |
| Speed | Up to 115 kbps | Up to 10 Mbps | Up to 10 Mbps |
| Nodes | 1 to 1 | 1 driver, 10 receivers | 32 unit loads |
| Direction | Full duplex | Full duplex | Half duplex (2 wires) |
| Where you see it | Consoles, older PLCs, GPS | Encoders, CNC | Modbus, DMX512, Profibus |
The basic difference between RS232 and the other two is not the speed or the distance: it is what the signal is measured against. And everything else follows from that.
- Twisted pair, and A and B must be from the same pair. If they are split across different pairs, the noise rejection is lost.
- Bus topology, not star: one cable that runs past all the nodes with stubs as short as possible.
- 120 Ω termination at both ends of the cable, not in the middle or at each node. It is the characteristic impedance of the twisted pair.
- Bias resistors (fail-safe) so that the bus takes a defined state when no node is transmitting. Without them, the receiver reads noise as if it were data.
- Only one transmitter at a time: since it is half duplex, the protocol (Modbus, for example) defines who talks and when.
- The ground is also carried along, even though the signal is differential: the receiver’s common-mode range is −7 V to +12 V, and in a factory that difference is easily exceeded.
A microcontroller delivers 0 V and 5 V; RS232 asks for +12 V for the 0 and −12 V for the 1. Between the two there is always a level shifter: the classic MAX232, which generates the negative voltages with a charge pump and four capacitors. Connecting an RS232 port directly to a microcontroller pin destroys it.
06In the lab
Build the ladder with 10 kΩ and 20 kΩ resistors at 1%, with four switches to ground or to 5 V. Measure the output at all sixteen codes and build the table of theoretical versus measured. The theoretical step is 5/16 = 312.5 mV. Plot it and calculate the maximum error. Then repeat with 5% resistors and compare: you can see directly why tolerance rules. Commercial values are chosen with the resistor calculator.
Connect a CD4040 counter to the R-2R network above and a 1 kHz clock. On the oscilloscope a staircase ramp appears. Count the steps, measure the height of each one and verify that the period of the ramp is 16 times that of the clock. It is a digital sawtooth generator.
With a digital oscilloscope on a slow timebase, inject a sine wave of increasing frequency. Past a certain frequency, the screen shows a wave that is slower than the real one, one that does not exist. Note at what frequency it starts and compare it with the sampling rate declared by the instrument. It is the demonstration that aliasing is not a technicality.
With a microcontroller, read a fixed voltage from a battery and show the raw code over the serial port. Note how much the last digit fluctuates. Then average 64 readings and look again: the fluctuation drops. That is the oversampling technique, which adds half an effective bit each time the number of samples is quadrupled.
07Common mistakes
| Symptom | Usual cause |
|---|---|
| The A/D reading jumps several counts with the input still | Noise on the reference or on the ground. Decouple Vref, separate the analog ground from the digital one and average readings. |
| The reading is always lower than it should be | Sensor output impedance too high for the S/H acquisition time. Put in a follower with an op amp. |
| A slow signal appears that does not exist | Aliasing: the anti-aliasing filter is missing or the sampling frequency is insufficient. |
| The D/A steps backward at some code | Non-monotonic converter, or network resistors out of tolerance. It is most noticeable at the 0111 → 1000 transition. |
| The converted value depends on temperature | Poor-quality voltage reference. Use a dedicated reference, not the supply itself. |
| The RS485 bus works on the bench and fails on the plant floor | Missing 120 Ω termination, or the cable is not twisted pair, or the ground was not carried along and the common-mode range was exceeded. |
| The serial port receives garbage | Wrong baud rate, or TTL levels connected directly to RS232 without an adapter. |
| On RS485 the first character of each message is lost | The direction control (DE/RE) switches too late. The transmitter has to be enabled before sending the first bit and released after the last one. |
08Self-assessment
A 12-bit A/D with a 3.3 V reference: what is its LSB?
3.3 V / 4096 = 0.806 mV, and the quantization error is half of that: ±0.40 mV.
Why does an anti-aliasing filter have to be analog and not digital?
Because it has to act before sampling. Once sampled, the high-frequency component has already been reflected into the useful band and is indistinguishable from a legitimate signal: no later processing can separate it.
What advantage does the R-2R network have over the weighted network?
It uses only two resistor values instead of a progression that doubles. With 8 bits, the weighted one needs values from R to 128R with 0.2% precision; the R-2R needs many equal resistors, which is exactly what an integrated process does well.
A 10-bit SAR with a 1 MHz clock: how long does a conversion take?
Ten comparisons, one per bit, plus one sampling cycle: approximately 11 µs. And that time is the same for any input voltage, unlike the ramp converter.
Why does the dual-slope converter reject 50 Hz hum?
Because it integrates the input for a fixed time. If that time is an exact multiple of the mains period (20 ms), the sinusoidal interference contributes as much in the positive half-cycles as in the negative ones and its average is zero.
How many comparators does an 8-bit flash need?
28 − 1 = 255. With 10 bits it would be 1023 and with 16 bits, 65,535: that is why flash does not go beyond 8 or 10 bits in practice.
You want to measure an audio signal of up to 15 kHz. What sampling frequency is needed?
More than 30 kHz. In practice quite a bit more is used, because the anti-aliasing filter does not cut off abruptly: the CD’s 44.1 kHz leaves a margin between the 20 kHz of the signal and the 22.05 kHz of Nyquist so that the filter has room to roll off.
Why does RS485 withstand 1200 m and RS232 only 15?
Because RS485 is differential: the receiver measures the difference between two twisted wires and the noise, which enters equally on both, cancels. RS232 measures against ground, so every potential difference and all induced noise add to the signal.
What is the 120 Ω resistor at the end of an RS485 bus for?
To match the cable to its characteristic impedance and prevent the signal from bouncing back from the open end. Without termination, the echoes overlap the data and errors appear that get worse the faster you transmit.
With an 8-bit, 5 V A/D, can you measure the output of a type K thermocouple (about 40 µV/°C)?
Not directly: the LSB is 19.5 mV, almost 500 °C per step. You have to amplify the thermocouple by about 250 with an instrumentation amplifier and also use a converter with more bits, or simply a 24-bit sigma-delta designed for that.