Jdi na obsah Jdi na menu
 


instalace pigpio v terminalu

Instalace softwaru:
Pro spuštění ventilátoru jsem nainstaloval knihovnu pigpio . Tuto knihovnu můžete nainstalovat z terminálu následujícím způsobem:

  • Závislosti: sudo apt-get install build-essential
  • Stáhnout pigpio: wget abyz.me.uk/rpi/pigpio/pigpio.zip
  • Dekomprimovat: unzip pigpio.zip && cd PIGPIO
  • Sestavte a nainstalujte: make && sudo make install

Nyní máme program, který použijeme k ovládání ventilátoru pomocí PWM.

Spuštění ventilátoru:
Můžete vytvořit nový bash skript s následujícím obsahem. A nechal to běžet @rebootv kořenovém crontabu. Upravte proměnné na začátku skriptu podle svých představ. Pokud chcete vidět, co se děje, soubor DEBUG=1a informace budou zapsány /tmp/debugNenechal bych to, jakmile potvrdíte, že to všechno funguje, protože by to provedlo zbytečné zápisy na vaši SD kartu. Tento skript předpokládá, že máte nainstalovaný python.





#!/bin/bash

fanSpeed=65 # run the fan at 65% speed; mine was too noisy at 100%. Be careful not to set this too low, or your fan may not spin!
thresholdF=135  # Turn the fan on when we're above this temp
thresholdC=$(/usr/bin/python -c "print ($thresholdF-32)/1.8")
DEBUG=0

function debug {
    [ $DEBUG -eq 1 ] && echo "[$(date +%m%d.%H%M%S)] $0: $@" >> /tmp/debug
}

function set_fan {
    percent=$1  # integer value
    GPIO=18     # GPIO pin number; 18 is hardware PWM
    freq=25000  # This is the frequency of the PWM signal; increase if you hear a whine

    debug "setting PWM on GPIO $GPIO at ${percent}%"
    [ $percent  -gt 0 ] && /usr/local/bin/pigs hp $GPIO $freq 1000000   # spin up the fan before cranking back
    sleep 1
    /usr/local/bin/pigs hp $GPIO $freq $(/usr/bin/python -c "print 10000*$percent")
}

function get_temp {
    echo "$(/opt/vc/bin/vcgencmd measure_temp | grep -oP "\d+.\d")"
}

# returns 1 if we're above our threshold or 0 if we aren't
function is_too_hot {
    echo $(/usr/bin/python -c "print 1 if $thresholdC<$(get_temp) else 0")
}

debug "fan script started"
# start pigpiod daemon if not already running
[ $(pidof pigpiod | wc -l) -eq 0 ] && /usr/local/bin/pigpiod && debug "pigpiod service started"

debug "threshold F: $thresholdF"
debug "threshold C: $thresholdC"

# start a loop to continue checking CPU temp and switching fan as necessary
state=off
while :
do
    while [ $(is_too_hot) -eq 1 ]
    do
        if [ "$state" == "off" ]; then
            debug "turning the fan on: $(get_temp) (CPU temp) > $thresholdC (threshold)"
            set_fan $fanSpeed && state="on" # turn the fan on
        fi
        sleep 300   # run the fan for 5 minutes before checking temp again
    done
    if [ "$state" == "on" ]; then
        debug "turning the fan off: $(get_temp) (CPU temp) <= $thresholdC (threshold)"
        set_fan 0 && state="off"
    fi
    sleep 60    # wait one minute before checking temp again
done

  a original:

 

Using PWM and a NPN transistor
Let me begin by saying I'm not an electrical engineer. If someone who knows more sees a problem with what I present, please do comment!

I found an excellent write-up for doing this with an Arduino here. I too, however, am using a RaspberryPi (RPi3). One concern that I've seen pointed out is that pulling power from GPIO18 to run the fan can have adverse consequences. Instead, what I propose below is pulling the 5v from a 5v RPi pin. Then use PWM and switching to control the passage of the 5v to the fan.

