# 1st attempt at PID controller?

#### greencardigan

##### Senior Member
I think I've jumped in the deep end a bit here. I have an idea for a project that might warrant a PID controller.

Currently I roast coffee beans in a hot air type popcorn maker but the roast times are to quick due to lack of temperature control.

I have seen PID controllers on ebay but cant justify the cost of using these for a \$20 popcorn maker.

So, is it possible/worthwhile to implement a PICAXE PID system to control the heating element in the popcorn maker?

Expected temps are in the range of 10 deg C to 250 deg C.

Not sure what kind of sensor I'd need? K-type thermocouple?

A few limitations I only have experience with 08Ms atm. Needs to be cheap.

I've looked at a few bit of PID code here (thanks BeanieBots) and have whacked the following code together. The tuning parameters used are arbitrary atm as I dont have a physical system to test it on yet.

Just wondering if I am on the right track with respect to the code and the project in general.

symbol Kp = 500 'proportional factor
symbol Ki = 11 'integral factor
symbol Kd = 10 'derivative factor
symbol dt = 3
symbol pt = b0 'previous temp
symbol t = b1 'measured temp
symbol sp = b2 'set point
symbol e = b3 'error
symbol pe = b4 'previous error
symbol PID = w3

sp = 150

PID = 32768

do
pt = t

pe = e

if t <= sp then 'for negative errors

e = sp - t

w4 = Ki * e * dt max 500 'INTEGRAL..........................
PID = PID + w4 max 52768

w4 = Kp * e max 12000 'PROPORTIONAL......................
PID = PID + w4 max 52768

else 'for positive errors

e = t - sp

w4 = Ki * e * dt max 500 'INTEGRAL.........................
PID = PID - w4 min 32400

w4 = Kp * e max 12000 'PROPORTIONAL....................
PID = PID - w4 min 12768

endif

if t > pt then 'temp increasing
w4 = t - pt
w4 = w4 * Kd max 10000 'DERIVATIVE ......................
PID = PID - w4
elseif t < pt then 'temp decreasing
w4 = pt - t
w4 = w4 * Kd max 10000 'DERIVATIVE ......................
PID = PID + w4 max 52768
else
w4 = 0 'no derivative part
endif

if PID <= 32678 then
output = 0
else
output = PID - 32678
output = output / 200
endif

pause 500

loop

#### BeanieBots

##### Moderator
I've only glanced through but it looks to be on track.
You probably should consider using a word for the temperature so that it can have bigger number. This is not to make it more accurate but to have more resolution for the number crunching.
Also, I could not see anything that controls the rate at which your loop executes. Might not be a problem once tuned to your equipment but each code change will affect the bandwidth which could make life difficult during the tuning stage.
Good luck with it. Will be hard to know until you have REAL hardware to control.

#### Dippy

##### Moderator
A nice project.

everyone jumps straight in with K type thermocouples

Before you start down the thermocouple route, are you Ok with the electronics for interfacing and physical connection?
Yes? Good. Here's a little info:
http://instserv.com/rmocoupl.htm
and
http://instrumentation-central.com/pages/thermocouple_reference_table.htm

Personally, I'd look at Platinum resistance as a serious option.
http://sensing.honeywell.com/index.cfm/ci_id/103016/la_id/1/document/1/re_id/0

btw: "Needs to be cheap". What is your definition of 'cheap'?
Under £20? Then forget all the above.

How accurate does it really need to be?

#### MFB

##### Senior Member
PID tutorials

The ‘Stamps in Class’ range ( www.parallax.com/Default.aspx?tabid=362 ) covers topics like industrial control and smart sensors, which and provide some excellent tutorials on PID control. The example Basic code is of course for the Basic Stamp but can be adapted for the PICAXE without much effort.

#### greencardigan

##### Senior Member
Also, I could not see anything that controls the rate at which your loop executes. Might not be a problem once tuned to your equipment but each code change will affect the bandwidth which could make life difficult during the tuning stage.
This is something I dont really understand yet. How often should it loop in this sort of application? I would have thought as often as possible.

