What’s limiting your PWM frequency? You want it above 25 KHz so it’s outside the range of human hearing.
Ask Electronics
For questions about component-level electronic circuits, tools and equipment.
Rules
1: Be nice.
2: Be on-topic (eg: Electronic, not electrical).
3: No commercial stuff, buying, selling or valuations.
4: Be safe.
I think the limitation is my own knowledge of this LEDC library. When I run it with the frequency value of 25,000 it doesn't output anything on my GPIO pin that I can detect with my multimeter.
I’m reading the docs and it seems capable of running up into the MHz range although at higher frequencies the duty cycle resolution is reduced. Although I wouldn’t expect most multimeters to detect a frequency that high, anything should pick up the voltage.
Do you get an error on the serial monitor? It should report if the frequency/duty-cycle range you’re requesting isn’t possible.
E (196) ledc: requested frequency and duty resolution cannot be achieved, try reducing freq_hz or duty_resolution. div_param=128
Try adding a large, low ESR capacitor across the power rails on your board. Preferably a polymer electrolytic if you have some.
Thanks for the idea, I'll give this a try but have to go and get some
Caps are definitely the first thing to try. To add on, the higher your frequency, the smaller caps you'll need. At 10kHz you'll need around 200uF of decoupling but at 50kHz you'd only need around 40uF. The smaller capacitance means you can find caps with better ESR, or just fit into a smaller space in general.
The drawback of higher frequency is that you'll be charging and discharging the gate of the MOSFET more often, which could mean heating it up and hitting thermal limits quicker. There's also a tradeoff within the MOSFET itself between low on-resistance and lower required gate charge - for slow switching you can find a FET with low Rds and high gate charge since youd be switching less often, but for very high frequency applications the amount of energy you put into charging and discharging the FET (mostly since the FET will spend a longer time in its linear region) can outweigh the savings of the lower resistance. Yay tradeoffs!
Thanks for typing this up! This helps me understand a lot more about what is going on.
I may have purchased too large of cap for the first try: 470uF. I bought it online so still waiting for arrival. I'll get some in the 40uF to 200uF range for more testing
470uf should be fine - bigger is almost always better, except if you sacrifice higher ESR for it in an application that requires lower ESR. It's pretty common to combine a large cap with higher ESR (like an Electrolytic or tantalum) with low ESR ceramic caps. That way the large cap can handle the high speed bulk C while the smaller cap can handle the high speed stuff and switching edges.
Did you make sure the cap you picked out was rated for the voltage you are working with? For hobbyist stuff it's usually a good idea to heavily derate voltages, to avoid blowing things up. For example, if I was working with a 24V power supply, I wouldn't nab a 25V cap; I would spring for a 35V (or even a 50V if I'm feeling particularly paranoid). You'll see derating like this commonly in commercial applications, and extremely frequently in military/aerospace applications.
As a rule of thumb you should always derate by at least 20%, then increase to 100% depending on how much ripple or switching the cap will see. For this application I'd probably want to derate to at least 50%
Are the changes in pitch alone, or does the amplitude change as well?
What happens if you set the frequency to something extremely low? I know that 50Hz is unusable, it will flicker, but does something still hum at 50Hz? What about if you increase the frequency in steps until you approach 200Hz?
Your multimeter couldn't measure anything when you went to 25kHz? That may be an issue of the microcontroller not supplying enough current to charge the gate capacitance on the mosfet in time, or not drain it fast enough to turn it off. If you disconnect the esp from the rest of the circuit, can you measure something then?
If yes, then you'll want to use a push pull pair for driving the mosfet. Or get a mosfet driver, but a bc547/bc549 pair and a bit of passives will be fine, available in through hole, and considerably cheaper.
If no, then either the esp can't go that fast in this implementation (was it a software PWM?) or your multimeter doesn't work in that range.
Interesting suggestions, I would not have tried such low frequencies except based on your suggestion and it turns out that going very low, below 1000Hz and even down to 100Hz causes the power supply whine to almost entirely go away. I also have a 470uF capacitor in between the power rails now too.
This range is a usable - there is no visible flickering even at very low duty cycles.
From what I understand in the reference guides the LEDC library attaches directly to the 80mhz or 40mhz esp32 hardware clock for PWM. Intuitively I'd have guessed that higher frequency would always mean less discernable audible feedback, but seems not in this case.
This range is a usable - there is no visible flickering even at very low duty cycles.
Before deciding on the frequency, and then mounting the strip in a position where the LEDs are directly observable, you should try moving the LEDs fast relative to your eyes. Eg. take the end of the strip and wave it from side to side. You may notice flicker at frequencies below 200Hz.
Ok, good thinking. I settled on 1000Hz and also made it something in remotely reconfigure should the need arise. That combined with the other suggestions, and getting a better power supply has made the system whisper quiet now.
It might be doing the PWM in software somehow, making it useless for this kind of control.
You could see if there's some other class or library or option or particular pin that can use a pulse generator or timer to do PWM instead.
I'm not really familiar with the exact environment but that's just what comes to mind.