Here's what I used: I happen to live in China, and the total cost, with shipping, for these parts (multiple of each) was less than $2 (USD). Maybe look on Alibaba.

  • 1x 5v fan
  • 1x 330 ohm resistor
  • 1x NPN transistor (2N4401)
  • 1x diode (1N4001)
  • 1x Raspberry Pi

Connect the fan to the Raspberry Pi using the following circuit:
Get ready for some soldering. I was able to solder all of the components together, wrap them around the fan itself, and still put the fan inside my RPi case. The case description says the fan is quiet. That's a lie which is why I run it at 65% speed!

schematic

simulate this circuit – Schematic created using CircuitLab

  • 5v connects to pin 2 or 4
  • ground connects to pin 6
  • PWM connects to pin 12 (GPIO 18)

Read the link to the Arduino instructions above for more detailed in formation of what's going on.

Installing software:
To run the fan, I installed the pigpio library. You can install this library from the terminal as follows:

  • Dependencies: sudo apt-get install build-essential
  • Download pigpio: wget abyz.me.uk/rpi/pigpio/pigpio.zip
  • Decompress: unzip pigpio.zip && cd PIGPIO
  • Build and install: make && sudo make install

Now we have the program we'll use to control the fan via PWM.

Running the fan:
You can create a new bash script with the following content. And set it to run @reboot in root's crontab. Modify the variables at the beginning of the script to your liking. If you want to see what is happening, set DEBUG=1 and information will be written to /tmp/debug. I wouldn't leave this on once you've confirmed that it's all working though as this would perform unnecessary writes to your SD card. This script assumes you have python installed.

Disclaimer: I have been running my RPi3 with this script for 17+ months as of Jan 2019. If these settings or any settings you use burn up your fan or cause harm to your Raspberry Pi, I cannot be held responsible. Use and make changes at your own risk!

!

#!/bin/bash

fanSpeed=65 # run the fan at 65% speed; mine was too noisy at 100%. Be careful not to set this too low, or your fan may not spin!
thresholdF=135  # Turn the fan on when we're above this temp
thresholdC=$(/usr/bin/python -c "print ($thresholdF-32)/1.8")
DEBUG=0

function debug {
    [ $DEBUG -eq 1 ] && echo "[$(date +%m%d.%H%M%S)] $0: $@" >> /tmp/debug
}

function set_fan {
    percent=$1  # integer value
    GPIO=18     # GPIO pin number; 18 is hardware PWM
    freq=25000  # This is the frequency of the PWM signal; increase if you hear a whine

    debug "setting PWM on GPIO $GPIO at ${percent}%"
    [ $percent  -gt 0 ] && /usr/local/bin/pigs hp $GPIO $freq 1000000   # spin up the fan before cranking back
    sleep 1
    /usr/local/bin/pigs hp $GPIO $freq $(/usr/bin/python -c "print 10000*$percent")
}

function get_temp {
    echo "$(/opt/vc/bin/vcgencmd measure_temp | grep -oP "\d+.\d")"
}

# returns 1 if we're above our threshold or 0 if we aren't
function is_too_hot {
    echo $(/usr/bin/python -c "print 1 if $thresholdC<$(get_temp) else 0")
}

debug "fan script started"
# start pigpiod daemon if not already running
[ $(pidof pigpiod | wc -l) -eq 0 ] && /usr/local/bin/pigpiod && debug "pigpiod service started"

debug "threshold F: $thresholdF"
debug "threshold C: $thresholdC"

# start a loop to continue checking CPU temp and switching fan as necessary
state=off
while :
do
    while [ $(is_too_hot) -eq 1 ]
    do
        if [ "$state" == "off" ]; then
            debug "turning the fan on: $(get_temp) (CPU temp) > $thresholdC (threshold)"
            set_fan $fanSpeed && state="on" # turn the fan on
        fi
        sleep 300   # run the fan for 5 minutes before checking temp again
    done
    if [ "$state" == "on" ]; then
        debug "turning the fan off: $(get_temp) (CPU temp) <= $thresholdC (threshold)"
        set_fan 0 && state="off"
    fi
    sleep 60    # wait one minute before checking temp again
done

It's not hard to imagine how this script could be modified to run the fan at different speeds for different temperatures.