View previous topic :: View next topic |
Author |
Message |
Ömer Faruk
Joined: 15 Nov 2018 Posts: 42 Location: Çanakkale
|
I need an algorithm to find out whether temp is increasing |
Posted: Wed May 22, 2019 11:24 pm |
|
|
Hello,
I have a homemade incubator. I use incandescent lamps as heater. I implemented on-off control of lamps. Sometimes these lamps can be broken while incubator is working. I want to know when lamps are on whether temperature is increasing. I use pic18f46k22, 10k NTC.
My method is like this:
I set up timer1 that ticks per 1s.
On the first second i save the first temperature.
Until second 64 i add all temperature.
When it is second 64 i find the average of the temperatures.
If first temperature is lower than average that means the heater is not working or incubator door is open or NTC is fallen somewhere on the floor.
Then alarm.
Do you have any suggestions or better algorthims, please share with me.
Last edited by Ömer Faruk on Wed May 22, 2019 11:28 pm; edited 1 time in total |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu May 23, 2019 7:16 am |
|
|
You should be comparing the average of the first 64 readings against the average of the next 64!
Otherwise you have a noise problem.
Are you certain this is the correct way round? Quote: | If first temperature is lower than average that means the heater is not working or incubator door is open or NTC is fallen somewhere on the floor. |
You also need to include some hysteresis in your comparison.
Otherwise the alarm will go every time you get an average temperature which is lower than the one before. How much hysteresis depends on the rate of fall when the lamps fail.
You can measure the rate of temperature fall by simply switching the lamps off once you have reached working temperature.
At this stage you're well on your way to doing full PID control.
I'm also assuming that the required temperature is always above expected ambient.
Mike |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Thu May 23, 2019 8:29 am |
|
|
For lamp failure, you 'should' monitor the current in each lamp( optocoupler) though other options could be a local IR detector to confirm the lamp is on.
Sampling every second is not necessary. My remote energy control systems only scanned once every 15 seconds , even that was 'excessive' as temperature is a very 'slow' process.
I would however, read the sensor 10X, then take an 'Olympic Average' of those readings. This would give a better actual temperature. |
|
|
Ömer Faruk
Joined: 15 Nov 2018 Posts: 42 Location: Çanakkale
|
|
Posted: Thu May 23, 2019 8:47 am |
|
|
Hello temtronic thanks for your answer but i dont want to use another component in the project. I believe there is a way for using just NTC. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 478 Location: Montenegro
|
|
Posted: Thu May 23, 2019 11:11 am |
|
|
Whatever you use for detecting the temperature is OK. Personally I'd never use a resistor, simply because I'm not sure how repeatable the measurements are. Is 1k always at 20 degrees or is it at 18? LM135 or one from that family or 1820, just to be sure about that. You mentioned the possibility of open door. Reed switch and a magnet would relieve you of that worry. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 478 Location: Montenegro
|
|
Posted: Thu May 23, 2019 11:16 am |
|
|
It should be vice versa. Is it always 1k at 20 degrees :-) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu May 23, 2019 12:24 pm |
|
|
Presumably the incubator has some form of temperature regulation?.
If so, the temperature _can_ fall in normal operation. Regulator
decides the chamber is getting too hot, and switches off the heat source.
Result, temperature falls for a while till the regulator decides the chamber
is getting cold, and switches on again. The change will only be small,
but it will happen.
Your test algorithm, either has to only test for temperature falling when
the heater is actually 'on', or instead test for the temperature dropping
below the lowest normally acceptable.
Normally incubators run at a nominal 37.5C. A drop below 37, would be
an acceptable 'limit'.
Now I am very dubious that a NTC based design is really going to be
very accurate. How are you actually measuring the resistance?. It'll
be perhaps 6014R at 37C, and 5773R at 38C. Assuming you are using
the ADC, you have to remember that if there is any current flowing
through the NTC element, it will self heat. The accuracy limits of the ADC,
VREF, and the heating effect, will make maintaining really accurate
measurement difficult. An IC sensor like a TMP117AIYBGT, will give
much more accurate and repeatable measurements. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Thu May 23, 2019 1:04 pm |
|
|
I agree with IC temperature sensors.
A NTC based design CAN be calibrated but imo it's way more trouble than it's worth even just getting a reference temperature. Off the top of my head, some convenient temperature references: ice water, your own armpit, boiling water... all pretty far apart considering the scale you are trying to measure. And then, add to the fact that your thermistor response to temperature is quite non-linear... kinda making my head hurt.
IC temperature sensors come pre-calibrated from the factory and there is plenty of driver code already written. Look for ones that are called "human body temperature sensors", which are specifically calibrated for that temperature range. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu May 23, 2019 2:15 pm |
|
|
Yes. If you think about even a good ADC reference, and then the conversion
between resistance and an ADC value, then the accuracy of the ADC itself,
and then the errors in the conversion maths, getting a repeatable reading to
a fraction of a degree, is going to be really hard.... |
|
|
Ömer Faruk
Joined: 15 Nov 2018 Posts: 42 Location: Çanakkale
|
|
Posted: Fri May 24, 2019 1:42 am |
|
|
Ttelmah wrote: | Yes. If you think about even a good ADC reference, and then the conversion |
5V
NTC
Measuring point
10K
0V
While measuring resistance value there is no need for a positive reference if negative reference is ground and positive voltage reference is 5V. |
|
|
Ömer Faruk
Joined: 15 Nov 2018 Posts: 42 Location: Çanakkale
|
|
Posted: Fri May 24, 2019 2:33 am |
|
|
PrinceNai wrote: | Whatever you use for detecting the temperature is OK. Personally I'd never use a resistor, simply because I'm not sure how repeatable the measurements are. |
İ think it is repeatable. My results are good for an incubator if you use high quality NTC with a correct temperature-resistance equation. There is also no need for positive voltage reference. The biggest con for me that it needs excessive mathematical operations (log10,floats,divide) which takes too much rom and ram. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri May 24, 2019 4:44 am |
|
|
You can get rid of the 'math'. Run some tests and record temperature vs. ADC bits then create a lookup table. Use the ADC result as the pointer to the element of the lookup table. The table elements are the 'temperatures'.
something like..
adcbits[temperature]
ADC bits temperature
98 1021
99 1023
100 1025
101 1028
My 'elements' are unsigned 16 bit integers, so displaying them is easy as xxx.x
The tricky part is getting a good linear span over the range of temperatures you're interested in. Though if you just 'read and record' to create the table , there's really no math involved.
I did this 'in the early' days for a 16C84 with a photocell as a dusk/dawn sensor. Lookup table is small AND fast. Be aware that sensor output IS dependent on VDD, unless you use a stable Vref.
It might be an option for you.
Jay |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 478 Location: Montenegro
|
|
Posted: Fri May 24, 2019 7:57 am |
|
|
As I said, LM335. Costs 1$. Output voltage is directly proportional to Kelvins. 2,85V = 285K. No math, nothing. No lookup tables. You even don't have to change the code, it's the same as "reading" your NTC. 1820, even better. Digital output, you don't have to worry about the accuracy of your power supply, self heating and the likes. The code is here on the forum and it just works. One include and a call to a function. Is there any special reason to use NTC? If there is, throw in a voltage reference, so you at least know that your AD readings are correct. By correct I mean that regardless of the fluctuations of your power supply you can calculate the correct temperature. I always use a simple calculation. If I go and dig a trench for 5€/h, is it worth spending 3 days coding just to solve a problem that is already solved, and solved good, in order to save 70c? Especially if it's only one piece that you are building, where you can't really save anything. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 478 Location: Montenegro
|
|
Posted: Fri May 24, 2019 9:34 am |
|
|
But all of this is a sub-topic of your original question. Let's postulate that your temperature readings are good. Never mind the sensor used. What you need are temperature ranges. Where the temperature is "OK", "TOO_COLD", "TOO_HOT", maybe also "CRITICAL_LOW" and "CRITICAL_HOT" where you just sound the alarm. And then react, but only when you read x consecutive temperature readings (consecutive readings filter out sensor glitches) in the same temperature range. If you are in "TOO_COLD" zone or even below the upper limit of "OK" for some time, try to heat the incubator. Measure the time of heating. If the incubator doesn't heat in a certain amount of time and the temperature is below your critical low setting, sound the alarm. If it is too hot for a certain amount of time, sound the alarm, too. If the door is open for a certain amount of time, sound the alarm. If temperature sensor is disconnected, sound the alarm.
I had the same problem with the cooling of a laser tube. I'm still waiting for all the parts to arrive to fully test it, but the principle works. A switch() statement with five states. It is executed once per second. Read the temperature, evaluate the reading. If x readings in a row are within the same range, do something. Change state, turn on the lamps, turn them off, sound the alarm. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu May 30, 2019 7:22 am |
|
|
I don't have a problem with using an NTC.
The method is ratiometric so variation in Vref is not an issue.
I'm assuming you're using a buffer amp.
I tend to use 100k as it gives 10x less self heating.
It's convenient to use a burden resistor that has the roughly the same value as the NTC at your point of interest.
In your case, I guess you're trying to regulate to ~40C so I'd use a 56k burden resistor.
If you now plot a chart of measurement voltage against temperature you'll get a sort of elongated tilted 'S' shape.
The burden resistor has the effect of linearising the Volts/Temperature characteristic.
The 'S' shape has a point of inflection at ~40C, giving very linear performance in that region.
You can avoid the use of logs by creating a lookup table every (say) 10C.
You convert measurement voltage to temperature by linear interpolation between the values in the LUT.
I found that within 10C around 40C the error was much less than 0.1C.
From 0C to 80C the worst error was ~0.3C.
If that's not good enough for you then do a LUT at 5C intervals.
For me it's only around the point of interest that really matters, I'd settle for the 10C interval!
You can avoid the use of floats by working with measurement voltages in mV and temperatures in mC.
Just so much easier and faster.
You've not told us how you control the relay.
Is it bang-bang control or proportional?
Proportional control will give you less fluctuation in the controlled temperature.
Adding a differential term will improve things further (taken from rate of rise of temperature).
Finally adding an integral term will eliminate the effects caused by ambient temperature change.
Good luck.
Mike |
|
|
|