In the comments to my series of blogs on temperature measurement on the late, lamented Microcontroller Central, there was some feeling that a blog on calibration of analog IO might be a good idea.
Let me start by taking you back to some high school math. We know that a straight line graph takes the form y = mx+b (Equation 1), where m is the slope of the line, and b is the intercept on the y axis. There was a spirited discussion on this site on whether this was a linear function. Without getting drawn into that discussion, I am going to refer to the straight line graph as linear for convenience, so sue me if you don't like it. The typical graph is shown below.
Graph showing a straight line relationship.
The way to derive the equation of the line is using this approach:
(x-X1)/(X2-X1) = (y-Y1)/(Y2-Y1)... (Equation 2)
When I learned that as a Cartesian technique, I just accepted it without realizing that it is nothing more than the equation derived from similar triangles. Of course, you can calculate the slope and intercept, but I have found that it is simpler to work with Equation 2 without the extended math operations to get what would probably be floating point numbers. Not every physical relationship can be characterized by a straight line, but it will be a good place to start.
In acquiring or outputting an analog voltage, the analog to digital converter (ADC) or the digital to analog converter (DAC) is rarely the only component in the chain of connections between the signal source and the digital number that the microcomputer will use. Every ADC or DAC has its own shortcomings, including offset voltages and reference tolerance, but throw in an op-amp or two and a multiplexer, and now you have a whole series of gain and offset variations that vary widely between individual units.
Unless you are designing a rather coarse system, you will want to take these variations into account, and the way to do this is by calibration. Calibration allows the system to learn the conversion response to a known stimulus. Provided the system is linear, if you set up two known input points (X1 and X2 in the graph above) and measure the associated output points (Y1 and Y2), you can then work backward from a measured y to get the corresponding input x.
A 0-5V out DAC driving an op-amp to give a 0-10V range.
Let's start simple with a DAC. Let us say that the DAC on the system is a 12-bit device with a 5V reference (so the maximum output is 5V at 0xFFF). The DAC output drives an op-amp; we want a 0-10V output. The way I do it is to set the gain on the op-amp to just over 2 to guarantee that the maximum output will always be higher than 10V for any possible variation on the DAC reference voltage and op-amp gain resistors. (It should go without saying that the op-amp must be powered from a supply of more than 10V.)