0% found this document useful (0 votes)
118 views3 pages

Pid Temperatura Arduino

The document defines constants for a PID controller that is used to control the temperature based on input from a thermistor. It takes multiple samples from the thermistor, averages the readings, and calculates the temperature using the Steinhart–Hart equation. The PID controller computes new output values periodically to regulate the temperature setpoint based on the sensor input. It outputs debug information over serial periodically and can also output the temperature reading over serial.

Uploaded by

Fernando Gomez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views3 pages

Pid Temperatura Arduino

The document defines constants for a PID controller that is used to control the temperature based on input from a thermistor. It takes multiple samples from the thermistor, averages the readings, and calculates the temperature using the Steinhart–Hart equation. The PID controller computes new output values periodically to regulate the temperature setpoint based on the sensor input. It outputs debug information over serial periodically and can also output the temperature reading over serial.

Uploaded by

Fernando Gomez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#define INTERRUPTION 3

volatile int temp = 0;

double Setpoint, Input, Output;


const int sampleRate = 1;

#define Kp 22.2
#define Ki 1.08
#define Kd 114

PID ControlPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

//Temporal Variables
const long SerialPing =500;
unsigned long now;
unsigned long lastMessage=0;

bool SerialDebug = false;


bool SerialProcessing = false;

// which analog pin to connect


#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 100000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 4066
// the value of the 'other' resistor
#define SERIESRESISTOR 22000

int val =0;

//Temp Steinhart
byte steinhart;

int samples[NUMSAMPLES];

void setup(void) {
Serial.begin(9600);
pinMode(10,OUTPUT);
pinMode(13,OUTPUT);

ControlPID.SetMode(AUTOMATIC);
ControlPID.SetSampleTime(sampleRate);

//InitializeSetPoint
//Setpoint to 120 degrees
Setpoint = 120;
Input=0;
Output=0;

analogReference(EXTERNAL);
Serial.println("Temperature PID ");
delay(4000);
}

void loop(void) {

uint8_t i;
float average;

if(Serial.available() > 0) //Detecta si hay alguna entrada por serial


{
val = Serial.parseInt();
Serial.println(val);
if(val != 0)
{
analogWrite(10,val);
digitalWrite(13,HIGH);
}else{
analogWrite(10,val);
digitalWrite(13,LOW);
}
}

Input = temp;

ControlPID.Compute();

now = millis();

if((now-lastMessage > SerialPing) && SerialDebug){


Serial.print("SetPoint= ");
Serial.println(Setpoint);
Serial.print("Input= ");
Serial.println(Input);
Serial.print("Output= ");
Serial.println(Output);
Serial.print("Temp= ");
Serial.println(temp);
Serial.println();
}

// take N samples in a row, with a slight delay


for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}

// average all the samples out


average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES; // convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
float steinhart; steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
if(SerialProcessing){
Serial.write( 0xff );
Serial.write( ((int)steinhart >> 8) & 0xff );
Serial.write( (int)steinhart & 0xff );
}else{
Serial.print("Temperature ");
Serial.print(steinhart);
Serial.println(" *C");
}

delay(1000);

You might also like