Helping with stablizing temperature with arduino PID code

Thread Starter

Kadav

Joined May 11, 2018
132
Hello

I am programing a PID temperature controller and i managed to program the Proportional to keep the actual temperature around 37 deg ( 36.5-37.5), which is different from the temperature readings in the sketch . but i want some advice on how to program the rest of the code like the integral to give a smoother value and not keep rotating .

There is a problem with the thermocouple that rotates around (0.25-0.75) everytime it reads a value . and that causes me a major problem with coding the PID..
Code:
#include <SPI.h>
#include "Adafruit_MAX31855.h"

#define CS 13         // pin 1 connect to pin 13
#define MISO 12  // pin 3 connect to pin 12
#define SCLK 10      // pin 4 connect to pin 10       

double input,setpoint, integral , proportional,piderror,pidvalue;

Adafruit_MAX31855 Temp(SCLK, CS, MISO); // function for PID

const int pin=3; //output pin
double cumerror=0; //start of the integral

double kp=150;  // proportional value
double ki=0;   // integral value

void setup()
{
  setpoint=37.5;
  Serial.begin(9600);
  delay(500);
}

void loop()
{
input=Temp.readCelsius()-1.5;// reading temperature from the thermcoupole (MAX31855)
piderror=setpoint-input; 

cumerror+=piderror;           // did not use the integral
integral=ki*cumerror;

proportional=kp*piderror;


pidvalue=proportional+integral;  // i just made the integral =0

if (pidvalue<=0)
{
  pidvalue=0;
}

if (pidvalue>255)
{
  pidvalue=255;
}

Serial.print("Temperature en C = "); Serial.println(input);    // acquisition de la température en degré Celcius

analogWrite(pin,pidvalue);

delay(1000);
cumerror=cumerror;// did not use the integral

}
the values of the temps are
1598254314921.png


thank you very much
 

trebla

Joined Jun 29, 2019
209
MAX31855 temperature resoluton is 0.25C. Maybe you must average readings or use you own calculated temperature resolution, 0.5C for example.
 

andrewmm

Joined Feb 25, 2011
574
Can I suggest that unless this as exercise to learn pid code,

just use the arduino library
https://playground.arduino.cc/Code/PIDLibrary/

With also has links as to how to tune the PID.


On the other hand,
why do you need a PID controller.

your simple controller seems to be doing just great,
as mentioned above, the resolution of the sensor your using is less than you require,
its the classic "my adc doe snot have enough bits " question.

The solution as mentioned is to take more samples and average .
every 4 samples you average, gives you one more bit of resolution,

I'd be tempted to just run as you are , till your close, then when you are, use a running average of the temperature.
 

Thread Starter

Kadav

Joined May 11, 2018
132
Hello i just averaged the signal but i have another problem as my ADC crushes after reading many values probably because the sample time is 1sec and gets tired , i don't know . how can i fix that problem ?
 

MrChips

Joined Oct 2, 2009
21,868
Actually, start off by getting a stable temperature reading.
You are looping every 1000ms. That means that you have time to take more than one temperature reading during the 1-second interval. Try taking more samples, 4, 8, 10 samples within the 1-second interval and take the average.
I do moving averages as follows:

sum = sum + new - avg
avg = sum/n

If your readings are corrupted by AC line frequency noise then you can reduce the noise by sampling at an integral multiple of the line frequency.
For example, if your AC line frequency is 50Hz, the cycle period is 20ms. Sample every 1ms for 20 samples/cycle or every 2ms for 10 samples/cycle.
 

jpanhalt

Joined Jan 18, 2008
10,233
Are you using the bare MAX 31855 or a breakout board (BOB) with DAC? Which board? Why not just read the digital signal? With the similar MAX31856 there is an error flag that must be cleared when it occurs or the device will appear to "freeze." Does the BOB have a similar flag?

I have experience with a lot of incubators, and as I said before, unless you have active cooling (e.g., a fan), you will have a hard time regulating temperature. Yes,it can be done, but it is hard to do if the room is also hot.
 
Top