fan with pico - Raspberry Pi Forums

PHOTO EMBED

Mon Feb 12 2024 19:36:54 GMT+0000 (Coordinated Universal Time)

Saved by @csmith66

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)) 
        
content_copyCOPY

https://forums.raspberrypi.com/viewtopic.php?t