0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

樹莓派控制風(fēng)扇的幾種方式

CHANBAEK ? 來源:頭條號科G棧 ? 作者:頭條號科G棧 ? 2023-03-22 15:22 ? 次閱讀

為了提高樹莓派的散熱效率,可以加一個(gè)小風(fēng)扇,網(wǎng)上那種5v-dc 25x25的就可以。 我使用一個(gè)s9013三極管控制風(fēng)扇的啟停,電路原理圖如下所示:

基極電阻的大小可以根據(jù)實(shí)際情況調(diào)整,如果電阻過大會(huì)導(dǎo)致基極電流太小,進(jìn)而CE電流太?。?如果電阻過小,可能會(huì)燒壞三極管。 最佳狀態(tài)是三極管導(dǎo)通后ce電流剛好是風(fēng)扇的額定電流,可以根據(jù)風(fēng)扇額定電流和三極管放大倍數(shù)大概算出基極的電流,然后根據(jù)gpio電壓算出需要的電阻。

控制風(fēng)扇可以有多種方式,c、python、shell腳本都可以,下面是我實(shí)現(xiàn)的代碼。

1、使用linux文件IO函數(shù),實(shí)現(xiàn)開關(guān)風(fēng)扇操作,這是最直接簡單的方式,不需要安裝其他東西,就是要頻繁開關(guān)文件。

#include
#include
#include
#include 
#include 
int get_temp()
{
    char  temp[8];
   int fd = open("/sys/class/thermal/thermal_zone0/temp",O_RDONLY);
   read(fd,&temp,5);
   close(fd);
   return atoi(temp);
}

uint8_t fan_on = 0;
void main()
{
    char* buff = "18";
	int fd = access("/sys/class/gpio/gpio18/value",F_OK);
	if(fd<0)
	{
		fd = open("/sys/class/gpio/export",O_WRONLY);
        write(fd,buff,1);
        close(fd);
	}
    fd = open("/sys/class/gpio/gpio18/direction",O_WRONLY);
    buff = "out";
    write(fd,buff,3);
    close(fd);
    while(1)
    {
        int temp = get_temp();
       // printf("temp:%f\\n",temp/1000.0);
        temp /=1000; 
        if((fan_on == 1)&&(temp <= 40))
        {
            
            fd = open("/sys/class/gpio/gpio18/value",O_WRONLY);
            buff = "0";
            write(fd,buff,1);
            close(fd);
            printf("fan off\\n");
            fan_on = 0;
        }
        else if((fan_on == 0)&&(temp >=45))
        {
            fd = open("/sys/class/gpio/gpio18/value",O_WRONLY);
            buff = "1";
            write(fd,buff,1);
            close(fd);
            printf("fan on\\n");
            fan_on = 1;
        }
        sleep(1);
    }   
}

2、使用bcm2835庫,開關(guān)控制風(fēng)扇,需要安裝bcm2835庫,編譯需要加上-lbcm2835選項(xiàng),另外需要sudo管理員權(quán)限運(yùn)行。

#include 
#include
#include
#include 
#include 
#define PIN RPI_GPIO_P1_12

int get_temp()
{
    char  temp[8];
   int fd = open("/sys/class/thermal/thermal_zone0/temp",O_RDONLY);
   read(fd,&temp,5);
   close(fd);
   return atoi(temp);
}

int main(int argc,char **argv)
{
    if (!bcm2835_init())
        return 1;
 
  // 輸出方式
  bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
 
  while (1)
  {
        int temp = get_temp();
       // printf("temp:%f\\n",temp/1000.0);
        temp /=1000; 
     if((fan_on == 1)&&(temp <= 40))
    {
        bcm2835_gpio_write(PIN, HIGH);
        fan_on = 0;
    }
    else if((fan_on == 0)&&(temp >=45))
    {
        bcm2835_gpio_write(PIN, LOW);
        fan_on = 1;
    }
    bcm2835_delay(1000);
  }
  bcm2835_close();
  return 0;
}

