PID CONTROL OF DC MOTOR WITH POT FEEDBACK USING ARDUNIO UNO

i need help with how to tune the parameters of PID . And can anyone verify if my code is correct. im trying to do the position control of a dc motor with pot acting as a feedback using arduino and PID.
thanks for your help in advance.

//PID constants
double kp = 2;
double ki =5;
double kd = 1;
 
unsigned long currentTime, previousTime;
double elapsedTime;
double error;
double lastError;
double input, output, setPoint;
double cumError, rateError;

int enB = 5;
int pin1 = 7;
int pin2 = 6;
int pot=A0;
int CurrentValue;
int  DesiredValue; 
void setup()
{
  pinMode(enB, OUTPUT);
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);

  Serial.begin(9600);

}
void loop()
{
  input = analogRead(A0);                
        output = computePID(input);
          delay(100);
        analogWrite(enB, output);

CurrentValue = analogRead(A0);
DesiredValue=600;  
setPoint=DesiredValue;
  if(DesiredValue>CurrentValue)//left
{ 
  
digitalWrite(pin1, LOW);
  digitalWrite(pin2, HIGH); 
 
       analogWrite(enB,255); 
}

  
 if (DesiredValue< CurrentValue) //right  
  {
     
  digitalWrite(pin1,HIGH);
  digitalWrite(pin2, LOW);

       analogWrite(enB,255);
  }
  
if(abs(DesiredValue-CurrentValue)<20)
{
   CurrentValue = analogRead(A0);
  analogWrite(enB,255);
  digitalWrite(pin1, LOW);
  digitalWrite(pin2, LOW);
}

        }
        double computePID(double inp){     
        currentTime = millis();  //get current time
        Serial.print(millis());
Serial.println(currentTime);    
elapsedTime = (double)(currentTime - previousTime);       
        
        error = setPoint - inp;                               
        cumError += error * elapsedTime;                
        rateError = (error - lastError)/elapsedTime;   
 
        double out = kp*error + ki*cumError + kd*rateError;  //PID output
 Serial.print(out);
        

        lastError = error;                               
        previousTime = currentTime;                       
 
        return out;                                   
}

Hi,
What model Arduino are you using?
What motor and controller?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Are you trying to make a servo?

Thanks.. Tom... :slight_smile:

I'm using an arduino uno and a dc motor with the potentiometer inbuilt with the motor acting as a feedback .no im not trying to make it a servo .i have made a very rough sketch of my connections .

Hi,
Thanks for the diag.


Your code shows you have declared a value of 600, but it would be better for you to attach another pot so you can change the position value as the code runs.

Can you post link/data/picture of your motor/pot assembly please?

Tom... :slight_smile:

Hi,
I hope you do not have the pot connected to the motor input terminals.
This would be better.

Tom... :slight_smile:

There are tutorials on the web for setting up the tuning parameters- I think there is a motor positioning one on you tube with an Arduino.

The motor is already enclosed in outer casing and sealed due to which i might not be able to show the connections. and the value that is declared as 600 is a setpoint i have given as reference value .i have to use the serial monitor to control the position of the motor .the pot ranges from (60-960). since i was not able to view the serial plotter and serial monitor at the same time that why i gave a reference value.