CodeOhms
Posts: 5
Joined: Sat Jul 07, 2018 8:27 am

This is how to control fan based on temperature

Sat Jul 07, 2018 11:08 am

Hi everyone,
I've recently created a program for my pi to control multiple fans. Just thought I'd share it :) . https://github.com/CodeOhms/RPi_Fan_Controller


Features
  • Configurable
  • PWM - varying speeds
    :arrow: Works with basic two pin fans. Don't have a proper pwm fan so I can't test for that
  • Control multiple fans, each assigned to a group
    :arrow: Each fan group is associated with a temperature sensor. Support currently only exists for the internal sensor - if enough want it I can add support for external sensors
    :arrow: Each group is assigned a speed from 0 to 100
  • Runs on start up
Please share this with your friends.

User avatar
Z80 Refugee
Posts: 359
Joined: Sun Feb 09, 2014 1:53 pm

Re: This is how to control fan based on temperature

Sun Jul 08, 2018 8:11 am

Not being a programmer, it is not immediately obvious to me how FanController.conf defines what pin(s) are controlling your fans. Does the default file define one fan on pin 18 by any chance? If so, that's just a guess and the comments could be a lot more explicit.

I think you should also include information (or links to information) on how to connect a fan - novices may well think you can just wire one to an RPi pin.
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

CodeOhms
Posts: 5
Joined: Sat Jul 07, 2018 8:27 am

Re: This is how to control fan based on temperature

Thu Jul 12, 2018 5:51 am

Hey mate,
sorry for the late reply - busy with work, the usual.

I've added some more information in the configuration file. This is, essentially, what I wrote:
numbers between the inner square brackets [ ] are the gpio pins to which an individual fan is connected. Each set of brackets is assigned a number starting from zero upwards. This number is the group ID number. Here is the excerpt which I included:
# Fan group ID derived from index, and fanID is associated output pin
fanInfo = [ [18] ]
# Above translates to: group 0 => fan connected on pin 18
# Example with multiple fans and groups
# [ [18, 16], [15] ] -> group 0 => fans connected on pins 18 and 16
# -> group 1 => fan connected on pin 15
For reference, here is a picture of the RPi gpio pin layout https://www.element14.com/community/ser ... 3_gpio.png.

CodeOhms
Posts: 5
Joined: Sat Jul 07, 2018 8:27 am

Re: This is how to control fan based on temperature

Thu Jul 12, 2018 6:33 am

This is how each fan should be wired, assuming a 5v motor.
Please note I am not responsible for any damage incurred by undertaking this.

Here is a discussion goes into depth about wiring a fan for you RPi: viewtopic.php?f=41&t=194621.
Attachments
RPi_fan_circuit_schem.png
RPi_fan_circuit_schem.png (81.18 KiB) Viewed 6025 times

Ayaya
Posts: 1
Joined: Thu May 21, 2020 8:49 am

Re: This is how to control fan based on temperature

Thu May 21, 2020 9:14 am

It could be neater but i'm lazy.
I'm using a raspberry pi 4 b w/ 4 GB ram,
I have an Ice Tower Cooler mounted with an intake that's always on and an exhaust that turns on when the threshold is met.
this code was originally for bench marking, I was lazy and added the fan control to it.
I used Thonny for this.

I am just starting and this would have helped, someone else might need it.

Code: Select all

import os
import time
import RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

def measure_temp():
    temp = os.popen("vcgencmd measure_temp").readline()
    return (temp.replace("temp=",""))


while True:
    imp = (measure_temp()[0:2])
    print(measure_temp() + " Celsius,")
    print(str((int(imp)*9/5)+32))
    print(" Fahrenheit,")
    
    # this 34 here is what i have it set at to turn the fans on
    #it's in celsius
    
    if int(imp) >= 34:
        GPIO.output(18, False)
        print ("Fan On")
    else:
        GPIO.output(18, True)
        print ("Fan Off")
    time.sleep(1)
    print()
    
Like the Pi I'm cheap, lazy, and replaceable.
I don't need a quote, I don't need a bio, I'm on the internet and on the internet it doesn't matter who I am.

alphanumeric
Posts: 2803
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: This is how to control fan based on temperature

Sat May 30, 2020 8:02 pm

There is a dtovery for fan control, Just a FYI post. I use the following with my Pimoroni Fan Shim. It's fan control Pin is GPIO(BCM) 18.

Code: Select all

dtoverlay=gpio-fan,gpiopin=18,temp=55000  
It turns on at 55c and off again at 45c.

Return to “Other projects”