Dippy said:
Before you start down the thermocouple route, are you Ok with the electronics for interfacing and physical connection?
Not yet
I've had a quick look at the data sheet for the MAX6675 but I'm not sure if it can interface easily with an 08M???

And I'll check out platinum resistance.

Cheap?? No more than \$40 AUD. What ever that works out at in £.

#### Dippy

##### Moderator
£18.52 eh? Oh dear, I think this will cost £18.53.
Well, quality (and platinum) comes at a cost eh? I hadn't looked at that as Ebay is the last place I'd buy it.

Putting that aside it depends how much effort you want to make to save money.
You could make your own probe using:-
http://uk.farnell.com/1266941/industrial-controls-automation/product.us0?sku=IST-P1K0.232.6W.A.010

or, better still:-
http://uk.farnell.com/541102/industrial-controls-automation/product.us0?sku=LABFACILITY-P100-2528

You may be able to find something cheaper as I guess the 'platinum' precision isn't needed?
Perhaps the Beijing Thermometer Company

#### BeanieBots

##### Moderator
Greencardigan,
The loop time is one of the most essential things there is! It will affect nearly all of your required parameters and the way it controls the temperature.
It MUST be a fixed rate. That is, if your time varies with changing temperature because a calculation takes longer for example, then the control will be unpredictable.

PID = Proportional,Integral,Derivative.
P & I are in the time domain. Delta t is your loop time.

Last edited:

#### BrendanP

##### Senior Member
You're a hard man Dippy.

"Perhaps the Beijing Thermometer Company"....

You're a hard man Dippy.

I once saw some Chinese made batteries called "Flying Bomb Brand". (True story.)

Perhaps they're into high end temp sensor parts as well.......

Last edited:

#### MFB

##### Senior Member
Free PID tutorials

Further to BeanieBots advice, I should have mentioned that the Parallax Inc 'Stamps in Class' range of tutorials are free to download.

#### greencardigan

##### Senior Member
The loop time is one of the most essential things there is! It will affect nearly all of your required parameters and the way it controls the temperature.
It MUST be a fixed rate. That is, if your time varies with changing temperature because a calculation takes longer for example, then the control will be unpredictable.
Yep, I understand the need for a fixed rate loop time. But how often should the loop run? Once every second? Once a minute?

#### BeanieBots

##### Moderator
Depends on the characteristics of your heater / thermal mass and how you have scaled your I & D gain constants.
As a very crude guideline, if T is the time taken for your required load to increase in temperature by the resolution you want to control at with the heater at 100%, then your loop time should be about 100 time smaller.
Any quicker and you will have problems with the integral. Any slower and you will overshoot before you can cut power and undershoot before it is re-applied causing massive oscillations.
If your thermal mass is significantly phase lagged from the heater then you might want to decrease the loop so that the proportional part has tighter control. Obviously that would require some 'fudgeing' of the I & D terms.
If you have looked at my earlier example, that is what I do for the derivative term to avoid integral runaway at higher loop times.

You will need to run your system open loop initially to know, plot and understand its characteristics in order to calculate what loop times and PID constants to use.

#### greencardigan

##### Senior Member
Thanks, that all makes sense.

You will need to run your system open loop
initially to know, plot and understand its characteristics in order to calculate what loop times and PID constants to use.
This is where I can't progress yet as I haven't got a system to run. I need to work out how to rejig the popper's electronics first.

#### BeanieBots

##### Moderator
Hmm... you need to know your system first.
It's a bit like "design me an engine" before you know what it needs to drive

Then again, my whole carear has been like that

#### greencardigan

##### Senior Member
I don't know if this is an appropriate forum to discuss this aspect of the project.

A far as I know, the popper has heating element(s) in series with a fan. That is, the elements drop the voltage to a usable level for the fan. The fan then runs off rectified AC.

I will need to separate the element and fan so I can control the elements and fan seperatly. The fan can be powered by a seperate power supply and the heaters with the existing power supply via a controller/SSR.

#### Dippy

##### Moderator
so what's the question?

#### greencardigan

