from machine import Pin, I2C, PWM, freq import utime set_temp = 50 #sets fan start temperature sensor_temp = machine.ADC(4) conversion_factor = 3.3 / (65535) fan_speed = 0 fan_pwm = PWM(Pin(18)) #sets output pin fan_pwm.freq(100) #sets pwm frequency fan_pwm.duty_u16(fan_speed) while True: reading = sensor_temp.read_u16() * conversion_factor temperature = 27 - (reading - 0.706)/0.001721 print(temperature) utime.sleep(2) if temperature > set_temp: fan_speed = (temperature - set_temp) * 2000 fan_speed = max(fan_speed,20000) #sets minimum speed to prevent stalling fan_speed = min(fan_speed,64000) fan_pwm.duty_u16(int(fan_speed)) else: fan_speed = 0 fan_pwm.duty_u16(int(fan_speed))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter