4-pin PWM FAN set level and start/stop with ESPHome

Hello everyone!
Share a little ESPHome project to control 4-pin PWM FAN.

Goal

  1. Control the fan speed by fan PWM.
  2. Ability to turn off the fan.
  3. Read RPM data from the fan.

Common PWN FAN doesn’t stop on 0% PWM signal and still running on minimum RPM. It is possible to control the fan speed not by fan PWM, but by PWM of DC input. In that case the fan can be stopped, but RPM data from the fan is messed up.
We will start/stop the fan with a MOSFET and set the speed with the FAN PWM input signal.

https://github.com/nordeep/esphome_fan_controller

substitutions:
  device_name: fanmaster
  device_description: FanMaster Fan controller
  friendly_name: fanmaster

esphome:
  name: ${device_name}
  comment: ${device_description}
  platform: ESP8266
  board: nodemcuv2
  on_boot:
    - priority: 200.0
      then:
        - output.set_level:
            id: pwmfan
            level: 100%

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${friendly_name}_AP
    password: !secret wifi_password

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_password

# Enable OTA
ota:
  password: !secret api_password

switch:
  - platform: restart
    name: ${friendly_name} Restart

binary_sensor:
# Reports if this device is Connected or not
  - platform: status
    name: ${friendly_name} Status

sensor:
# Reports the WiFi signal strength
  - platform: wifi_signal
    name: ${friendly_name} Signal
    update_interval: 60s

# Reports RPM by pulse_counter
  - platform: pulse_counter
    pin: D6
    name: ${friendly_name} Fan Speed
    unit_of_measurement: 'RPM'
    filters:
      - multiply: 0.5
      - lambda: |-
          if (x <= 1500) return x;
          else return {};
    count_mode:
      rising_edge: INCREMENT
      falling_edge: DISABLE
    update_interval: 30s

# Reports how long the device has been powered (in minutes)
  - platform: uptime
    name: ${friendly_name} Uptime
    filters:
      - lambda: return x / 60.0;
    unit_of_measurement: minutes

text_sensor:
# Reports the ESPHome Version with compile date
  - platform: version
    name: ${friendly_name} ESPHome Version

# Reports WiFi name
  - platform: wifi_info
    ssid:
      name: ${friendly_name} WiFi

# Reports IP
  - platform: wifi_info
    ip_address:
      name: ${friendly_name} IP

status_led:
  pin: 
    number: D0
    inverted: False

output:
  - platform: gpio
    pin: D5
    id: fan_supply
  - platform: esp8266_pwm
    pin: D7
    frequency: 25000 Hz
    id: pwmfan

fan:
  - platform: speed
    output: pwmfan
    name: ${friendly_name} Fan
    on_turn_on:
      - output.turn_on: fan_supply
      - logger.log: "Power of Fan turned ON"
    on_turn_off:
      - output.turn_off: fan_supply
      - logger.log: "Power of Fan turned OFF"
      - delay: 1s
      - logger.log: "Write 100%% to output pwmfan"
      - output.set_level:
          id: pwmfan
          level: 100%

A little bit trick to stop PWM fan is set 100% level of PWM on output after remove power.

2 Likes

Hey,

great project. I have 2 of 140MM fans with 4 pins (noctua).

I have hassio image on my raspy so i cant really install this guide on my device :

Is this possible to control those 2 fans without the components which are described in git hub?

Thank you

Fist of all you need to determinate Voltage of FANs. As I can see in Raspberry Pi case have used 5V FAN, when in my case 12V FAN.

  1. If you have 12V FANs, you need 12V PSU to power up.
  2. To switch on/off FANs by 5V Raspberry PI output you need Relay or MOSFET like IRF520.
  3. By PWM signal of Raspbery PI output you can control FAN speed. Connect to FAN PWM input.

thanks! exactly what i want to do :wink:

i have 2 noctua fan and i want to add a thermostat with automatic temperature control.