Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

lookup tablesss VS equations

Status
Not open for further replies.

DanD

New Member
Hey guys,

OK, i'm stuck. i using my PIC18F4480's ADCs to monitor temp and pressure sensors. the sensors vary their resistance according to the temp or pressure. I have a chart that shows what the temp or pressure is at a given resistance.

I've been plotting and playing w/ circuits in excel all day and i cant find a smooth way to build a circuit whos output voltage when converted to digital maps to the actualy units of temp or pressure. for example it would be nice if i could get teh circuit to output 5V at 102.4 degrees for example. but maybe thats too idealistic, cuz i can't figure it out.

so what are the pros and cons of using a lookup table for the values vs an equation? i'm thinking the equation would be more memory efficient, but it still wouldnt be as accurate as the lookup table. i've plotted various equations agains the actual data points and can get very close, but still not PERFECT w/o getting too fancey w/ the math.

what do you guys suggest? Thanks
 
Size of the lookup table versus the size of the code space. Three tables of 256 bytes in a 2K space would not seem unreasonable to me. If you can find the math routines that you need you should compare the space they require with the space of a table.

With fixed point arithmetic, using bytes or words you will never get more than 2 or 4 digits of precision. There will always be either truncation or roundoff errors as long as the number of significant digits is limited. With 32 bit arithmetic the situation improves to nine digits. I'd be willing to bet that 32 bit multiply and divide would take on the order of 0.5 to 1.0K of code space. It might take six months to write and debug from scratch if you're lucky. It might take less to copy code you find on the internet. What happens if it doesn't work? I think you get the idea.

There are sophisticated methods for computing the coefficients of polynomial equations that minimize and distribute the errors of a set of data points. I can give you a pointer to a package that will do this if you are interested. It is not for the faint of heart however. Here is a link

https://www.amazon.com/gp/product/1...102-0053808-3548142?s=books&v=glance&n=283155
 
Without information on the sensors it is very difficult to suggest either alternative. You would have to scale your sensors to give 0-1023 out of the ADC and would therefore need a 1k lookup table. 16*16 multiply takes 28 memory locations and the same number of clock cycles to execute, but, if it was that simple then an op-amp would have provided a nice solution.

A link to the sensors would help.

Mike.
 
Precision needed?

It all depends on which precision you want..
DanD said:
Hey guys,

...ADCs to monitor temp and pressure sensors.
I think that for temperature you have enough with 1 decimal. For pressure maybe 2 or 3? So calculations with bytes will be more than enough. Multiply all your data by lets say 256. So at the end you can forget the last byte.

DanD said:
I have a chart that shows what the temp or pressure is at a given resistance.
If you could have datapoints instead of chart it will be much better. Enter the datapoints in Excel or MathLab and play around with polynomials to find the best one.

DanD said:
I've been plotting and playing w/ circuits in excel all day and i cant find a smooth way to build a circuit whos output voltage when converted to digital maps to the actualy units of temp or pressure. for example it would be nice if i could get teh circuit to output 5V at 102.4 degrees for example. but maybe thats too idealistic, cuz i can't figure it out.
"would be nice" is not a requirement. What do you require? Max 102.4F (°C) Try to make a circuit that gives 4.8 to 5V for your absolute maximum temperatue. It's nice to know that you still have some overshoot...

DanD said:
so what are the pros and cons of using a lookup table for the values vs an equation? i'm thinking the equation would be more memory efficient, but it still wouldnt be as accurate as the lookup table. i've plotted various equations agains the actual data points and can get very close, but still not PERFECT w/o getting too fancey w/ the math.
Lookup table is only accurate if you have one entry for every bit combination of your ADC. Let say you have a 12bit ADC, then you need a lookup table with 4096 points.
Maths can handle all these 4096 combinations with the same equation...

DanD said:
what do you guys suggest?
Maths
 
verrry interesting. Yes a look up table would require a LOT of interpolation.. which would probably be worse than an equation. hrmm

Here are my tables:

Code:
Temperature:

Sender R	Temp
1123.00	100
708.00	120
460.00	140
374.00	150
253.00	170
175.00	190
123.00	210
119.00	212
105.00	220
89.00	230
65.00	250
42.00	280
32.00	300
25.00	320
20.00	340


Pressure

250	0
158	25
111	50
75	75
43	100

Pretty crappy from an accuracy point of view i suppose. I've been focusing on the temperature and forgot how few data points Pressure has. I need an 'accurate' pressure scale from 0-100PSI and temperature from about 100-275

