Why arduino does not switch off the power when the setpoint is reached?

#include <SPI.h>
#include "Adafruit_MAX31855.h"

// Define the pins for SPI communication
#define MAXDO 3
#define MAXCS 4
#define MAXCLK 5

Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

#define SSR_PIN 10

// Variables
float real_temp;
float Setpoint = 20; // In degrees C
float SetpointDiff = 3; // In degrees C
float elapsedTime, prev_time;
float refresh_rate = 500; // PID loop time in ms
float now_pid_error, prev_pid_error;

// PID constants
float kp = 2.5;
float ki = 0.06;
float kd = 0.8;
float PID_p, PID_i, PID_d, PID_total;

void setup() {
// Initialization
Serial.begin(9600); // Adjust baud rate if needed
pinMode(SSR_PIN, OUTPUT);
digitalWrite(SSR_PIN, HIGH);

// Initialize SPI communication for MAX31855
SPI.begin();
thermocouple.begin();
}

void loop() {
// Perform PID control
PID_control();
}

void PID_control() {
// Calculate PID control
elapsedTime = millis() - prev_time;
if (elapsedTime > refresh_rate) {
// Get temperature reading
real_temp = thermocouple.readCelsius();
// Calculate error
now_pid_error = Setpoint - real_temp;
// Calculate PID terms
PID_p = kp * now_pid_error;
PID_d = kd * ((now_pid_error - prev_pid_error) / refresh_rate);
// Integral term (apply only when error is small)
if (-3 < now_pid_error && now_pid_error < 3) {
PID_i = PID_i + (ki * now_pid_error);
} else {
PID_i = 0;
}
// Calculate total PID output
PID_total = PID_p + PID_i + PID_d;
// Apply limits
if (PID_total < 0) {
PID_total = 0;
}
if (PID_total > 255) {
PID_total = 255;
}
// Control the SSR using PID output
analogWrite(SSR_PIN, 255 - PID_total);

// Check if temperature is within a range of the setpoint
if (real_temp >= Setpoint - SetpointDiff && real_temp <= Setpoint + SetpointDiff) {
  // Turn off the power source
  digitalWrite(SSR_PIN, LOW);
} else {
  // If temperature is not within range, ensure power is on
  digitalWrite(SSR_PIN, HIGH);
}

// Print values for debugging
Serial.print("Setpoint: ");
Serial.println(Setpoint);
Serial.print("Temperature: ");
Serial.println(real_temp);
Serial.print("PID output: ");
Serial.println(PID_total);

// Update variables for next iteration
prev_time = millis();
prev_pid_error = now_pid_error;

}
}

`Why when the setpoint is reached Arduino keeps sending signal to the powerswitch and does not switch off. The temperature over heats. What is wrong with the code

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

You are using serial.Print() for other things, why not use it to display the values involved in the test so you can answer your own question. That is called "debugging" the code.

1 Like

I do not think that most SSR will accept a PWM input.

But immediately after, you use digitalWrite() to set the same pin to HIGH or LOW.

So I think I understand the problem with your code. You have copied and pasted parts of "found" code together with no understanding of how they work. This is like welding together parts of cars, trucks, boats and planes and asking why it does not fly.

I suggest you read through the code you posted and make sure you fully understand what function each and every line of code performs. If you find any you cannot understand or you are not certain why they are needed, either delete them, or ask for advice about that part on this topic.

Post the exact SSR part number or model you are using and how you are connecting it to the Arduino.

Some SSR's require 3-3.5 V to turn off.

And if by-chance your SSR is controlling DC... Most AC SSRs are built with TRIACS. TRIACs latch-on until current drops to zero (i.e. the next AC zero crossing) so with DC they latch-on and never shut off.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.