3、使用bcm2835庫,pwm方式控制,可以更精細(xì)的控制轉(zhuǎn)速,不過pwm的頻率不宜太高,否則高頻噪音很大,還不如讓其全速運(yùn)行。 另外pwm占空比過低風(fēng)扇也不轉(zhuǎn),所以我直接讓他從50%開始運(yùn)轉(zhuǎn),并且在停止和運(yùn)轉(zhuǎn)有一個(gè)死區(qū),避免在零界點(diǎn)頻繁啟停。 具體大家可以根據(jù)自己的需求調(diào)整。

#include 
#include 
#include 
#include 
#include 
#include 

// PWM output on RPi Plug P1 pin 12 (which is GPIO pin 18)
// in alt fun 5.
// Note that this is the _only_ PWM pin available on the RPi IO headers
#define PIN RPI_GPIO_P1_12
// and it is controlled by PWM channel 0
#define PWM_CHANNEL 0
// This controls the max range of the PWM signal
#define RANGE 200

int get_temp()
{
    char  temp[8];
   int fd = open("/sys/class/thermal/thermal_zone0/temp",O_RDONLY);
   read(fd,&temp,5);
   close(fd);
   return atoi(temp);
}

int temp;
int main(int argc, char **argv)
{
    if (!bcm2835_init())
	return 1;

    // Set the output pin to Alt Fun 5, to allow PWM channel 0 to be output there
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_ALT5);

    bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_2048);
    bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1);
    bcm2835_pwm_set_range(PWM_CHANNEL, RANGE);

    while (1)
    {
        temp= get_temp()/100;
        // printf("temp:%f\\n",temp/1000.0);
        if((temp <= 400))
        {
	        bcm2835_pwm_set_data(PWM_CHANNEL, 0);
        }
        else if(temp > 500)
        {
            bcm2835_pwm_set_data(PWM_CHANNEL, RANGE);
        }
        else if(temp > 450)
        { 
            bcm2835_pwm_set_data(PWM_CHANNEL, (temp-450)*2+100);
        }
        //printf("temp:%d\\n",temp);
        sleep(1);
    }
    bcm2835_close();
    return 0;
}

4、python開關(guān)控制

import RPi.GPIO as gpio
from time import sleep

Temper_HI = 47  # 風(fēng)扇啟動(dòng)溫度
Temper_LO = 40  # 風(fēng)扇關(guān)閉溫度
gpio_pin = 12
# 初始化GPIO針腳控制
gpio.setmode(gpio.BOARD)
gpio.setup(gpio_pin, gpio.OUT)

while True:
    with open("/sys/class/thermal/thermal_zone0/temp", "r") as f: 
        temper = int(f.read()) // 100 / 10  # 計(jì)算溫度,保留一位小數(shù)
    # print(temper)
    if temper > Temper_HI:  
        gpio.output(gpio_pin, gpio.HIGH)  

    if temper < Temper_LO:
        gpio.output(gpio_pin, gpio.LOW)  
    sleep(1)

5、python pwm控制

import RPi.GPIO as gpio
import time 
fan_gpio_pin = 12
temp_max = 500
temp_on = 450
temp_min = 400
def get_cpu_temp():
    with open('/sys/class/thermal/thermal_zone0/temp') as f:
        cpu_temp = int(f.read())
    return cpu_temp/100 

def main():
    gpio.setwarnings(False)
    gpio.setmode(gpio.BOARD)
    gpio.setup(fan_gpio_pin, gpio.OUT, initial=gpio.LOW)
    pwm = gpio.PWM(fan_gpio_pin, 50)
    hasFanStarted = False
    while True:
        temp = get_cpu_temp()
        # print(temp)
        if temp < temp_min:
            if hasFanStarted:      
                pwm.start(0)         
                hasFanStarted = False
        elif temp >= temp_on and temp <= temp_max:
            pwm.start(temp-temp_on+50)
            hasFanStarted = True

        elif temp > temp_max:
            pwm.start(100)
            hasFanStarted = True
        time.sleep(1)

