Spindle RPM - headache

Goal is to: Control spindle RPM with cmmd := M03 Sxxxx, where S is RPM value

Firmware changes:
in Configuration_adv.h:

#define SPINDLE_FEATURE  // Enabled

than enabled PWM on pin which is defined in pins_RAMPS.h: (pin 8 )

  #define SPINDLE_LASER_PWM     true   
  #define SPINDLE_LASER_PWM_INVERT      false

And now the part that gives me the headache:

  • Speed / Power can be set (‘M3 S’) and displayed in terms of:
    • PWM255 (S0 - S255)
    • PERCENT (S0 - S100)
    • RPM (S0 - S50000) Best for use with a spindle



if CUTTER_POWER_UNIT is seated as RPM and command M03 Sxxxxx is send, it turns on PWM on pin but voltage is between 0v and 0.56v.

if CUTTER_POWER_UNIT is seated as PERCENT or PWM255 it works like charm.
In percent mode I can send for example

M03 S50

and voltage on the pin will be ~6v, with

M03 S100

voltage on the pin is 12v, also if it is in PWM255 mode with command M03 Sxxx it works without the problem.

Ups… posted to soon accidentally, sorry guys.

So what you think, did I missed something…any ideas how to solve the issue.

The RPM has dinner more parameters. I think it basically has a min and max rpm. So if you set the min to zero and the max to 30000, M3 S30000 will turn it on 100% M3 S3000 will turn it on 3%.

If you just want the answer then here it is
The code is broken for 8 bit controllers trying to use RPM mode. PWM255 and PERCENT work.
In the code in the src file there is a line in class SpindleLaser that reads
return unitPower ? round(100 * (cpwr - SPEED_POWER_FLOOR) / (SPEED_POWER_MAX - SPEED_POWER_FLOOR)) : 0;

I tracked down a Github Marlin fix… one word added… float…
return unitPower ? round(100 * float(cpwr - SPEED_POWER_FLOOR) / (SPEED_POWER_MAX - SPEED_POWER_FLOOR)) : 0;

Make that change and you can set spindle speed from 0 to 50000 and get proper PWM output on Pin 4 (on RAMPS) which is the SECOND set of servo pins on the board.

How I got there…

Total newbie here, just finished my MPCNC, RAMPS controller, dual endstops. Got it built, working, cutting, but was MANUALLY controlling the spindle…

Spindle speed control was my biggest hurdle of this project I think…First trying to figure out what pin was the PWM pin took me forever… Eventually figured out that the second servo set of pins does this on the RAMPS board.

Still didn’t work… was missing something…

Once I found that I learned you have to modify the Marlin firmware to turn on the spindle control
Had to uncomment this in Configuration_adv.h to ENABLE spindle control

And unfortunately while I was there I saw the options for spindle control… PWM255 (0-255 values for PWM); PERCENT (0-100 percent for PWM); RPM (0-50000RPM) and PWM255 is the default

I thought “hey, I want RPM not PWM255” which was another define right there in the code.


The code is broken…

PWM255 mode works
PERCENT mode works
RPM mode is broken (if you are using an 8 bit controller like a mega2560 used on the RAMPS configuration like I was…)

I beat myself up for a long time on this before I even realized that PWM255 and PERCENT worked…
So I went with PWM255 and edited my ESTLCAM tools to set RPM to 255… (or whatever was approrpiate between 0 and 255)

But it bugged me so I went after it again today…

Thats when I found some github commits on the Marlin site… I figured the issue I was having was that I grabbed the code from V1Engineering that was customized for dual endstops etc… that fix wasn’t in there…

So I downloaded Marlin direct from github… same RPM issue… Huh?

Well in addition to being a newbie to MPCNC I am also pretty newbie to github so I didn’t know where to go from there other than back to the Marlin I downloaded from V1Engineering and just add the fix directly into my copy of the source…

Then I stumbled onto the V1Engineering forum and this topic… so I figured maybe this post will help someone else get back many hours of their life that I never will.


