PWM input for PID CNC Spindle Control via GRBL

Hello

I have just completed my CNC spindle control to these instructions on youtube DIY Arduino/Leonardo CNC Spindle Speed Control - YouTube by jmhrvy1947

It works great for manual control and have managed to tune the pwm output to suit my motor controller and adjust for max speed of 12000 rpm (200w chinese spindle motor)

My problem now is that I very much want to be able to set the motor speed using G Code via my GRBL system (Protoneer 3.51 cnc shield with PWM output between 0.02 - 5v with 0V when spindle disabled) The Min and max rpm is adjustable in the GRBL 1.1F parameters.

Also it would be ideal to use the lcd keypad shield or potentiometer to be able to adjust the spindle speed on the fly by +/- 10%

Any help is greatly appreciated

CNCSpindleSpeedControl.ino (35.8 KB)

I see the commands: M3, M4, M5: Spindle Control on the GRBL source code site. Do these not work for you? If not, then the GRBL source code is available for download.

Paul

I understand that.

What im trying to do is take the PWM output from GRBL and use it as the input for my spindle control. So in effect translate the PWM outputted by GRBL into an RPM set point for the PID control loop.

Im very much a beginner at arduino programming so here to ask for help.

Pffabrications:
I understand that.

What im trying to do is take the PWM output from GRBL and use it as the input for my spindle control. So in effect translate the PWM outputted by GRBL into an RPM set point for the PID control loop.

Im very much a beginner at arduino programming so here to ask for help.

Then it's probably time to use your oscilloscope to study the two signals.

Paul

Might be easier to integrate the GRBL PWM to a DC voltage with a low pass filter, read that with an analog input and map the ADC result to a PID setpoint.

//Reads a GRBL's spindle PWM signal's duty cycle and converts to rpm value.


#define READ_PIN 6


int RPM;
int maxrpm = 12000;
static double duty;
static double freq;
static long highTime = 0;
static long lowTime = 0;
static long tempPulse;


 

void setup()

{
    
  pinMode(READ_PIN,INPUT);
  
  Serial.begin(9600);
  
  }
  
  byte x = 0;



void loop()


//================================================================================================
//CALCULATING PWM DUTY CYCLE AND CONVERTING TO RPM
//================================================================================================

  {
  readPWM(READ_PIN);

  //=================================================================================================================
  // Print duty to serial
  //=================================================================================================================
  
//  Serial.println(duty); //prints 'duty' to serial monitor

//=================================================================================================================
//Print calculated RPM to serial
//=================================================================================================================

  Serial.println(RPM); //Prints calculated RPM
  }

//================================================================================================
//Takes in reading pins and outputs pwm duty cycle.
//================================================================================================
  
  void readPWM(int readPin){
  highTime = 0;
  lowTime = 0;
  
  tempPulse = pulseIn(readPin,HIGH);
  if(tempPulse>highTime){
  highTime = tempPulse;
  }
  
  tempPulse = pulseIn(readPin,LOW);
  if(tempPulse>lowTime){
  lowTime = tempPulse;
  }

//========================================================================================
//Calculates rpm from duty cycle % (120 = max rpm 12000/100 )
//========================================================================================


  duty = (100*(highTime/(double (lowTime+highTime))));
RPM = duty * (maxrpm/100);

  delay(500);        // delay in between reads for stability


}

I have built the same spindle controller and its working nice.
It would be nice to controlling it with grbl.
Have you got it working?