PID output value to PWM signal??

Hey guys, hope ya’ll got a great weekend. I need some help on PID control code here.
I try to use PID control system to make a BBQ smoker temperature control. Here is part of the code.

 if (Serial.available()>0) {
  target_temp = Serial.parseInt();
  Serial.println(target_temp);
  }//set up target temperature, this can be changed anytime during program run
  current_time = millis();
  if (current_time - old_time > 3000) {          //take temperature reading every 3 seconds
    thermistorReading = analogRead(thermistorPin);
    TempInF = 1047 - ( 2 * thermistorReading);  //it convert analoge read from prob to temperature in Fahrenheit. the equation will change base on prob and resistor used in circuit
    //Serial.print(thermistorReading); //for testing only
    Serial.print("tempeature in Fahrenheit:");
    Serial.println(TempInF);
    Serial.print("Target tempeature in Fahrenheit:");
    Serial.println(target_temp);
    delay(1000); //just here to slow down the output for easier reading

    if (TempInF <= target_temp) {       //program active when reading temperature is smaller than target temperature
      error = target_temp - TempInF;
      Serial.print("tempeature difference:");
      Serial.println(error);
      interror = interror + error;
      Serial.print("interror test:");
      Serial.println(interror);
      fan_speed = (float) (3 * error + 0.05 * interror);

Now, I have the output of the system. How can I transfor it to PWM signal that I can use to control the speed of fan? I’m thinking use map function, but how does it work? the range is a variable.

You only provided a snippet of your code. How are we supposed to answer an incomplete question?

map(...) takes your variable (which I am guessing is fan_speed),
the range of your variable (which I can't guess because of a lack of information),
the range of your output (which is 0 to 255 for PWM using analogWrite(...)),

and "outputs" (in the sense of a function) the equivalent value.

Tell us:

  1. What variable you want to output,
  2. What its range is, and
  3. Whether your PWM is analogWrite(...) or something else,

and we may be able to help you.

A wiring diagram and ALL of your code would help too.

Yes, show your complete code please.

When using PID functionality avoid using any delays, such as you have:

delay(1000); //just here to slow down the output for easier reading

Why would you have that anyway when you have a three second non-blocking delay, it makes no sense at all ?

Also, there should be no reason to make use of any floating point maths at all. you can do this simply with integer maths. I mean, your setpoint is an integer to start with, and your analog read is only ever going to be an integer.

I try to use PID control system

PID is not simply input_reading - setpoint to give an error, then multiplied by some factor, that is only the 'P' part.

Why do you print back the setpoint via the serial that has just come from the serial.

Unfortunately what I see is a tangled mess.


Paul