EC-9.4 Analog In and Analog Out on the Chip

en

What this is and why it exists

The converter inside a microcontroller is where the physical world becomes a number.

Its datasheet figures are also the most over-trusted numbers in embedded work. Twelve bits of resolution on the front page very rarely means twelve bits of usable accuracy on a real board.

Knowing why is the difference between a reading and a guess. This topic is about the converter you already have, not about how to design one.

The vocabulary

  • Reference voltage — the voltage the converter compares its input against.
  • Sample and hold — a small capacitor charged from the input and then frozen during conversion.
  • Acquisition time — the time allowed for that capacitor to charge before conversion begins.
  • Resolution — how finely the input range is divided.
  • Offset error — a constant shift in every reading.
  • Gain error — an error proportional to the reading.
  • Non-linearity — a departure from equal step sizes across the range.
  • Direct memory access — a unit that moves data without the processor doing it.

The mental model

A converter does not measure volts. It measures the input as a fraction of its reference. Everything else follows from that sentence.

If the reference is the supply, and the supply sags when a motor starts, every reading moves. If the reference is a dedicated part with its own accuracy figure, the readings are as good as that part. Choosing the reference is therefore choosing the accuracy of the whole measurement, and it is a decision people make by accident.

There is a useful trick hidden in the same fact. Suppose you are measuring something that is itself a fraction of the supply, such as a resistive divider fed from it. Using the supply as the reference then cancels the supply's error entirely. Ratiometric measurement is the name for it, and it is free.

The sample and hold is the second thing to understand, because it silently causes low readings. Before converting, the input charges a small internal capacitor. That charging happens through your circuit's impedance.

If the source impedance is too high, the capacitor does not reach the input voltage in the allowed acquisition time. The conversion then reports a value between the true one and whatever was there before, and no error is flagged. The datasheet gives a maximum source impedance for this reason, and the fix is either a longer acquisition time or a buffer amplifier in front.

Resolution and accuracy are different questions and datasheets encourage the confusion. Resolution is how finely the range is divided: twelve bits over three volts gives steps of about seven hundred microvolts. Accuracy is how close the answer is to the truth.

Offset error, gain error and non-linearity are all listed separately, usually in units of the smallest step. Add them up honestly and a twelve-bit converter often gives nine or ten trustworthy bits. Adding board noise usually costs another one or two.

Starting a conversion can be done three ways and the choice matters more than it looks. Software starting means the sample is taken whenever the code got around to it. The time between samples varies with whatever else the program did.

Timer starting fixes that: the hardware takes the sample at an exact instant, which is what any later frequency analysis depends on. Uneven sampling corrupts a spectrum in a way that cannot be corrected afterwards.

Direct memory access completes the arrangement by storing the result without the processor waking. A timer starts the conversion, the converter finishes it, and the transfer unit writes it to a buffer. Whole blocks of samples arrive at an exact rate with the processor asleep.

Analog output is the other direction and many parts have none at all. The standard substitute is pulse width modulation through a low-pass filter. A square wave whose high proportion is adjustable, averaged by a resistor and capacitor, gives a voltage proportional to that proportion.

It is enough for a motor, a lamp, a bias voltage or an audio tone. It is not enough where the ripple or the delay of the filter matters, and choosing the filter is a trade between those two.

Finally, grounding, which is where good converters are ruined. Digital outputs switching hard put current through the ground the converter measures against. Separate analog ground and analog supply pins exist precisely so that current does not pass under the converter.

Using them properly is what makes the last two bits mean anything. It is the point where this topic and the measurement module meet, and the reasoning there applies here without change.

What you should now be able to explain or do

  • Explain why the reference sets the accuracy of every reading, and when a ratiometric measurement cancels its error.
  • Work out whether a source impedance is low enough for the acquisition time available.
  • Read offset, gain and non-linearity figures and estimate the usable bits of a converter.
  • Choose between software, timer and transfer-unit driven conversion, and say why uneven sampling cannot be repaired.
  • Produce an analog output using pulse width modulation and a filter, and state the trade in choosing that filter.
  • Wire the analog ground and supply of a mixed-signal part so that switching currents stay away from the converter.

Check yourself

The reference is the supply, and the supply is sagging. The converter reports a fraction of its reference, so a moving reference moves every reading.

The internal sampling capacitor cannot charge fully within the acquisition time. The conversion then reports a value part way between the true input and the previous one, with no error indication.

Offset, gain and non-linearity errors are specified separately and add up, and board noise adds more. The headline figure is resolution, which is not the same as accuracy.

Because the samples then occur at exactly equal intervals. Software timing varies with whatever else ran, and uneven sampling corrupts any later frequency analysis irreparably.

Pulse width modulation into a low-pass filter. The average of the square wave is proportional to its high proportion, and the filter turns that average into a voltage.

Go deeper

Back to Analog In and Analog Out on the Chip: work through the checklist