[Help]​ Kp in pid controller is not steady oscillation

I have problem to tune pid controller in position controller of dc motor

I increase Kp and set Ki, Kd to Zero to tune byZiegler–Nichols method.
But Graph is not steady oscillation

excel file


#include <PID_v1.h>

//Abosolute Encoder 12 Bit 4096 
int CLK = 13; // Blu  Pin 10
int DO = 12; // Grn   Pin 11
int CSn = 11; // Ylw  Pin 12

//DC motor30
const int in_1 = 7 ;
const int in_2 = 6 ;
const int pwm = 5 ;

double setpoint = 180;

double p = 100;
double Ku = p/0.5;
double Pu = (12.981-12.488);

double kp = 0.5*Ku             ,ki=0,kd = 0;

//double kp = 0.45*Ku, ki = (1.2*kp)/Pu , kd =0;

//double kp = 0.6*Ku, ki = (2*kp)/Pu , kd = (kp*Pu)/8;

  double input = 0,output = 0;

 PID myPID(&input,&output,&setpoint,kp,ki,kd,DIRECT);
double Degree = 0;
double x;
double total_error,last_error;
double last_time,T = 1.3,current_time,delta_time;

void setup() {
  // put your setup code here, to run once:
    pinMode(pwm,OUTPUT) ; pinMode(in_1,OUTPUT) ; pinMode(in_2,OUTPUT) ;

   TCCR1B = TCCR1B & 0b11111000 | 1; 
  pinMode(CSn, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DO, INPUT_PULLUP);
  digitalWrite(CSn, HIGH);digitalWrite(CLK, HIGH);


void loop() {
  // put your main code here, to run repeatedly:
  int ee = 170;
  ReadEncoder() ;
          { int Angle = Serial.parseInt();
            if(Angle>ee || Angle<-ee)
          {   stop();
             Serial.println("Error:Angle should be in -170 to 170");
             kp = 30; 
             setpoint = 180;}
          setpoint = 180+Angle;}
         input = Degree;
  //Serial.print(" x = "); Serial.print(((x/4095)*359)-180);
         Serial.print(" ");Serial.print(setpoint-180);
          Serial.print(" "); Serial.print(input-180);
         Serial.print(" ");Serial.print(output);
           Serial.print(" ");Serial.println(delta_time);

void pwmOut(int out)
 if (out>0)
 { analogWrite(pwm,out);forward();} 
 {  analogWrite(pwm,abs(out));reverse();}

void time()

 current_time = millis();
 delta_time = current_time - last_time;

 if(delta_time>= T)
  double error = setpoint - input;
  double delta_error = error-last_error;
  total_error+= error;      
         last_error = error;
         last_time = current_time;


void ReadEncoder() 
{  time();
  float Data = ReadSSI();
   Degree = map(Data, 0, 4095, 0, 359); //cange data to Degree
int ReadSSI(void)//read clk cs do from encoder
{ int i, dReading;
  char Resolution = 12;
  unsigned int bitStart = 0x0800;
  dReading = 0;
  digitalWrite(CSn, LOW);
  digitalWrite(CLK, LOW);
  for (i = (Resolution - 1); i >= 0; i--)
  { digitalWrite(CLK, HIGH);
    if (digitalRead(DO)) dReading |= bitStart;
    digitalWrite(CLK, LOW);
    bitStart = bitStart >> 1;
    if (i == 0)
    { digitalWrite(CLK, HIGH);
      if (digitalRead(DO)) dReading |= bitStart;
  digitalWrite(CSn, HIGH);
  return dReading;

void forward()
  digitalWrite(in_1,LOW) ;digitalWrite(in_2,HIGH);

void reverse()
  digitalWrite(in_1,HIGH) ;digitalWrite(in_2,LOW);

void stop()
  digitalWrite(in_1,HIGH) ;digitalWrite(in_2,HIGH);

Two graphs, both show oscillation.

Please tell us about your project, explain what the graphs represent, and what you mean by "not steady oscillation".

Most forum members won't download unknown material from off site.

My project about pid controller to control angular position of dc motor between -​170 to 170.
Input angular position to arduino to control dc motor position and have absolute encoder feedback to arduino.(150=-30,180=0,210=30)​

Input setpoint>arduino order drive motor>motor move >absolute encoder feedback angular back to arduino

My problem when i setpoint and try to increase Kp to make steady oscillation

to put than value to Ziegler-Nichols Table for tunning pid controller. Graph angular show position when setpoint is 30 it move oscillation​ but not like in Ziegler-Nichols ​to calculate Pu for this method.

PWM graph show signal from controller to control drive motor by i set myPID.SetOutputLimits(-255,255) to out put pwm

Both graphs show oscillation. It does not have to look like the ideal case.

Reduce Kp until the oscillation stops.

Both graphs show oscillation. It does not have to look like the ideal case.

Reduce Kp until the oscillation stops.

So i just reduce Kp until osc... stop and use that value in Ziegler method?
And i can use Pu from different amplitube?

Why did you post those two graphs? They are not informative.

Consider posting the important details about your project, so readers can judge whether it could actually work correctly. See How to use this forum for hints on posting. Post the code, using code tags, a wiring diagram, and links to the motor, motor driver, motor power supply and encoder.

There are other tuning methods. Do some more reading and get a broader perspective.