PWM Fan Control issue

Hi there,

i build a smart ventilation for my bathroom with 2 PC Fans. It all runs properly but i cant control the fan speed with PWM. Im using the Arctic P12 MAX Fans. For testing i used another PWM Fan and i could controll the Speed with PWM. But with the Arctic Fan it just turns off if i put it to 99%. On 100% it runs with full power.
The Arctic Fans have a 0% Mode which shuts them off at <2% PWM thats why i bought them. But they shut off way to early.

I tested the Fan on my Computer and it worked, but it would shut off when lowering the Power to 50% or less in the Fan Controll Software. I could live with that and fit it in software but currently i cant controll the Fans at all in ESPHome.

The Code:

esphome:
  name: bad-luftung
  friendly_name: Bad LĂĽftung

esp32:
  board: esp32dev
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "LonTooc7wix+dQ/gatS/YlSAB1KZfdszEG3wlVU1Idc="

ota:
  - platform: esphome
    password: "887229c5f85b2597dbdf08535d19409b"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  manual_ip:
    static_ip: 192.168.0.36
    gateway: 192.168.0.1
    subnet: 255.255.255.0
    
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bad-Luftung Fallback Hotspot"
    password: "QUnImxeXIFZ4"


captive_portal:
sensor:
  - platform: pulse_counter
    pin: GPIO14
    name: FAN 1 RPM
    id: fan_1_pulse
    unit_of_measurement: 'RPM'
    filters:
      - multiply: 0.5
    count_mode:
      rising_edge: INCREMENT
      falling_edge: DISABLE
    update_interval: 3s

  - platform: pulse_counter
    pin: GPIO27
    name: FAN 2 RPM
    id: fan_2_pulse
    unit_of_measurement: 'RPM'
    filters:
      - multiply: 0.5
    count_mode:
      rising_edge: INCREMENT
      falling_edge: DISABLE
    update_interval: 3s

  - platform: dht
    pin: GPIO25
    temperature:
      name: "Bad Temperatur"
    humidity:
      name: "Bad Luftfeuchtigkeit"
    update_interval: 60s

servo:
  - id: my_servo
    output: pwm_output

output:
#Fans
  - platform: ledc
    pin: GPIO13
    frequency: 25000 Hz #25000
    id: fanhub_pwm

#servo
  - platform: ledc
    id: pwm_output
    pin: GPIO26
    frequency: 50 Hz

fan:
  - platform: speed
    output: fanhub_pwm
    name: "PWM Fan"

number:
  - platform: template
    name: Servo Control
    min_value: -100
    initial_value: 0
    max_value: 100
    step: 1
    optimistic: true
    set_action:
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return x / 100.0;'

Does anyone has an idea why this happens and how to fix it ?

Hello!
I have the same problem with facing same issue but no response from anyone and couldn’t find this topic troubleshooting in search engine.

How are you driving the fans?

Are you using an RC filter to smooth the PWM?

Have you tried lower frequencies?

How is your fan circuit?
What is the power source of the fan?

12v are directly from the Powersupply. The PWM Signal is from the ESP32 itself. I guess some Fans dont like the 3.3V PWM Signal and need 5V wich the ESP cant Provide.

Maby a Levelshifter could help ?

I have tried varying the Frequencie, but it didnt help.

Can be, but more likely your fan expects open collector output.
Do you have any NPN transistor available ?

No need the ESP32 has an open drain output pin type.

e.g.

output:
  - id: ch_1
    platform: gpio
    pin:
      number: 4
      mode:
        output: true
        open_drain: true #### <-----
      inverted: true
1 Like

Absolutely worth to try.
I remember I tried PWM with open drain pin mode many years ago on arduino framework and it didn’t work. Things might be different now though.

I wasn’t aware of that limitation. I’ve never used it for PWM. Only interfacing to remote control buttons.

Maybe there is not any. I remember back then I found some discussion about how LEDC was implemented on Esp arduino core x.x.

1 Like

I tried that but sadly it didnt help. Code compiled but it is still shutting off the Fan at 99% PWM Signal.

I dont have any transistors available and i think of just swapping the fans out for something idfferent. In Testing every other Fan i tried worked. Just those Artics didnt. Little bit frustrating because i know that they should work but i dont know how

So you configured the pin OUTPUT_OPEN_DRAIN ?
Disconnect the pin from fan, set the PWM at 50%, 25kHz and measure the pin voltage with multimeter. If it’s stable 1.65V, open drain LEDC approach doesnt work, if you get some lower fluctuating or zero voltage, it works.

Also, try without servo output configuration to be sure esphome doesn’t give them adjacent LEDC channels (which share same frequency). Or add option
channel: 4
to your fan ledc output config.

It worked: I assigned different Channels to Servo and Fans and used the open_drain function. That worked, thank you so much :smiley:

Thanks to @tom_l and @Karosm for the Solution. I will reference this post as the Solution because it has the working code in it, but credits belong to those guys <3

This is the code that worked for future reference:

output:
#Fans

  - platform: ledc
    pin: 
      number: GPIO13
      mode:
        output: true
        open_drain: true

    frequency: 23000 Hz #25000
    id: fanhub_pwm
    channel: 4

#servo
  - platform: ledc
    id: pwm_output
    pin: GPIO26
    frequency: 50 Hz
    channel: 2

Nice you got it working. For my curiosity could you give it a try without open_drain: true
to confirm if the problem was just adjacent LEDC channels or push-pull PWM.

Thats kind of the correct suggestion but, it can be simplified much more. Logic shifters are just a group of transistors which typically come only as small as 4 channel modules commonly. Thats kind of overkill and wasteful to use a logic shifter especially if its 4+ channels and you only need 1 channel.

Instead someone only needs a simple transistor or moafet to achieve the same output. Depending on how much someone does DIY stuff or plans to do, it cab be a good idea to pick up one of the variety packs of transistors, mosfets, resistors, etc so that little problems like this dont completely stop a project in its tracks to wait on components.

I personally use and really like these mostly because of the organizer they come in and each one completely closes so that you cant have accidental mixing between different components.
Component variety kit

Also if your not familiar with these mosfet modules you can buy for very little money, these things are extremely useful to have around IMO and i use them all the time because they can control so much stuff!

They come in single channel modules or 4 channel modules most commonly.
Mosfet module

It all stores away nicely when not being used too.

You don’t even need that if the current is low enough. The ESP32 outputs are capable of operating in open-drain mode.

Ya, that’s true but, transistors are dirt cheap and if im going to risk burning something up from pulling excessive mAh then id rather smoke a .01 cent transistor personaly.

The main reson for my post though was a bit more deceptive though… Muahahaha!! (Evil laugh) im working on it…

Using transistors/mosfets are something that anyone wanting to DIY stuff is going to need to have a good deal of understanding and some practice and is why i was motivating them that direction, plus i dont believe that open-drain is capable of driving something that would require a PNP type transistor is it? Yes, i realize that NPN type control is far more common but… when those dang situations come up and your way rusty, they can be a PITA or thas how it goes for me if i dont keep knowledge circulating up there where my hat holder is.