First, my personal solution was just turning my router on and off, and I did exactly what Barry describes including an IOT relay like the one he references and using a fan pin with M106 and M107. This solution did not require me to modify Marlin.
From the g-code side of things, I know of four pathways used for on/off and sometimes speed control.
- Use a fan pin and M106/M107 to set on/off or to set PWM level. Note that fan pins will not be 5V, and they may use ground-side switching, but you can reassign the fan pin to a 5V pin (requires changing Marlin). Typically the insertion of the M106 and M107 is automated using the Start and End code of the CAM solution or the g-code sender.
- Use an M42 to control an “arbitrary” pin. This solution is nearly identical to #1, but it does require you to track down a free (and possibly PWM if you need it) pin. This solution allows you control a 5V pin without modifying Marlin.
- Use M3, M4, and M5 to control the spindle. I know based on topics in the forum that some people have struggled to get this working. I don’t believe these g-codes are enabled by default in the V1 maintained firmware, and there may be conflicts between controlling the spindle and the laser code since they typically specify the power/speed differently. The upside to this solution is that you can probably get your post processor to generate these codes, and they would map the RPM you specify in the CAM to a PWM level you can used to set the RPM of your router And this solution would, if supported by your post processor, allow you to change the speed during your job .
- Use the inline speed parameter associated with the G0, G1, G2 and G3 codes. This solution has the same strengths and weaknesses as #3, though I believe the changes in Marlin to support this solution are a bit different than #3.
Solutions #3 and #4 are the only ones that “automate” setting the RPM, but getting them working can be significantly more work mucking around in Marlin’s configuration files.
As for wiring things up, it will depend on your control board, your switching solution, and whether you are trying for speed control (not just on/off). What control board are you using?
Note there are a number of topics on this forum on speed controls for routers. In an ideal world, you want a solution with a feedback loop (PID control) since without it, the RPM of the router will vary somewhat depending on the load. Here is one topic that explores PID control.