Nonetheless, i have found a way to at least get a linear voltage output. If i build a simple voltage divider using Vo=R/(S+R)*Vin (S=sensor and Vin is 5V), V0 will range from 0.5 to 4V with decently linear slope. I can then use the equation 'Vout*50+49 to get a decently accurate Temperatue from 100 to 250F which is the sweet spot.

This only needs to be accurate within 2-3 degrees.

I can used the same voltage divider circuit for the Pressure sensor and just use an EQ of Vo*50-71 for similar accuracy.

I guess my real question is now, what should be the goal of the input circuitry. what should and shouldnt it handle? and then what should and shouldnt be expected to be handled in the software? i'm getting confused drawing the line between the two.
 
You have to balance it between speed and accuracy, a giant look-up table or a complex equation are both slow. Look-up tables tend to take up more memory than equations, but can be faster (not necessarily if the look-up table gets too big) .A look-up table works best when you have a small number of very uncharacterized or non-linear values that can't be well approximated by a simple equation. Equations work best when you need very fine resolution for many many values.

Equations = less speed, less memory
Look-Up = more speed, more memory (assuming the uC can sort through values faster than it can perform the equation).

There are many exceptions of course, like when a look-up table is a enormously large, or when an equation is very non-linear and complex that it might take too long to calculate.

If space is a concern and your application is measuring a very slow changing value, you might want to go with a potentially very complex equation.

If speed is a concern and you are measuring very fast changing values...that's when you have to decide which is best on a case-by=case basis.
 
Last edited:
why are you using such stupid sensors? for example temperature can be measured much more
linearly using standard diode (ok need amp but so what).
also there are ready sensors with I2C, SPI or whatever bus you like or your chip supports.
I don't know what language you can use and what math functions are available but here are
reasonably accurate fits.

For temperature you can use Hoerl Model: y=a*(b^x)*(x^c) ; (+/-1deg)
where
a = 730.56825
b = 0.9998117
c = -0.25442213

or simpler Harris Model: y=1/(a+bx^c) ; (+/-3deg)
where:
a = 0.0012661636
b = 0.00051422347
c = 0.39865197

For pressure try 3rd degree Polynomial Fit: y=a+bx+cx^2+dx^3 ; (+/-0.25 of psi or whatever unit you use)
where:
a = 138.7808
b = -0.95952107
c = 0.0013292397
d = 0.0000011539145
 
Last edited:
That's a good point. Why are your sensors so bad at those readings? As far as temperatres goes you can get excellent linearity using silicon IC temperatures sensors, RTDs or thermistors. One of the only times a temperature sensor might be non-linear is if you are using a thermocouple to measure something at high temperatures...in which case the output is not a resistance but a voltage which requires special circuitry (or a special IC to linearize the output). However, you say that the output is a resistance, which probably makes it a thermistor or RTD, both of which are very linear (at least I think a thermistor is linear, but an RTD is the ultimate in lineartiy as far as temperature sensors go).

I read your post again, you said you wanted perfect without getting into fancy math? Too bad...not much you can do there since the very act of curve fitting (approximating an aritrary graph with an equation) requires numerical analysis mehods which I guess you could call fancy math.

If you want perfect without math a massive look-up table is the only way to go...

Where exactly are you getting your values from? You say that you can get very close but not perfect...you do realize that things in life vary right? And that if you find an equation that can land on those table values bang on might be moot because of equipment variations and tolerances in reality. Also, can your ADC resolution read fine enough increments to recognize the discrepenecy between table and equaton values? It's kind of pointless to have a sensor of infinitite resolution if your ADC has only a finite resolution.
 
Last edited:
indeed. hrm math isnt a problem, i just thought getting complicated would be too slow. i guess i dont have enough uC experience to know what they can and cannot handle.

These sensors are from Accutech, a well known automotive guage/sender company. The tables i posted are from their support people. This is for a car data display/aquisition unit so i figured id go w/ cheap readily available sensors that the car folks are familiar with. they are crappy, but if theyve been good enough for car people for years maybe its ok. and theyre only $12 a pop.

I'm open to other sensor ideas, but itd have to make sense in a car. ie, vibrations, lots of heat cycles, and it would probably have to be mounted into a threaded sender location in order to get to the water or oil, etc.
 
