# Non-linear functions ...

Discussion in 'Electronic Design' started by Richard Rasker, Nov 24, 2009.

Hi all,

I'm currently working with a mass air flow sensor (a Honeywell AWM3100V, see
http://datasheet.octopart.com/AWM3100V-Honeywell-datasheet-57019.pdf), and
I would like to convert the rather non-linear response curve of this device
into a voltage which bears a linear relationship to the actual air flow.
Ideally, I would like to see the air flow converted in millivolts, so that
it can be fed into a 3.5 digit voltmeter directly.

These are the values (F=flow):
F (ccm) Vout (V)
0 1.00
25 1.90
50 2.67
75 3.27
100 3.75
125 4.17
150 4.50
175 4.80
200 5.00

The first problem was simple: finding a suitable mathematical function which
fits the curve; I looked at something along the lines of
Vout=c1*(1-e^(-F/c2))+1, and it turns out that c1=5 and c2=125 provides a
near-perfect fit. The second problem was to find an inverse function -- no
problem there either: F=-c2*ln(1-(Vout-1)/c1) -- leading to the third and
rather trickier problem, which of course is to implement that inverse
function in an actual circuit.

I've been doing some trial-and-error experimenting with a simple circuit,
based on a simple Si-diode with some bypass and series resistors in several
configurations, but that doesn't produce satisfactory results -- the best
curve I get is easily 10% off at the extremes, and that's even without
temperature instability. All this is of course no surprise, as the
exponential function of a forward-biased diode is something different than
a logarithmic function, and a simple PN junction has a temperature
coefficient of approximately 2 mV per degree Celsius.

Does anyone know of designs which provide a better fit for this type of
logarithmic function, and preferably a better temperature stability?

2. ### JoergGuest

I'll second that. Being an analog guy this was hard to write but we've
got to know when to fold'em.

The MSP430F2003 has the ADC part built in. Somehow DVM tells me it's
slow so you could get by with the el-cheapo MSP430F2001 or an even
cheaper PIC. The MSP can work sans crystal. Program it to do dual slope
conversion for the ADC and PWM the output, then RC filter that. The math
goes in between where the code is

Piecewise linear can be ok but with diodes it's temperature-sensitive
and with comparators it's expensive.

3. ### Jon KirwanGuest

You were looking for an inverse function of something with an offset
built into it. Sometimes, it helps to first subtract the offset (the
1V) before feeding it to an inverse function to linearize. That was
my first thought. Second, was to wonder if you might instead focus on
a function block that replicates the exponential behavior of the
sensor but is _driven_ by a linear parameter (such as time or
frequency, for example) and then arrange things to adjust the linear
control so that the two outputs match and then read off the control
parameter value, instead. Third was to consider recommending a micro,
which is a rather common approach to conditioning sensors these days.

Jon

I assumed that this step could be considered trivial -- in my measuring
setup, I have a +1.00V reference voltage for this exact purpose.
Well, of course that would provide a limited (in time and range) solution,
but I'm rather more interested in a direct transfer function without
translating the nonlinearity into another domain first.
I know. But I'm one of those old school die-hards who prefers hooking up a
dozen or so components to a meter in an hour or so instead of spending a
multiple of that time programming a controller to do the same.

Good questions. I varied the temperature of the air input to the sensor
between 15 and 60 degrees Celsius, and found no significant differences.
I didn't vary the air pressure, but the setup is intended for use in normal
atmospheric pressure at sea level, so this latter parameter is not
important.
Also, the gas measured is air with only very low traces (< 100ppm) of
contaminants, so I don't have to take differences in gas parameters into
account.

6. ### Spehro PefhanyGuest

Definitely use a micro-- you can implement your equation (in a very
straightforward manner if you use C) and/or do a polynomial fit to
squeeze out the last bit of error.

For example:

Flow (ccm) = 0.6629*v^5 - 9.5389*v^4 + 53.6729*v^3 - 139.4424*v^2 +
192.9420 *v -98.3132

.... which can be evaluated with only five multiplies and no
transcendental operations.

You *could* go looking up how to design analog log/antilog amplifiers
with matched transistors and thermistors for temperature compensation,
but AFICR this is not 1980 and a suitable micro is going to be
cheaper, simpler and much more stable.

7. ### Jon KirwanGuest

It is trivial. But I didn't see you mention it and it's hard to know
what to assume, from my end. I just documented by early thoughts upon
I'm worried about what appears to be an open-loop approach you are
taking.
Well, I'm not going to argue with you about that. You know your
situation and desires better than I do. However, ... you may find
yourself buffeted by practical forces to head this way, all the same.

Jon

8. ### JoergGuest

With dual-slope conversion at the input and a PWM at the output that
should be quite easy. The 3-1/2 digite DVM probably doesn't read more
than a few times per second anyhow.

9. ### Frank BussGuest

Combine this one:

http://www.frank-buss.de/PICPWM/

with some of the C code from this one:

http://www.frank-buss.de/vco/

and you're done, in less than an hour

The function is already known, or you can use a simple piecewise linear
approximation with your measured values. Another idea would be to use a
slightly more powerful PIC and a realtime spline interpolation:

http://www.frank-buss.de/spline.html

I guess it would fit in an PIC16F628 and even with small PICs this would be
much faster than you can see the changes on your voltmeter. But I would use
a 7 segment display anyway, driven by the microcontroller, or with a
MAX7221, which is really nice for this, if price doesn't matter.

10. ### Rich GriseGuest

.-----. .-----. .-----.
| | | | | |
| | | | | |
'-----' '-----' '-----'

;-)
Rich

11. ### Frank BussGuest

But the requirements were lower limit and upper limit for the voltage,
which would require more parts.