if __name__ == '__main__':
    main()

6、shell腳本開關(guān)控制(shell腳本語法有點(diǎn)麻煩,不熟悉不建議使用)

#! /bin/bash
fan_is_on=0
if [ ! -e "/sys/class/gpio/gpio18/value" ]
    then
    echo "export gpio 18"
    echo "18" > "/sys/class/gpio/export"
fi

echo "out" > "/sys/class/gpio/gpio18/direction"

while ((1))
do 
TEMP=$(cat /sys/class/thermal/thermal_zone0/temp)
echo $TEMP
if [[ $fan_is_on -eq 1 ]] && [[ $TEMP -lt 40000 ]]
    then
    fan_is_on=0
    echo "fan off"
    echo "0" > "/sys/class/gpio/gpio18/value"
elif [[ $fan_is_on -eq 0 ]] && [[ $TEMP -gt 45000 ]]
    then
    fan_is_on=1
    echo "fan on"
    echo "1" > "/sys/class/gpio/gpio18/value"
fi
sleep 1s
done

大家覺得哪種方式更好呢? 如果是你會(huì)選擇哪個(gè)?

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • 三極管
    +關(guān)注

    關(guān)注

    142

    文章

    3574

    瀏覽量

    121342
  • 電流
    +關(guān)注

    關(guān)注

    40

    文章

    6683

    瀏覽量

    131491
  • Linux
    +關(guān)注

    關(guān)注

    87

    文章

    11171

    瀏覽量

    208474
  • 風(fēng)扇
    +關(guān)注

    關(guān)注

    4

    文章

    408

    瀏覽量

    37369
  • 樹莓派
    +關(guān)注

    關(guān)注

    116

    文章

    1683

    瀏覽量

    105396
