# Calibrating for differing slopes in potentiometer readout

I have a potentiometer that we're using to measuring the sliding distance (0 to 15mm range) on a medical device.

Doing some testing we're seeing that the low and high end of the measurements are slightly off when measuring it on a measurement attached with known distances on it. I plotted the voltage readout vs the distance in excel to get a better idea of the sensor readout and it's linear for the most part but not the same slope as the in the 0-2 mm range as rest of the measurements afterwards.

So is there a way to build this calibration into the code?

You can calibrate the device by measuring the voltage at various intervals (the finer the better) and then fitting a curve to the result. This way you can find an empirical function to get distance from voltage.

Post a plot of your measurements, and forum members will be happy to suggest candidate functions and approaches for fitting the data.

ThomSal:
I have a potentiometer that we're using to measuring the sliding distance (0 to 15mm range) on a medical device.

Doing some testing we're seeing that the low and high end of the measurements are slightly off when measuring it on a measurement attached with known distances on it. I plotted the voltage readout vs the distance in excel to get a better idea of the sensor readout and it's linear for the most part but not the same slope as the in the 0-2 mm range as rest of the measurements afterwards.

So is there a way to build this calibration into the code?

If you disassemble a potentiometer, you will discover there is a physical connection made to each end of the resistive element. The connection cannot ever be linear close to the connection. Same will be found at the other end of rotation.

Best to avoid using resistance at the extremes.

Paul

Here are a few graphs from the data (2nd, 3rd and 4th polynomial when adding a trendline in excel).

Equations-
2nd: y = 0.8295x2 + 44.003x
3rd: y = -0.1262x3 + 3.3362x2 + 33.148x
4th: y = 0.0442x4 - 1.3285x3 + 13.077x2 + 11.307x

A medical device? Are they any safety issues if the potentiometer malfunctions?

No just used for some external measurements. The greater the accuracy the better

I would go with the third order poly; the fourth order appears to be overfitted. But you need many more calibration points.

If you swap X and Y for the plot and refit, you get the inverse function.

Gotcha. Thanks for the replies.

I'm pretty new to all of this so sorry for the dumb questions but what function would I need to use to implement this?

Currently I'm just using the map() for the inputs to get the corresponding distance readout.

This is a mathematical statement function:

y = -0.1262x^3 + 3.3362x^2 + 33.148x

This is a C/C++ statement function that implements the above mathematical statement function:

``````float x=some_value, y;
...

y = -0.1262*x*x*x + 3.3362*x*x + 33.148*x;
``````

Or, slightly faster:

``````float x=some_value, y;
...

y = (((-0.1262*x) + 3.3362)*x + 33.148)*x;
``````

You need many more data points and I wouldn't be surprised if in the end you find out the best fit is after all a simple linear regression (for the 5-95% range at least, the ends of the range will likely give issues).

There are basically two types of pots: linear and logarithmic (the latter are used for volume controls and the like). Linear is of course never perfectly linear; you may find information in the part's data sheet on how close linearity you should expect.

The few data points you do have point to it pretty darn close to linearity to me.