Hi,
at least I think a thermistor is linear
No, they are not linear, but if you are using only a small range of scale there are ways to make the line fairly straight using an op amp circuit. (See "Analog Interfacing To Embedded Microprocessors," chapter 3, by Stuart Ball)
Panic mode gave you some equations, but you need the constants from the manufacturer to use them.

The reason for using resistive sensors, NTC thermistors, in particular, is that they are fast. They are not as fast as a thermocouple but can respond in less than five seconds, where it may take a silicon sensor up to thirty seconds or more. Thermistors also have much greater range than silicon, and I think Dan wants too measure temperature 'from 100 to 250F' which are beyond the range of silicon ICs. (top ~150F) They are also more accurate than silicon ICs.

RTDs are fast and accurate, but not as durable, and more expensive, and they also require linearizing, although with a 2nd degree polynomial, rather than 3rd degree like the NTC.

If you want to spend the time to develop the application, NTCs can give you within 0.1 deg C., but less fine results should be easy to get, without a big investment in time.

theyve been good enough for car people for years maybe its ok.
Yes, of course! I would stick with what you have; if you persist you will get there, and will have another tool under your belt by the end of it all.

It sounds like you are measuring the temperature of water? Also, if you wished to go with a nice IC that gives you a neat digital value, you would need to wrap it as they are not immune to water.

Just my 2 cents
Robert
 
Last edited:
i've gotten to with in a couple degrees or PSIs of accuracy w/ a simple Vout*X+Y equation. it runs plenty fast so i think this will be fine. thanks for the input
 
Yeah a table lookup is quite effective.
Polynomials are not really practical. You're going to have to bring in a complex math routine to do any exponentials which are not whole numbers. And dealing with "0.0000011539145" as a parameter is pretty tricky unless you bring in floating point math. Once you bring in these things, the "low ROM requirement" feature of the solution may evaporate.
 
sorry, but...

to do it PERFECT you cannot rely on some generic table, you need to calibrate yourself so
produce, wait until settled, measure and record every point in whole range
you plan on reading in the future. as you can see this is going to be real pain.
it can be automated if you have controlled source (temperature or whatever variable you want to measure),
good reference instrument and data acquisition. also this has to be repeated several times
to get average, see repeatability, histeresis etc. then you can export collected data and
use it in your program.

using math is much easier since you only need few points. there is no such thing as perfect
unless you have to deal with just one bit of information (true or false).
for anything else you need to define accuracy, say what is acceptable.

so, if you ask me, you should ask your self:
- what are you measuring
- what range
- what accuracy
- how fast response
- what conditions will apply (ambient etc.0
- how robust (NEMA or IP rating, vibrations, temperature, ...)
- what budget (money and time)
- how important/dificult is to reproduce it (repeate calibration etc.)
- is it worth it
-

did you see what accuracy is available in commercial products?

just because something is used in a car, it doesn't mean it is robust or accurate.
trust me, i'm building test equipment for automotive industry (Honda, Toyota, Chrysler, Ford, GM...)
to evaluate every piece they produce or use, we are talking milions and milions of parts a year,
everything from engines, car seats, mirrors, brakes, pedals, headliners, ECUs, airbags, you name it.
not all but MANY devices are neither robust nor accurate, they are just meant to be cheap!
so you found temperature sensor that is used in cars... how accurate is temperature gauge in your car?
have you seen how fine divisions on the temperature scale are? did you see ANY numbers beside them?
do you think this is really linear or just cosmetic meaning "temperature is fairly high", "... or low",
"maybe is high enough to be concern..."?

sorry again... i hope it helps you rethink your project.
 
panic mode said:
i hope it helps you rethink your project.
or cancel it at once :)

I agree that
DanD said:
i've gotten to with in a couple degrees
isn't what you can expect from a temperature sensor. But you also have to make a difference between industry and hobby...

Please read the attached file to make a good design: temperature sensor, signal conditioning, µP.
 

Attachments

  • Sensor to ADC-Analog Interface design--slyt173.pdf
    322.8 KB · Views: 429
Excellent app note, however, it deals with scale and off set, rather than curve fitting, which is the problem at hand. The original debate, if I'm not mistaken, is concerned with whether to use a look up table, or floating point math routines to straighten out the curve of a resistive sensor.
That app note, does, however, detail a method of doing it with a diode, which seems linear, and probably easier.
I don't agree, though, that resistive sensors are stupid. They have been around for a long, long time, and are very accurate. It just takes more work to obtain that accuracy.
I agree, though, that he should be able to get better than 'within a couple of psi,' but this should be possible with a lookup.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top