##### Senior Member
Ahh yes the question. I must have been distracted.

When I separate the heating elements form the fan, I was wondering if the elements would run hotter than before (assumuing controller on full power or no controller) as it would be getting the full 240V AC. Would that be OK?

#### greencardigan

##### Senior Member
And can anyone advise if these platinum RTDs would suit my application.

There may also be another possibility. I'm thinking of getting a multimeter with thermocouple reading capabilities and RS232 output.
Can a picaxe read outputs from an rs232 multimeter?

Last edited:

#### BeanieBots

##### Moderator
With regard to your heater fan issue. Impossible to comment without numbers.
The heater will certainly run at higher power. The question is, will it just be a bit hotter or instantly evaporate in a puff of smoke.
The answer depends on the electrical characteristics of both heater and fan. Without those, it's only guess work. My guess is that it would NOT be OK.
You will need to take measurements. You could take them at the same time you characterise the thermal characteristics needed for calculating your PID terms.

#### Dippy

##### Moderator
Like BB says, without measurements/figures (your job) who can say.

That PRT sensor OK? Well, there's no data, and you're obviously picking it as it is the cheapest. On the face of it it looks OK. I'm guessing but it looks like a 100ohm device. For your electronics it may be easier to use a 1000ohm device, but it's your wallet.
And I guess you'll be back for a schematic for reading temp on a 100R PRT?

One question I really must ask re PID: Is this not a little OTT to be looking at some convoluted PID when all you need is a posh thermostat? If you can work out the heating-up-speed-after applying power (to pre-empt overheating) you could make a semi-PID. If it's for educational purposes then fire away with the Full Monty.

(Oh sorry, BB aleady said "...characterise the thermal characteristics needed " which is the pro way of saying what I did and more, but he said it first!!)

And of course all the above depends which thermosensor you buy and where you put it (using properly rated cable and mechanical attachment i.e. screw not glue.)

Thermocouple+M/meter with RS232 output?? Whats the spec? How fast? Real RS232 levels or just logic? You may be able to connect it to PICAXE as the only one I've seen is 9600baud minimum, so that limits PICAXE choice.
But aren't you going over your \$40 threshold here? And you'd have to leave the M/meter on all the time. And would you trust a cheap one? And you'd trip over the wires.

#### greencardigan

##### Senior Member
One question I really must ask re PID: Is this not a little OTT to be looking at some convoluted PID when all you need is a posh thermostat? If you can work out the heating-up-speed-after applying power (to pre-empt overheating) you could make a semi-PID. If it's for educational purposes then fire away with the Full Monty.
Yes, it is partly for education. But I'm beginning to see it is OTT.

Thanks for all your help anyway. I may get to it one day.

#### greencardigan

##### Senior Member
Just an update on this project.

I've got my PID controller in a working state and have been using it to roast coffee for some time now.

I haven't fiddled much with the PID parameters yet but it seems to work ok as is.

The full code is attached.

#### Attachments

• 7 KB Views: 77

#### greencardigan

##### Senior Member
And a graph showing what's going on.

#### Attachments

• 114.7 KB Views: 61

#### BeanieBots

##### Moderator
Some very familiar lines/routines in that code!

#### kermet

##### New Member
Very cool to see, well done.

As for the sample rate you could make it a variable so the user can change to suite.
With the line of process controls i work with this is adjustable as each peace of plant is different such as Air temperature Supply/Room, pressure Gas/Liquid and many more.

Robert

#### rodmcm

##### Member
Rather than a PID try using a simple circuit that switches an output on or off as a pulse where the pulse length is proportional to the error between the measured temperature and the set point. Sample the error at period Tp(sec/min) and make the maximum pulse for the maximum error as Tp. ( sec/min). This type of contoller approximates a PID loop but is easier to implement for on/off controls.

I have used this in PLCs to run governors for large( and small) turbines to hold kW setpoint.
If you have a motor control then of course the error can be detected as pos or neg open or close the control.

#### greencardigan

##### Senior Member
Some very familiar lines/routines in that code!
There are indeed. Thanks