DS18B20 smoothing and PID tuning


I’m using that now before my “IBM” PID. It seems to work well with a sample rate of 10.



I’m still working on process parameters for Colin’s PID. When I get that sorted out I’ll try his smoother but for now it’s more a matter of curiosity. I can find median code in java script on the internet but generic java script doesn’t always translate to node-red for me.



Thanks gmag11. Your nudge got me to searching and I found node-red-contrib-statistics. It will do a median over a specified range and only requires a msg.topic with the required math function (‘mytemp/median’). I have it installed and monitoring it’s work and so far it looks like a winner.



I advise not to do any smoothing till you have tuned the loop and proved it is necessary. Smoothing adds delay into the loop and therefore makes it more difficult to control, like putting an elastic coupling on the steering wheel of a car. From the signals you showed earlier with less than one bit of noise there is no need for smoothing unless you need to use significant derivative, in which case you want smoothing only on the derivative, not on the proportional and integral terms.


Also if any smoothing is needed then a simple RC time constant is the best bet. Anything more complex such as a running average or median adds high order terms or non-linearities into the system which is the last thing you need for good control.


Ya, I haven’t done anything yet. I did smooth my Ambient and may smooth my brewbox temp, neither of which is used by the PID.
As long as you are listening, I’m having a hard time determining my parameters. I set my PB at 6 (+/- 6 or +/-3 ?) determined by your method and I’m dropping my freezer to 25F so the bucket temperature way over shoots the setpoint (by 3F) then swings back to far, because the freezer was so cold. ugh, I wonder if PID is the right control method for such a slow changing process?


PID will be the best way for a simple process like this. The fact that it is slow is irrelevant, maths doesn’t care about time scales. Post the chart please so I can have a look and tell me the P and I you have.
PB of 6 means the band is 6 degrees wide but that is not the same as ±3. The integral effect moves the band slowly up and down around the set point to get to the final result. Once it has stabilised it might be +5 -1 for example.


Hi Colin!
I redid my step 1 and adjusted my setpoint closer to 1F and got these parameters:
Pb = 4.575
Ti = 5746
d = 0
I let it run overnight and got a much better looking graph. I’m not nearly so disappointed this morning. I also got my data saved to a csv file if that will help. It will give the whole story instead of cutting off the brewbox temp below 55F, if that will help…

My intention was to open the data file in Excel in order to manipulate it but I wonder if Grapha would do a better job…

the forum wouldn’t let me post a pic of my settings. Said it was too similar to previous posts.



No need for an image of the settings, the values are what is needed. Am I right in thinking that cool at 65 is the cooler fully on, and at 70 it is fully off? If so then there seems to be something strange going on with the cooler. If you look at the section 03:30 to 07:00 it is asking for gradually more and more cool (as the temperature is above the setpoint). The box temperature starts coming down but then starts going up again even thought more and more cool is being applied, then suddenly at around 07:00 the cool seems to kick in and the box temperature falls dramatically. Can you explain that?
Are you using my timeprop node (node-red-contrib-timeprop)? If so what settings have you got for it?


The PID and the freezer were untouched from the start to about 8am this morning when I only viewed the chart. Up to this time I haven’t tweeked the PID or opened the freezer.
I am using your Timeprop and settings are: 20, 0, 1 invert not checked.


I think a 20 second cycle time on a compressor unit is probably much too short, the result being that until it gets close to fully on the compressor never gets going. As it is, for example, a power request of 0.25 (equivalent to 25%) will be on for 5 seconds and off for 15. I suggest trying a cycle time of 10 mins (so 600 seconds) and put the actuator time at 15 seconds, which will mean it will not come on (or go off) for less that 15 seconds at a time.

[Edit] On the electrical heating 20, 0 will be fine.


Ok, that’s done. I chose 20s because that is how long it takes to begin to change the air temperature inside the freezer. I agree 20s is way to short and wanted to extend that but didn’t know how it would affect the PID parameters.
Should I clear the graph, reset the PID and try again?



Just leave it running, no need to reset the PID. You can clear the graph if you want to.
Five second sampling sounds good, the timeprop algorithm will adjust to use the latest values as they are received. Oh, you have deleted that post.


Could you give some more information on this please.
I’m currently using your PID, but found that it more stable if I added a smooth node, but of course I do get a delay.
Have you an example of a RC time constant (yes, I have googled it :thinking:)




If the process is more stable when you add a smooth node then the loop is not tuned correctly. Could you start a new thread for this rather than get it mixed up with this one? Post a chart showing the temperature (if it is temperature), setpoint and PID output value showing the problem and tell me what PID values you have and details of the sensor and output device.

Help with PID tuning!

Ya, figured that one out myself. :wink:


Thanks, will do. I’ll take out the smooth node, and post when I’ve got sufficient data.


I’ve also discovered that a 12 hour graph really boggs down my network connection to the Pi. My network is all gigabit but the Pi only runs a 100mb. I’ve reduced it to 4 hours and will make a graph out of saved data. I can open the csv file in Excel and make a graph for next time.


That will be rather a lot of data point on the graph. Are you running the browser on the pi? Much better to run it on a PC pointed to the pi ip address. The browser probably consumes much more resources than node red does. A four hour graph is probably enough anyway.

You can get much better graphing capabilities using influxdb and grafana, but one thing at a time probably.


I was wondering how to get grafana to graph a csv file. I had a quick look and it appears to me that it only takes data from a database. Oh, I just noticed influxdb is a database. I have some experience with sqlite on android, maybe I’ll see if it will work with sqlite.
I was going to adjust my sample rate to something like 30 seconds or a minute. With an half hour from crossing to peak a minute doesen’t seem critical.
I am viewing node-red in firefox on my desktop pc. With 12 hours of data it was transferring well over a megabyte of data every 5 seconds. I had about a 3 second window to make changes. Pretty frustrating.