AndreaLombardo / L298N

An easy to use Arduino library to control DC motors with an L298N module
GNU Affero General Public License v3.0
130 stars 50 forks source link

PWM frequency #18

Open kjkent opened 1 year ago

kjkent commented 1 year ago

Hi there, and thanks for writing this useful library! Do you have any plans to implement the ability to set the PWM frequency, and would you be open to a PR that implements this? My L298N makes a pretty harsh whine at the frequency it's currently at.

Thanks!

AndreaLombardo commented 1 year ago

Hi, thank you for your appreciation. Unfortunately right now I don't have much time to spend on this project. If you have a solution to the problem, it would be interesting to see it. Thank you!

kjkent commented 1 month ago

Hey @AndreaLombardo! Super late follow-up as I've only recently found time myself to look at this.

So the standard Arduino analogWrite() uses a set Hz value for its PWM, I think it's ~500-900Hz depending on IC/pin.

It's fairly easy to implement a frequency change on, e..g., an ESP-32 (which is the platform I'm using), but, doing so seems to conflict with the use of Arduino's functions which will expect the PWM to be at its standard frequency. Straying from such ruins the portability the library has, so its not much of a PR-able solution.

My next step is to see how Arduino manages cross-compatibility with its existing library. Also, Particle seems to have an API that's based on, and extends, Arduino, and their analogWrite() accepts a third argument for frequency. As they produce dev kits across multiple IC platforms, their codebase may hold some good tips!

I'll report back if/when I figure anything useful out (see you next year! :wave: :laughing:)