Outstanding work tracking down the problem and the fix, and providing such a clear explanation.

For the record, RAMPS pins 4,5,6, and 11 can all do PWM output. Good to know 4 is the default spindle pin.

I wish I had come here and read your post earlier… I weould have then at least known that PWM255 and PERCENT worked and that would have saved me some grief… but I might have just taken the lazy route then and not dug deeper… I was so far into it by the time I got things working I was in for the long haul!

So far I am totally loving my MPCNC… The goal is aluminum eventually but I figure I need to break a few tools and create a lot of sawdust before I tackle that… I thought 3D printing was easy… and 4 years into that I am still learning… Starting CNC machining I am realizing that 3D printing IS easy in comparison… Everything I learned 3D printing and laser engraving has been useful to get to this point but its also just gotten me to the kindergarten level… Wow have I got a lot to learn about speeds and feeds and toolpaths and… and everything…

And thanks for your feedback. Glad to help out.

Terry, excellent job thank you.
I’m software engineer with more than 20 yrs of experience but was to lazy to investigate the issue, so my approach was to edit Estlcams start/“tool change” sections and replace RPM cmmd with power percentage like S100 for max power and so on.
I can’t wait to test your solution-but right now I’m in the isolation because my wife has COVID-19.
I’ll report back ASAP.
Thank you!!!

And by the way if I’m remember well setting up spindle rpm from display didn’t worked to.

That is a good point, I too noticed that the Spindle Control on the LCD menu (appears when you enable Spindle Control so there is a hint that you have not enabled it in the Marlin firmware if that doesn’t show up on the menu)

Since I use program control I hadn’t looked into that aspect, but since you mentioned it, I just checked on my spare RAMPS 1.4 board that is currently loaded with Marlin from Marlin github vs Marlin from V1 Engineering and I see that on the github Marlin, spindle control DOES work from the LCD. Although I did notice things were not perfect even though they worked… For example on reset, the spindle menu says Spindle Power 0 and Spindle On. highlighting Spindle On and clicking turns the spindle on and the display still reads Spindle On and still Spindle Power 0. Press again and it read Spindle off and the spindle is really off. BUT… the Spindle power switches to 25000 from Spindle 0 to Spindle: 25000. So working but not displaying perfect.

Two differences here between my production and my spare RAMPS boards are that I am using the Marlin from the main stream on my backup RAMPS 1.4 board which may have a bug fix, and my spare board uses a plain text graphic display vs the full graphic LCD on my “production” RAMPS 1.6 board. Given there are both display and functionality differences between my two RAMPS setups there may be issues in different areas of the code.

I will see if I can track down the issue there at some point and follow up here.

1 Like

Well, bonus discovery…
Before I dug into the code I decided to just confirm that the LCD Spindle Power stuff was not working…
If you make the change to add that float operative to get the RPM mod working and enabled, in RPM mode, the Spindle Power controls on the LCD do actually work.

I will add that in the same section of Configuration_adv.h there are min (5000) max (30000) and default startup values (25000) that as far as I can tell do NOT affect the M3 Sxxxxx commands but do limit the LCD controls… to 5000 minimum and 30000 maximum.

Actually the behaviour is even more odd that I do not have a full handle on yet… I set my limits in firmware to 0 and 50000 to use the full range and and the LCD correctly displays that now. But when setting speeds it was clear that when my firmware was set to 5000 and 30000 the 30000 was actually giving full speed… when I dial in 30000 on the LCD now that the limits are 0-50000 that is way slower than when I had the firmware limits set to 0-30000 so I believe these limits in the firmware are the range through which your full speed range gets “scaled” for the LCD dial…

Further on this… may be issues…

I started having random milling errors. It would random detour… so far the way I have resolved these is to disable the spindle control. My current working theory is that spindle control is probably fine IF you are using PWM255 mode on an 8 bit RAMPS/mega256 setup but the RPM mode may be causing corruption elsewhere even thought RPM seems to be working… So for now I would hold off on the RPM fix and use PWM255 until I confirm.