Curve fitting for analog sensor

The sensor is an old water level meter that I got from a local industrial company, they told me it worked (it does) but they have to replace them every 20 years. It’s powered by 24V and outputs a 4-20mA signal; it seems to be an old industrial standard.

I made a simple circuit with an OP-Amp measuring the voltage drop over a resistor, the OP-AMP has a gain of 100 so the signal out is 400-2000 mV!

There is however a linearity problem with the OP-Amp and I want to solve this in software.

I’ve done some testing with the sensor and come up with a polynomial function describing the measured signal but I’m not sure how I can apply this to my code.

The testing was conducted by using the multi meter to measure current and a multi meter to measure the voltage output from the OP-Amp, I made 4 identical circuits and had 5 test points for each test. I then put them the data into excel and used the graph function to display it and create a poly-function of it. It’s just an error of 3-4% but it’s still nice learning how to do things properly!

Below is an image of the averaged graph and the poly-function.

Is there a better way to doing this?

I am aware that I could use multimap but I want to learn something, so would prefer to use the "harder" way

Thanks in advance for any help!

Poly-function: y = -5,0833x^4 + 74x^3 - 369,92x^2 + 875x - 411

It would be easier to fix your circuit, so please post that (hand drawn schematic diagram, not Fritzing).

Most people just use a 250 Ohm resistor on a 4-20 mA output, and read the voltage across it directly using the Arduino ADC. The output is perfectly linear with input, 1-5V.

A simple, properly-designed op-amp current-to-voltage converter should be, for all practical purposes, perfectly linear. Fix THAT, rather than kludge-ing it in software…

Ray L.

I did some more testing and I’m not really sure what’s going on. It seems like the 4-20mA signal isn’t very linear itself...

Could anybody inform me what’s going on?

Maybe that’s why they toss em after 20years…

Make a lookup table of the output versus the water level, and use it.