Catto / Topic Map · Digital Electronics II Year 5
Digital Electronics II · 96 h · Topic 2 of 4

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.

Conversion Sampling R-2R Successive approximation RS485

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:

Sensorquantity → V Conditioningamplify, filter S / Hholds the value A / DV → number ProcessingµC or µP D / Anumber → V analog world digital world Each block adds its own error: the whole system cannot be better than the worst of them
Figure 1. The typical chain. In a modern microcontroller, everything from the S/H to the D/A is inside the chip; the technician’s work is left to the sensor, the signal conditioning and the filter.

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.

t v original signal sampling instants held value (S/H) 8 samples per cycle
Figure 2. Sampling and holding, animated. The cursor runs along the signal; at each sampling instant the value is taken and held until the next one. The reconstructed output is the orange staircase: it resembles the original, but it is not the original.
Sampling theorem (Nyquist-Shannon)

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.

fs>2·fmax
Aliasing cannot be fixed afterward

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.

LSB=Vref2ne=±12LSB Quantization error is unavoidable: even with a perfect converter, the actual value lies somewhere within the step.
BitsStepsLSB at 5 VLSB at 3.3 VWhere it is used
825619.53 mV12.89 mVSimple control, slow sensors.
1010244.88 mV3.22 mVThe typical microcontroller A/D.
1240961.22 mV0.81 mVGeneral-purpose instrumentation.
1665,53676.3 µV50.4 µVAudio, scales, precision measurement.
2416,777,2160.30 µV0.20 µVLoad cells and thermocouples (sigma-delta).
Example · Measuring temperature with an LM35

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:

SNR≈6.02·n+1.76dB Each added bit is 6 dB. The 16 bits of the CD give 98 dB; the 8 bits of a toy, 50 dB, and that is why you can hear the background noise.

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.

Why it is hardly ever used

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.

R 2R 2R 0 1 b0 0 R 2R 0 1 b1 0 R 2R 0 1 b2 0 R 2R 0 1 b3 0 ground (bit = 0) · Vref (bit = 1) Only two resistor values: the topology is what sets the binary weights Vout Vref 16 equal steps input code 0 → 15
Figure 3. A 4-bit R-2R network, animated. Each switch connects its 2R to ground (bit at 0) or to the reference (bit at 1). On the right, the output: sixteen equal steps. With 8 bits the steps number 256 and the staircase already looks like a ramp.
Vout=Vref·D2n D is the binary input number. With Vref = 5 V and 8 bits, code 200 gives 5 × 200/256 = 3.906 V.
The three specifications to look at on the datasheet
  • 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.
Where a D/A shows up without being called that

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.

Vref ground Vin Priority encoder Output · 3 bits b0 b1 b2 2n − 1 comparators: 3 for 2 bits, 255 for 8 bits The fastest there is: nanoseconds. Also the most power-hungry and the most expensive. Digital oscilloscopes, video, software-defined radio.
Figure 4. Flash converter. One comparator for each level: the input is compared against all of them at the same time and the result appears immediately. The number of comparators doubles with every bit, and that is its limit.

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.

full scale · 255 counts 0 Vin = 161 counts 128 b7 1 192 b6 0 160 b5 1 176 b4 0 168 b3 0 164 b2 0 162 b1 0 161 b0 1 Green: the trial fits and the bit stays at 1 · red: it overshoots and the bit goes back to 0 Result: 10100001 = 161 · eight steps for eight bits, whatever the input
Figure 5. Successive approximation, animated. Each step tests one bit: if the trial value overshoots the input, the bit is discarded; if not, it is kept. Eight steps and the number is ready, whatever the input voltage.

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.

ArchitectureSpeedTypical resolutionWhere it is found
Flash1 ns to 100 ns6 to 8 bitsDigital oscilloscopes, video, RF.
Ramp / counterµs to ms8 to 12 bitsEducational, discrete circuits.
Successive approximation1 µs to 100 µs8 to 16 bitsMicrocontrollers, general data acquisition.
Dual slope10 ms to 400 ms3½ to 6½ digitsMultimeters, scales, thermometers.
Sigma-deltams16 to 24 bitsAudio, load cells, thermocouples.
The sample & hold is not optional

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.

RS232RS422RS485
SignalingSingle-ended, referenced to groundDifferentialDifferential
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
Distance15 m1200 m1200 m
SpeedUp to 115 kbpsUp to 10 MbpsUp to 10 Mbps
Nodes1 to 11 driver, 10 receivers32 unit loads
DirectionFull duplexFull duplexHalf duplex (2 wires)
Where you see itConsoles, older PLCs, GPSEncoders, CNCModbus, 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.

A B A − B what the receiver sees induced noise on both wires 1 0 1 1 0 1 0 0 data
Figure 6. Common-mode rejection, animated. The noise that the installation induces in the cable enters equally on both wires. The receiver does not look at either of them against ground: it looks at the difference, and there the noise cancels. It is the same idea as the instrumentation amplifier.
Practical rules for an RS485 bus
  • 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.
RS232 levels are inverted and large

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

Lab 1 · A 4-bit D/A with R-2R

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.

Lab 2 · Digital ramp

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.

Lab 3 · Aliasing with the generator and the oscilloscope

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.

Lab 4 · Real resolution of an A/D

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

SymptomUsual cause
The A/D reading jumps several counts with the input stillNoise 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 beSensor 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 existAliasing: the anti-aliasing filter is missing or the sampling frequency is insufficient.
The D/A steps backward at some codeNon-monotonic converter, or network resistors out of tolerance. It is most noticeable at the 0111 → 1000 transition.
The converted value depends on temperaturePoor-quality voltage reference. Use a dedicated reference, not the supply itself.
The RS485 bus works on the bench and fails on the plant floorMissing 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 garbageWrong baud rate, or TTL levels connected directly to RS232 without an adapter.
On RS485 the first character of each message is lostThe 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.

Development of the topic “A/D and D/A converters” of Digital Electronics II (Year 5), based on the “Curriculum Proposal – Second Cycle of the Technical-Vocational Track, Secondary Education – Electronics,” Ministry of Education of the Province of Córdoba, DGETyFP. Back to the Topic Map · catto.ar