BTW: The new ATtiny10, mentioned in comp.arch.embedded, is interesting for
such applications, because it has some interesting PWM modes and other
timer goodies, which makes it even more trivial to solve such problems.

12. ### Jon SlaughterGuest

I don't know but if I had to do it with analog I might simply do averaging.
This will produce a spline approximation:

for example:

75 3.27
100 3.75

if your input was 80 then your output is (3.75 - 3.27)/(100-75)*(80 - 75) +
3.27. i.e., fitting a line between the two points.

now, you implement the analog electronics to compute that equation then use
a multiplexer to select the different data points which is simply setup by
using appropriate resistors that feed your analog equation calculator.

For a large number of data points this is out of the equation as also if
linear approximation is bad. (you can use better approximations but the
analog circuitry starts to get very complex real quick)

You gotta decide when the analog method starts to become more complex than a
uC. Alsoa uC will, in general, be much more accurate and be easily changed
even after fab.

13. ### Robert LacosteGuest

Of course the microcontroller-based approach proposed in previous posts is
very probably the easiest solution. On the "nearly analog" way there are
some chips dedicated to signal conditionning and sensor linearization, like
the PGA309 from TI (see http://focus.ti.com/lit/ds/symlink/pga309.pdf).
However it will probably not be easy to use it in your application as the
liearisation is done through a modification of the sensor excitation and is
adapted to wheatstone bridges. But may be you could add an external aop or
two ?

Friendly yours,
Robert
www.alciom.com

Robert

15. ### Spehro PefhanyGuest

First Google hit is this PDF, which has all the equations needed.

http://www.electronics.dit.ie/staff/ypanarin/Lecture Notes/DT021-4/6LogAntiLogAmplifiers.pdf

see around page 9 (simplified schematic). Matched transistors are used
to compensate for offset change with temperature, and to compensate
for gain error with temperature R4 is made to be a temperature
dependent resistor. Probably good enough for your application, but not
as good as the dedicated chips were.

Unfortunately, the IC dedicated analog blocks they talk of, and even
many of the matched transistors, have been becoming more and more
availability-challenged. The market has spoken.
That does seem a tad on the warm side, Bill. At 22-23°C+ some folks
start to complain, IME.

Best regards,
Spehro Pefhany

Ah, yes, of course -- I searched for "non-linear amplifiers", and got lost
in all sorts of interesting but (for my application) useless information.
And I still have a few SSM2210's around somewhere, so a matched transistor
pair is no problem.
I know, and I guess I'll eventually switch to using controllers for these
applications as well -- but somehow, having a digital chip perform
complicated calculations feels like making things needlessly difficult,
when there are one-step analog processes to do the same. It's like asking
someone across the room if they want another cup of coffee by sending then
a text message.

Also, I don't have the need for any digital stuff at this point -- I have a
several different parameters selected by a switch, and some simple analog
circuitry surrounding it, all running on a single 12 volt supply.

I'm also pondering the idea of charging a capacitor via a resistor, and have
a linear rising voltage (integrator) starting at the same time; when the
capacitor voltage has reached the sensor output voltage, a simple
sample-and-hold of the linear voltage should provide a relatively accurate
value for the flow rate. The only critical component here would be the
capacitor, which must have a low temperature drift.

Anyway, thanks to everyone for their contributions. It's quite interesting
to see how ever more electronic design questions are answered by the single

Thanks again, best regards,

17. ### Fred BartoliGuest

OK, then instead of using your log approach, one could notice the
extreme similarity between your sensor response Vout=c1*(1-e^(-F/c2))+1
and an RC step response Vout=Vo*(1-e^(-t/tau)) which only differs from
your sensor response by a 1 constant. You can easily put this to work,
without having to bother with the inherent log amp thermal problems, and
use feedback to recover the flow value...

R C RC = C2
___ ||
-1 >----. C1 >--|___|--+--||---.
| | || |
.---. | ===
Vout >--| + |---. .--------+ GND
'---' | | |
.-----. |
\+ -/ +-||
comparator \ / ->||
V +-||---.
| | |
| === |
.--o--. GND |
GND -|D S Q|-------------'
| |
Clk >---|> -| ___
| R Q|---|___|--+-----> Flow
'--o--' |
---
---
|
===
GND

Note that this will be a sampled system, so you have to adapt the clock
frequency to your wanted frequency bandwidth.

Yes, I just figured out something along the same lines, and I was just about
to design a schematic -- but yours is quite a bit simpler and more elegant
than what I had in mind
And as your schematic bears a great resemblance to the innards of the humble
555, it wouldn't surprise me if I could use that device for this particular
purpose.

Thanks for this very neat suggestion!

19. ### Fred BartoliGuest

Yes, maybe you could use the 555...
make sure the comparators have the low rail in their input range, which
is not the normal working mode and make sure to use the CMOS version so
that you don't have input bias currents and output switch VCEsat problems.

Hmmm, even simpler...
You could use the 555 with an external mos switch with it's source
shifted, say 1V (guess why) and omit the constant subtraction from your
Vout input.

R C RC = C2
___ ||
C1 >--|___|--+--||---.
| || |
| ===
Vout >----------. .--------+ GND
| | |
.-----. |
\+ -/ +-||
comparator \ / ->||
V +-||---.
| | |
| +1V |
.--o--. |
GND -|D S Q|-------------'
| |
Clk >---|> -| ___
| R Q|---|___|--+-----> Flow
'--o--' |

20. ### Don LancasterGuest

Use a cubic spline.

http://www.tinaja.com/cubic01.asp

--
Many thanks,

Don Lancaster voice phone: (928)428-4073
Synergetics 3860 West First Street Box 809 Thatcher, AZ 85552