PWM controlled fan with ESPhome on ESP32

Hello all! I have just gotten my first ESP32 and to be perfectly honest I am a complete newbie around ESPs… But I at least managed to install ESPhome on Hassio and also I flashed my ESP32 so it connected on the network so, jay, so far so good.

The setup I want to achieve is actually just like this, except this is ESP8266 and I have ESP32:
image

Anyone have any pointers how I could attack this challenge? If I could get slider or dropdown to set the speed of the fan similar to this: https://esphome.io/components/fan/index.html?highlight=fan

So sorry for this open ended question but I really am new to this.

1 Like

Define a PWM pin (I cant read your image so I’ll use my ESP8266):

output:
  - platform: esp8266_pwm
    pin: D5
    frequency: 20000 Hz
    id: pwm_output

In home assistant I have this automation (for a light, but you can use the set fan speed service):

  trigger:
    platform: state
    entity_id: input_select.lounge_ac_mode
  action:
  - service_template: >
      {% if is_state('input_select.lounge_ac_mode', 'Silent Heat') %} script.fireplace_25
      {% elif is_state('input_select.lounge_ac_mode', 'Normal Heat') %} script.fireplace_75
      {% elif is_state('input_select.lounge_ac_mode', 'Powerful Heat') %} script.fireplace_100
      {% else %} script.fireplace_0
      {% endif %} 

And the scripts look like this:

fireplace_25:
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.fireplace
      brightness: 64

fireplace_50:
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.fireplace
      brightness: 128

fireplace_75:
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.fireplace
      brightness: 192

fireplace_100:
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.fireplace
      brightness: 255

It would be easy enough to map an input_number to the brightness/fan speed value instead.

data_template:
  brightness: "{{ states('input_number.fan_speed') }}"

Just make sure the input number only goes from 0 to 255.

1 Like

Thanks for a great reply! Do you know how I can define a PWM pin on a ESP32? When I try this I get the following:

INFO Reading configuration...
Failed config

output.esp8266_pwm: [source /config/esphome/basement_ventilation_fan.yaml:29]
  
  Component output.esp8266_pwm doesn't support ESP32.
  platform: esp8266_pwm
  pin: D5
  frequency: 20000 Hz
  id: pwm_output

That’s because I’m using the ESP2866 software PWM (not compatible wit the ESP32). You want this instead for the ESP32:

5 Likes

I think this might actually do the trick! :smiley: Testing right now.

2 Likes

Hello @tom_l, thanks again! It works :smiley:

esphome:
  name: basement_ventilation_fan
  platform: ESP32
  board: esp-wrover-kit

wifi:
  ssid: "xxx"
  password: "xxx"

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "xxx"

ota:
  password: "xxx"

output:
  - platform: ledc
    pin: GPIO19
    frequency: 10000 Hz
    id: ventilation_fan_pwm

fan:
  - platform: speed
    output: ventilation_fan_pwm
    name: "Basement Ventilation Fan"

Just put the integration in on Home Assistant and the high-medium-low works out of the box. Tried with a lower frequency first but it made no sense. At 10000Hz it works just as I hoped.

Thanks for your help!

4 Likes

Nice!

If you get any audible 10kHz coil whine (particularly at low speed) you can up the frequency to 20kHz easily.

Thanks, yes I might do that, but now I need to get my “installation” fan and build the component and then I can fine-tune. How easy it was with ESPHome! #impressed

Yep it is a brilliant project.

You’ll have to upgrade to a Ubiquiti distributed wifi access point system as you’ll have wifi gadgets everywhere before you know it :slight_smile:

2 Likes

I already have Ubiquity Mesh, but not the professional one. Wifi is really important these days!

1 Like

just want i was looking for!!! thx, do you by any chance want to share your HA config on this? :slight_smile:

There is no specific HA config other than the use of ESPhome. It creates the sensor for me and I get the fan device in the devices of HA. :slight_smile:

1 Like

I have attempted exactly the same way you have it setup and not able to get it work. Only difference is I am using a Amperage: 12V DC 1A 1 Amp 1000mA 12W power supply. Does it need more power? Amp?
Thanks in advance.

No it should not need more Amp than this. Nor sure why this does not work for you @dominate4life

Afternoon,

How did you work out the frequency for this?
I have the same MOS Module and I have 2 x 12v Fans connected and everything works great.

The problem I have is I want to control a 12v DC water pump that pulls 8A max. I purchased a XY-MOS that supports 400w 15A (https://www.robotics.org.za/XY-MOS) but I’m just not sure what Frequency to use in esphome or how to work it out. Google is telling me 0 - 20 Mhz supported signal frequency. is it that simple?

Yes I just tried different frequencies until I got the results I wanted. Trial and error :slight_smile:

Following this up

Apologies for posting on this thread, but it’s the only one that I can find that makes mention of using an ESP8266 at 2KHz.

@tom_l I have recently got an ESP8266 with integrated fets (Sinilink XY-WFMS), and have been trying to control a 5V fan in the same way @corvy is.

I’ve used this thread as a rough guide and have noticed that I get coil whine at low freqs as you mentioned above. I set the frequency to 20KHz as you suggested, but I lose speed control all together. I also noticed you set your frequency to 20KHz in your example. I’ve read the ESP8266 isn’t capable of going above about 2KHz due to it’s software PWM. There is an alternative PWM implementation that claims to combat this, but it’s not available in ESPhome at the moment. I opened a feature request, hopefully It is of interest to the devs.

How are you able to run an ESP8266 at 20KHz? Or was this just an example for Corvy’s setup?

1 Like

No it’s what I used. I’ve never actually hooked an oscilloscope up to it to check what frequency it is outputting but it works.

I’m not using MOSFETs, just a GP transistor. See here: HA Fireplace Light

And after refreshing my memory with that thread it seems the output is about 14kHz.

Where did you read that?

Thanks for the info. I read it in a blog post by the author of the improved PWM implementation:

Blog post: https://lurchi.wordpress.com/2016/06/29/esp8266-pwm-revisited-and-reimplemented/
Git: https://github.com/StefanBruens/ESP8266_new_pwm

The problem I am having is that I cannot get the fan to have low/med/high speeds as well as no whining on low speeds. If I set the freq to 20KHz, low/med/high are all the same. I tried setting varying integers for the speed in the ESPhome config (0.01 to 0.99 and many in between), but it’s only if I reduce it to about 2KHz that I get speed control back but the lower speeds are terribly whiny.

I’m trying to decide what the best option would be. I have a couple of 5V USB fans (2 wire) that I have gutted and want to replace their PCBs with a HA controllable alternative. The Sinilink XY-WFMS seemed great, but I cannot get it to perform as it did with the fan’s own board. I tried Tasmota to start, but the slider in the Tasmota UI was only useful for the last 10% or so. Anything below that was whiny. I tried adjusting the pwm freq via the console, but it doesn’t appear to go as high as 20KHz, so I moved to ESPhome and whilst I can set the freq to 20KHz, I still cannot get the results I want. A ton of reading led me to the above blog, which has now pushed me towards thinking an ESP32 and fet board may be the better option… Unless you have any suggestions? I am willing to try things before I put an order in for more parts.