# HOW TO PID CONTROL PELTIER BOTH COOLING AND HEATING

1 like 0 dislike

Dear sir i have gone through your interesting video on PID controller https://youtu.be/szm_y-KM1T4

i have tried to make same and it worked very fine i have just changed the LCD code for oled.

now i was trying to control the peltier with the built system for both cooling and heating using H bridge. but H bridge needs one more pin to change the polarity. can you please help me with the code. so that when the set temperature is lower than the reading temperature it activate another PWM pin for cooling and deactivate the heating pin and vice versa. I am a biologist and don't know coding just learned few things with the help of genius like you.

help will be appreciated

This is code i have tried to modify but not working

//We define PWM range for heating between 0 and 255

// if(PID_value < 0)

//  {    PID_value = 0;    }

// if(PID_value > 255)

// {    PID_value = 255;  }

//Now we can write the PWM signal to the mosfet on digital pin D3

//Since we activate the MOSFET with a 0 to the base of the BJT, we write 255-PID value (inverted)

if (set_temperature  > temperature_read);

analogWrite(Heat_pin,255-PID_value)

digitalWrite(Cool_pin,HIGH);

if (set_temperature < temperature_read);

analogWrite(Cool_pin,255-PID_value)

digitalWrite(Heat_pin,HIGH);

previous_error = PID_error;     //Remember to store the previous error for next loop.

in Arduino

## 1 Answer

0 like 0 dislike
Just make the PWM range from -255 to 255 and if the value is engative you activate the cool PID and if PWM is positive you activate the hot aprt of the PID
by (34.8k points)

I did this

//We define PWM range between 0 and 255

if(PID_value < -225)

{    PID_value = -225;    }

if(PID_value > 255)

{    PID_value = 255;  }

//Now we can write the PWM signal to the mosfet on digital pin D3

//Since we activate the MOSFET with a 0 to the base of the BJT, we write 255-PID value (inverted)

analogWrite(PWM_pin,255-PID_value);

previous_error = PID_error;     //Remember to store the previous error for next loop.

but not working.
actually i want another pin to activate for the cooling when set temperature is lower than the what thermocouple is reading. and when thermocouple reach to that set lower temperature it start maintaining that in same way it does for heating.
i will be thankful if you give me a example
Dear sir i have done some more modification.

i am little bit closer to it but the problem what i am facing now is

1. when i set higher temp values the hot pin LED blinks as usual (goes full off and on)

problem associated cool pin remains half on and blink effect due to increase in glowing intensity

2. when i set lower temp values the heat pin remain high and cool pin completely goes on off cycle.

problem when the required lower temperature is reached the heat pin also starts blinking as the first case.

i am sharing a part of my code please help.

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

delay(100);

display.clearDisplay();

display.display();

delay(100);

pinMode(Heat_pin,OUTPUT);

TCCR2B = TCCR2B & B11111000 | 0x03;    // pin 3 and 11 PWM frequency of 928.5 Hz

Time = millis();

pinMode(Cool_pin,OUTPUT);

TCCR0B = TCCR0B & B11111000 | 0x03;    // pin 5 and 6 PWM frequency of 928.5 Hz

Time = millis();

Last_State = (PINB & B00000001);      //Detect first state of the encoder

PCICR |= (1 << PCIE0);    //enable PCMSK0 scan

PCMSK0 |= (1 << PCINT0);  //Set pin D8 trigger an interrupt on state change.

PCMSK0 |= (1 << PCINT1);  //Set pin D9 trigger an interrupt on state change.

PCMSK0 |= (1 << PCINT3);  //Set pin D11 trigger an interrupt on state change.

pinMode(11,INPUT);

pinMode(9,INPUT);

pinMode(8,INPUT);

}

void loop() {

if(menu_activated==0)

{

// First we read the real value of temperature

int  temperature_read = readThermocouple();

temperature_read = readThermocouple();

//Next we calculate the error between the setpoint and the real value

PID_error = set_temperature - temperature_read + 3;

//Calculate the P value

PID_p = 0.01*kp * PID_error;

//Calculate the I value in a range on +-3

PID_i = 0.01*PID_i + (ki * PID_error);

//For derivative we need real time to calculate speed change rate

timePrev = Time;                            // the previous time is stored before the actual time read

Time = millis();                            // actual time read

elapsedTime = (Time - timePrev) / 1000;

//Now we can calculate the D calue

PID_d = 0.01*kd*((PID_error - previous_error)/elapsedTime);

//Final total PID value is the sum of P + I + D

PID_value = PID_p + PID_i + PID_d;

//PID_value = PID_p + PID_i + PID_d;

//We define PWM range for heating between 0 and 255

if(PID_value < 0)

{    PID_value = 0;    }

if(PID_value > 255)

{    PID_value = 255;  }

//Now we can write the PWM signal to the mosfet on digital pin D3

//Since we activate the MOSFET with a 0 to the base of the BJT, we write 255-PID value (inverted)

//if (set_temperature>readThermocouple());

//digitalWrite(Cool_pin,HIGH);

analogWrite(Heat_pin,255-PID_value);

//We define PWM range for heating between -225 and 225

if(PID_value < -255)

{    PID_value = -255;    }

if(PID_value > 255)

{    PID_value = 255;  }

//if (set_temperature<readThermocouple());

//digitalWrite(Heat_pin,HIGH);

analogWrite(Cool_pin,0-PID_value);

previous_error = PID_error;     //Remember to store the previous error for next loop.