Dear community,
A am building a little house for my new 3D-Printer. The case needs to have a fan - obviously.
I thought of using a D1mini, DTH-11, a relay and a ARCTIC P14 PWM PST-fan. (5V power supply and 12V power supply)
Well, it was all working fine, until i tried to read the fan speed from the fan itself. At first, it was all fine - and now, the d1mini is dead. I am not sure, if it is due to my last update, wrong code, or wrong wiring. Before I try it again with a new d1mini, I would like to check with you guys, if you can point at my mistake. Any help is very welcome.
Wiring: (sorry, did not find a free tool to do some nice schematics)
red wire is load, black is ground
white is sending pwm to the fan
green should be reading pwm from the fan
As the fan runs on 12V and at least ground and the green wire are mixing voltages: was this the problem?
Code:
# Variablen
substitutions:
device_name: "3d-drucker-lufter"
friendly_name: "3D Drucker"
device_description: "3D Drucker Steuerung der Geschwindigkeit des GehĂ€uselĂŒfters"
temperature_threshold_low: "22" # At what temperature, in celcius, should the fan turn on to its minimum speed
temperature_threshold_high: "30" # At what temperature, in celcius, should the fan turn on to its maximum speed
minimum_fan_speed: "7" # What is the minimum fan speed, as a percentage
# ESPHome Core Configuration
esphome:
name: '${device_name}'
friendly_name: '${friendly_name}'
comment: '${device_description}'
esp8266:
board: d1_mini
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: ""
ota:
password: ""
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "3D-Drucker-Lufter"
password: ""
captive_portal:
# enable web interface on device
web_server:
port: 80
text_sensor:
# Send IP Address
- platform: wifi_info
ip_address:
name: $friendly_name IP Address
# Send Uptime in raw seconds
- platform: template
name: $friendly_name Uptime
id: uptime_human
icon: mdi:clock-start
sensor:
- platform: dht
pin: D2
temperature:
name: "Temperatur"
id: temperature_sensor
on_value:
then:
- script.execute: set_fan_state
humidity:
name: "Feuchtigkeit"
id: humidity_sensor
update_interval: 30s
# Sensor Taupunkt berechnen
# - platform: template
# name: "Taupunkt"
# lambda: |-
# return (243.5*(log(id(humidity_sensor).state/100)+((17.67*id(temperature_sensor).state)/
# (243.5+id(temperature_sensor).state)))/(17.67-log(id(humidity_sensor).state/100)-
# ((17.67*id(temperature_sensor).state)/(243.5+id(temperature_sensor).state))));
# unit_of_measurement: °C
# update_interval: 120s
# icon: 'mdi:thermometer-water'
# Fan Speed
- platform: pulse_counter
pin:
number: D6
mode:
input: true
pullup: true
name: PWM Fan RPM
id: fan_pulse
unit_of_measurement: 'RPM'
filters:
- multiply: 0.5
count_mode:
rising_edge: INCREMENT
falling_edge: DISABLE
update_interval: 3s
# PWM output for the fan speed control
output:
- platform: esp8266_pwm
pin: D0
frequency: 25000 Hz
id: pwm_output
# Hidden switch object to control the relay
switch:
- platform: gpio
name: "fan_relay"
id: fan_relay
pin: D3
internal: true
# The actual fan entity presented to Home Assistant
fan:
- platform: speed
output: pwm_output
name: '${friendly_name} LĂŒfter'
id: "the_fan"
on_turn_on:
- switch.turn_on: fan_relay
on_turn_off:
- switch.turn_off: fan_relay
# Sets the speed of the fan based on a linear calculation
# between the high and low temperature thresholds and
# the minimum specified fan speed
script:
- id: set_fan_state
then:
- if:
condition:
lambda: |-
return id(temperature_sensor).state < id(${temperature_threshold_low});
then:
- fan.turn_off: the_fan
else:
- fan.turn_on:
id: the_fan
speed: !lambda |-
if (id(temperature_sensor).state >= id(${temperature_threshold_high})) {
// Over upper threshold, fan speed at maximum
ESP_LOGD("Fan speed calc", "Temperature is above or equal to upper threshold so setting to max");
return 100;
}
else {
float calc_speed = ((100-id(${minimum_fan_speed})) / (id(${temperature_threshold_high})-id(${temperature_threshold_low})))*(id(temperature_sensor).state-id(${temperature_threshold_low}))+id(${minimum_fan_speed});
ESP_LOGD("Fan speed calc", "calculated speed = %f", calc_speed);
return calc_speed;
}
The whole thing is mostly based on this video.