Rough Temperature Sensor Calibration

First, let's talk about using the temperature sensor. You should be getting some number back from the thermistor circuit when you check the A/D input assigned to it, which of course is not degrees F. So we need to calibrate the sensor circuit. A quick and dirty way to do this is to fill the baking pan with hot water at about 160 deg F, put a thermometer in it, and let it sit in the bread machine with the lid closed until the thermometer reaches about 140. You'll have to use the window or guess and check, but try to only check once or twice. When the thermometer reads about 140, use your program to read the A/D input and temporarily consider this number to correspond to 140 deg. It will actually be more like 120-130 deg, but we'll fix this later. In addition to this, you need to confirm whether the A/D value gets larger or smaller as the temperature increases. These two pieces of information are enough to start doing temperature feedback control.



Feedback Control Options

There are two ways of implementing temperature feedback control that I considered: thermostatic and proportional-integral (PI).



Thermostatic control solves the problem much the way an old thermostat for a home furnace does: it turns on the heat full blast until the desired temperature is reached, and then it shuts off until the temperature drops a certain amount below the desired point. In this way it cycles between these two temperatures, and the rate at which it cycles depends on how fast the heating element adds heat, and how much delay there is between heat being added and the temperature sensor sensing it.



I found that thermostatic control works, but there is enough delay between the heating element and the temperature sensor that the oven will overshoot quite a bit (probably close to 200 deg F) when it initially switches on and must rise to 140F from room temperature. This creates a marginally unsafe situation if someone opens the lid a few minutes after the unit is first powered on. It's also going to smell worse if any organic matter accidentally falls on the heating element.



Proportional-Integral (PI) control is a little more complicated, but works better. We'll need to use it in conjunction with PWM - and I'll first explain PWM briefly because the Wikipedia article is confusing and starts off with ugly math.



Pulse-Width Modulation: Averaging ON and OFF into something in between

The basic idea of PWM is that when we are only allowed to turn something all the way on or all the way off, we can achieve an effect equivalent to part-way on by rapidly switching it on and off and regulating the ratio of on-time to off-time. This works only when the thing we're controlling naturally averages out the pulses at the speed with which we're switching. For example, you can repeatedly poke the 'pulse' button on a blender or the gas pedal in a car almost fast enough to get a consistent average speed. But if you tried to flick a light switch on and off fast enough to get averaged dim lighting, you're out of luck - yet a fluorescent bulb blinks on and off 120 times per second with the AC supply and is averaged out by our eyes. For bigger things with more inertia, the pulses can come slower and still be averaged out. In our case, what we care about is the temperature of the contents of the baking pan, which won't heat and cool nearly as fast as the surrounding air in the oven (or the temperature sensor in the wall of the oven). So, if the oven air swings by a few degrees every few minutes, this is acceptable and the baking pan contents will stay at about the average temperature.



With this in mind, I chose 5 minutes as the cycle time for the PWM control of the heating element: every 5 minutes, the heating element will be turned on for between 0 and about 45 seconds, depending on what the PI controller wants. The reason for not turning the heater on and off more often is that switching it involves switching a relay on the power circuit board, which makes an audible click each time and would wear out much faster if it were switched frequently.



Implementing a PI Controller

Okay, now for the PI controller. An explanation of PI control would take up a bit too much space here, so I'll refer you to the reasonably well-written Wikipedia entry on PID control. The reason I leave off a derivative term (PI instead of PID) is that I know my sensed temperature is going to oscillate a bit anyway because the PWM cycle time is so long, and the oscillations I'll get without a derivative term are no bigger.



Since we're controlling the average temperature, we need to make sure we measure the average temperature for performing feedback control - so at the beginning of every PWM cycle, the controller should average out the temperature over the last cycle to decide how long to leave the heater on for in the new cycle.



Results and Fine Tuning

Testing a feedback controller that takes 5 minutes to do anything interesting can be a real hassle. One way to make life easier is to automatically record and plot what the controller is doing. Your programming tool for the microcontroller no doubt provides a debugging console which you can send text to from your program. Take advantage of this by sending a comma-separated list of useful variables each time your controller updates itself. Then, you can just save all these numbers into a text file, import them into Excel or another spreadsheet program, and quickly plot all the variables at once. Excel's scatter plot function will automatically plot each column against the first column, so if you make sure the first variable sent is the time, things will plot nicely with the default settings.



After some experimentation, I was able to get the performance shown below from my PI controller. Now I'm ready to do a more accurate temperature calibration: I can set the feedback controller to a specific setpoint based on the thermistor A/D value, fill the baking pan partway with water and suspend a thermometer in it, and wait until the controller reaches steady state (zero error for at least 2 cycles). Now I know that this particular thermistor value corresponds to a particular temperature. If I do this twice or better yet three times, I can plot them and draw a line which gives me my conversion equation! Keep in mind that your thermistor won't be terribly linear over a broad range of temperatures, so focus on making sure that your curve fit matches your data exactly at the desired setpoint. Some errors at other temperatures are fine, the feedback controller will still work.

