I am really hoping this is the right forum to ask this, because it's related more to controlling a fan via PWM than anything else, I think.
I recently got one of those small cheapo USB desk fans, which I want to control via Home Assistant. It's a small 5V unit with it's own board, and a surface mount button than cycles through the speeds: low > med > high > off : https://www.ebay.co.uk/itm/Portable-Mini-USB-Fan-Powerful-Air-Circulator-Desk-Fan-Ultra-high-Wind-UK/353171305721 (https://www.ebay.co.uk/itm/Portable-Mini-USB-Fan-Powerful-Air-Circulator-Desk-Fan-Ultra-high-Wind-UK/353171305721)
I originally thought of using an ESP to bridge the button and simulate a press, but I would have no way of knowing what state the fan was in. After some recommendations, I decided to use an ESP8266 board with integrated MOSFET, namely the Sinilink XY-WFMS: https://tech.scargill.net/sinilink-xy-wfms-esp8266-board-with-mosfet/ (https://tech.scargill.net/sinilink-xy-wfms-esp8266-board-with-mosfet/)
I have flashed this with the Tasmota firmware (compiled without the 4KHz limit), and configured the template. After changing GPIO04 from relay to PWM, I am able to turn the fan on and off and control its speed (to some degree).
The problem I am having is that PWM is totally perplexing me. I understand in general how it works, but the fine tuning aspect of it is not something I seem to be able to get right. I have read on several sources that you want a PWM frequency of at least 20KHz for a fan to avoid whining at lower speeds. If I do this, however, I don't seem to be able to control speed. If I take it down to about 12KHz, I get some very minor amount of speed control at one end of the slider, but it's whiny as hell at lower speeds. Below that, it's extremely audible.
So the major issue here is speed control without whining - I don't seem to be able to get both at once! If I understand correctly, speed is related to the duty cycle, not the frequency, but there is no duty cycle option in this firmware... I thought that it might be 'pwmrange' option relates to but I have tried playing with that to no avail. I first tried asking for help with the Tasmota guys, but whilst they are very helpful with the firmware side of things, they seemed to know less about the PWM side of things - ie. they were suprised I would want to run PWM at 20KHz. I think the usual use is things like LEDs. I am hoping someone with knowledge of Tasmota will be able to assist here.
Because I wondered if this was a Tasmota issue, I also tried using ESPhome, but had similar results. I set the frequency to 20KHz, and even tried different integers for the speed settings. Similarly to the Tasmota guys, the ESPhome guys were helpful, but I never managed to figure out how to solve it.
Here is an example of my ESPhome config:
output:
- platform: esp8266_pwm
pin: GPIO04
frequency: 20000 Hz
id: fan_pwm
fan:
- platform: speed
output: fan_pwm
name: "Test Fan"
speed:
low: 0.50
medium: 0.75
high: 1
Presumably, the above speed setting integers affect the duty cycle? But on 20KHz, even with a value of 0.01, it doesn't slow the fan down.
One thing to note is that the ESP8266 uses software PWM, not hardware PWM like the ESP32. Although the ESP8266's software PWM has improved greatly, could this be the cause of the issues? Is using an ESP8266 a dead loss for this application? Or do I just need to try many different PWM values. If so, what? If I lose speed control at 20K but it's whiny at 12K, what am I doing wrong?
TIA for any help :)
Edit: One thing I have been considering, is if purchasing a budget oscilloscope might be useful, so I can see what frequency the fan's own board is putting out... would this be a good idea?