收藏 人收藏

    評論

    相關(guān)推薦

    樹莓自動(dòng)散熱風(fēng)扇

    控制一個(gè)迷你5V風(fēng)扇,不需要用到電路實(shí)驗(yàn)板和晶體管之類的。只需要一些電纜和通道繼電器。這里推薦2通道繼電器。一個(gè)腳本執(zhí)行每小時(shí)檢查樹莓溫度和開/關(guān)
    發(fā)表于 01-06 15:20

    控制樹莓板載 LED 的幾種方式

    所有的樹莓型號或多或少都自帶了一些]
    發(fā)表于 05-23 07:00

    請問樹莓管腳有哪幾種編碼方式?

    樹莓40Pin引腳對照表樹莓管腳有三種編碼方式
    發(fā)表于 11-10 07:11

    【.NET 與樹莓】小風(fēng)扇模塊 精選資料分享

    想,這種小風(fēng)扇直接上電源就行了,還用得著單片機(jī)和樹莓?確實(shí),不過,搭配 L9110 驅(qū)動(dòng)芯片,再用單片機(jī)發(fā)出...
    發(fā)表于 09-08 07:33

    樹莓4B Ubuntu 21.04自動(dòng)溫控開關(guān)風(fēng)扇

    樹莓4B Ubuntu 21.04 自動(dòng)溫控開關(guān)風(fēng)扇以及RPi.GPIO避坑指南本人對樹莓有一些了解,雖然學(xué)過模電數(shù)電,但也只是學(xué)過,過
    發(fā)表于 09-08 07:38

    樹莓是怎樣控制風(fēng)扇散熱的

    樹莓9層外殼自帶一個(gè)散熱風(fēng)扇風(fēng)扇是2針的,不能控制,插上電源后就一直全速運(yùn)轉(zhuǎn),晚上噪音很大。而且,一直運(yùn)行,
    發(fā)表于 09-08 07:47

    樹莓裝機(jī)教程

    樹莓裝機(jī)教程樹莓裝機(jī)教程樹莓裝機(jī)教程樹莓
    發(fā)表于 11-25 10:14 ?52次下載

    樹莓風(fēng)扇安裝

    RaspberryPi(中文名為“樹莓”,簡寫為RPi,(或者RasPi/RPI)是為學(xué)習(xí)計(jì)算機(jī)編程教育而設(shè)計(jì)),只有信用卡大小的微型電腦,其系統(tǒng)基于Linux。隨著Windows10IoT的發(fā)布,我們也將可以用上運(yùn)行Windows的
    的頭像 發(fā)表于 03-25 17:42 ?3.8w次閱讀

    樹莓是什么樹莓的簡單介紹

    要想玩轉(zhuǎn)樹莓,首先得知道樹莓是什么。在本節(jié)中,作者將帶領(lǐng)大家揭開樹莓的神秘面紗,了解
    發(fā)表于 05-15 18:09 ?30次下載
    <b class='flag-5'>樹莓</b><b class='flag-5'>派</b>是什么<b class='flag-5'>樹莓</b><b class='flag-5'>派</b>的簡單介紹

    使用樹莓設(shè)計(jì)智能小車教程之樹莓手機(jī)PC控制小車的實(shí)驗(yàn)免費(fèi)下載

    本文檔的主要內(nèi)容詳細(xì)介紹的是使用樹莓設(shè)計(jì)智能小車教程之樹莓手機(jī)PC控制小車的實(shí)驗(yàn)免費(fèi)下載。
    發(fā)表于 06-24 08:00 ?22次下載
    使用<b class='flag-5'>樹莓</b><b class='flag-5'>派</b>設(shè)計(jì)智能小車教程之<b class='flag-5'>樹莓</b><b class='flag-5'>派</b>手機(jī)PC<b class='flag-5'>控制</b>小車的實(shí)驗(yàn)免費(fèi)下載

    使用樹莓控制的汽車

    電子發(fā)燒友網(wǎng)站提供《使用樹莓控制的汽車.zip》資料免費(fèi)下載
    發(fā)表于 11-23 11:20 ?1次下載
    使用<b class='flag-5'>樹莓</b><b class='flag-5'>派</b><b class='flag-5'>控制</b>的汽車

    樹莓控制步進(jìn)電機(jī)

    樹莓控制步進(jìn)電機(jī) 前言 設(shè)備 連接 源碼 前言 測試步進(jìn)電機(jī) 設(shè)備 名稱 型號 樹莓 3B+ 步進(jìn)電機(jī) 28BYJ-48-5V 步進(jìn)電機(jī)
    發(fā)表于 03-21 11:39 ?0次下載
    <b class='flag-5'>樹莓</b><b class='flag-5'>派</b><b class='flag-5'>控制</b>步進(jìn)電機(jī)

    樹莓控制PWM控制電機(jī)轉(zhuǎn)速

    樹莓控制PWM控制電機(jī)轉(zhuǎn)速 一、硬件 樹莓 12V直流電機(jī) L298N電機(jī)驅(qū)動(dòng)器 220V轉(zhuǎn)
    發(fā)表于 03-31 10:59 ?2次下載
    <b class='flag-5'>樹莓</b><b class='flag-5'>派</b><b class='flag-5'>控制</b>PWM<b class='flag-5'>控制</b>電機(jī)轉(zhuǎn)速

    樹莓控制繼電器

    樹莓控制繼電器命令行輸入gpio readall查看樹莓io口屬性可以看到被分為左右兩側(cè),左側(cè)為樹莓
    發(fā)表于 04-21 11:50 ?0次下載
    <b class='flag-5'>樹莓</b><b class='flag-5'>派</b><b class='flag-5'>控制</b>繼電器

    樹莓4b風(fēng)扇插哪個(gè)引腳

    樹莓4B驅(qū)動(dòng)風(fēng)扇時(shí),風(fēng)扇可以插接的引腳主要取決于風(fēng)扇的類型和所使用的控制方法。以下是一些常見的
    的頭像 發(fā)表于 08-30 16:53 ?794次閱讀