PID Control for Stepper motor's position, Output help

i am new in coding or even arduino overall, and so i need help to write my code

i am attaching a stepper motor to a valve to control steam flow. the input would be temperature from a thermocouple. i would need some PID as to make my valve able to control the flow proportionally.

i've read some PID libraries, but it states that i need to define only 1 pin as an output. from what i know, stepper motor needs 2 pins as their input, which is the Pulse pin, and the Direction pin.

i also has downloaded accelstepper to help me control the motor, and so i know that i can control the motor by using mystepper.step (number of steps), am i able to use this variable to be the output of the PID library ?

this might be the thing that i need, but still i don't know how to make the output of this library to be the input of my stepper motor.

/********************************************************
 * PID Basic Example
 * Reading analog input 0 to control analog PWM output 3
 ********************************************************/

#include <PID_v1.h>

#define PIN_INPUT 0
#define PIN_OUTPUT 3

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

void setup()
{
  //initialize the variables we're linked to
  Input = analogRead(PIN_INPUT);
  Setpoint = 100;

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(PIN_INPUT);
  myPID.Compute();
  analogWrite(PIN_OUTPUT, Output);
}

i do know that i can control the max and min PID output, as to limit my stepper motor movement, but yet to know how to make it as an input for my stepper motor. thanks for the help guys, really meant something for me :slight_smile:

Compare the new PID value with the previous PID value. Then You know if the stepper should move CW or CCW, plus or minus. You know the stepper position and the size of the PID difference set the amount of steps to go.

PID is not normally applicable to temperature control due to long time lags. Better use a digital control with hysteresis.

i dont think digital control is good in controlling a valve, since the valve could oscilates forward and backward way too many times

That's why hysteresis has been invented.

I would start with a simple controller and move to PID if such a thing isn't adequate. Assuming the steam is heating something, check the temperature periodically. If it's too cold, open the valve a bit. If it's too hot, close it a little. If it's ok (hysteresis), do nothing.

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