I’ve got an XIAO that I’m using to send PWM signals to a motor controller, nothing special. The problem is that the motors whine quite loudly, because the PWM frequency is in the human hearing range. I am currently using the Arduino IDE to write my code, and I am simply using the analogWrite() function to set the PWM duty cycle. Is there a way to increase the PWM frequency by e.g. changing the prescale value for the appropriate timers?
I apologize if this has already been covered, but I can’t seem to find anything that clearly solves this problem (which is surprising, considering how common I imagine this problem is).
I may have finally found something… It is not specific to the XIAO, but is for anything running a SAMD21:
I’ll give it a try and see what I can come up with.
Well it took me long enough, but I finally cracked it. I was able to set the XIAO’s PWM frequency to 20kHz, which is out of (most) human hearing range. Sorry I wasn’t nice enough to make this its own stand-alone repo, but here is the relevant code within my robot’s repository. See the wiring_analog_extras.cpp/.h, specifically.
Hope this helps some of you out!
Many thanks for this tip and library code. Really saved me big time with a project I am working on.