Skip to content

Instantly share code, notes, and snippets.

@unwiredtech
Created September 25, 2024 10:52
Show Gist options
  • Save unwiredtech/92889615d2a41080891b4bf484082f98 to your computer and use it in GitHub Desktop.
Save unwiredtech/92889615d2a41080891b4bf484082f98 to your computer and use it in GitHub Desktop.
ESPHome - Temperature controlled PWM
esphome:
name: esp-pwm-temp-controller
friendly_name: ESP-PWM-Temp-Controller
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "WaHn1a6+xOHHPtZkiT7YsKoU1hX2RjwrmnVFxtMxZ8s="
ota:
- platform: esphome
password: "ace7afe1fba2586baf96a757cbe0aef2"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: 192.168.0.181 # Replace with an IP in your network's range
gateway: 192.168.0.1 # Replace with your router's IP address
subnet: 255.255.255.0 # Typically this is the subnet for home networks
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp-Pwm-Temp-Controller"
password: "XJnQbyxoyxxJ"
captive_portal:
web_server:
port: 80
# OneWire Bus Setup for DS18B20
one_wire:
- platform: gpio
pin: GPIO13 # The GPIO pin connected to the DS18B20 data pin
sensor:
- platform: dallas_temp
address: 0x3c00000086949528 # Replace with your DS18B20 address or use 0x0000000000000000 initially
name: "Room Temperature"
id: room_temp
- platform: pulse_counter
pin: GPIO12 # The GPIO pin connected to the tachometer pin of the fan
id: fan_rpm
unit_of_measurement: "RPM"
name: "Fan RPM"
update_interval: 5s
accuracy_decimals: 0
filters:
- multiply: 0.5 # PC fan tachometers usually output two pulses per revolution
# PWM Output to Control the Fan Speed
output:
- platform: ledc
pin: GPIO14 # Connect this GPIO to the PWM pin of the fan
id: pwm_fan
frequency: 25000 Hz # 25kHz is typical for PC fans, but check your fan's datasheet
# Fan Control Component
fan:
- platform: speed
output: pwm_fan
name: "PC Fan Speed"
id: pc_fan
on_turn_on:
then:
- output.set_level:
id: pwm_fan
level: 50% # Set to 50% speed when the fan turns on initially
# Temperature-Based Fan Control
interval:
- interval: 10s
then:
- lambda: |-
float temp = id(room_temp).state;
ESP_LOGD("custom", "Current Temperature: %.2f°C", temp);
if (!id(pc_fan).state) {
id(pc_fan).turn_on();
ESP_LOGD("custom", "Fan was OFF, turning ON now.");
}
if (temp > 45) {
id(pc_fan).speed = 1.0; // 100% speed
ESP_LOGD("custom", "Fan Speed set to 100%%");
} else if (temp > 35) {
id(pc_fan).speed = 0.75; // 75% speed
ESP_LOGD("custom", "Fan Speed set to 75%%");
} else {
id(pc_fan).speed = 0.5; // 50% speed
ESP_LOGD("custom", "Fan Speed set to 50%%");
}
ESP_LOGD("custom", "Current Fan Speed: %.0f%%", id(pc_fan).speed * 100);
ESP_LOGD("custom", "Current Fan RPM: %.0f", id(fan_rpm).state);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment