How to change PWM frequency in grbl?

Hello,
How can I change PWM frequency in grbl ?

This depends on your firmware type and version. Standard grbl does not support this. You need to change the source code and compile it.
Newer versions / ESP32 ports sometimes support it, the parameter is called differently. In grbl_esp32 it’s called $Spindle/PWM/Frequency but I’v also seen this as a simple $number command in other ones.

2 Likes

Why do you need to change it? What effect are you trying to accomplish?

You’ll need to grab the GRBL source code (from GitHub), then dig into the spindle_control.c file. There’s a line in there that controls the PWM frequency—it’s tied to Timer2 on the chip.

You’ll see something like: TCCR2B = (1<<CS21); // Sets PWM to ~7.8kHz

That line sets the prescaler. If you mess with the prescaler bits (like changing CS21 to something else), you can bump the frequency up or down. But here’s the catch: you need to know what you’re doing, or you’ll break how GRBL controls the laser power.

After that, you recompile it using something like Arduino IDE or PlatformIO, flash it back to your board, and then you’ll have your new PWM rate.

So yeah, not plug-and-play—it’s more like “get your hands dirty and hope you don’t brick it.” But doable if you’re up for it.

Note that this applies to GRBL-based lasers using only the pure open-source GRBL source tree. In principle, you can fetch the manufacturer’s source code, tweak it, recompile it, flash it, and the machine is good to go.

However, if your “hobby laser” is typical, then it uses a proprietary / closed-source / ripped GRBL with unknown modifications and no public source tree. Because you do not know any of the details, including the I/O pinout, compiling and flashing stock GRBL on such a machine will likely result in a dead machine and severe unhappiness.

1 Like

If I am not mistaken, Sculpfun sets their diode lasers to 1500 Hz, having decided that was optimum. I think I read their parameter setting for it, but that would not help you.

What is the reason behind changing the period (base frequency) of the pwm signal?

How this relates to the material and to the lasers operation. A majority of this depends on what kind of system you’re using.

If you’re driving a co2 laser power supply, the period, within range, has no effect on how it lases. Depending on how this is wired, you can have different results.

:smiley_cat: