風扇速度由溫度和Arduino控制
出處:維庫電子市場網(wǎng) 發(fā)布于:2024-08-01 17:41:32 | 558 次閱讀

LM35 數(shù)據(jù)表
Arduino草圖
#include <LiquidCrystal.h>
//source: https://www.electroschematics.com/9540/arduino-fan-speed-controlled-temperature/
LiquidCrystal lcd(7,6,5,4,3,2);
int tempPin = A1; // the output pin of LM35
int fan = 11; // the pin where fan is
int led = 8; // led pin
int temp;
int tempMin = 30; // the temperature to start the fan
int tempMax = 70; // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
void setup() {
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
pinMode(tempPin, INPUT);
lcd.begin(16,2);
}
void loop() {
temp = readTemp(); // get the temperature
if(temp < tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
}
if((temp >= tempMin) && (temp <= tempMax)) { // if temperature is higher than minimum temp
fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
fanLCD = map(temp, tempMin, tempMax, 0, 100); // speed of fan to display on LCD
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}
if(temp > tempMax) { // if temp is higher than tempMax
digitalWrite(led, HIGH); // turn on led
} else { // else turn of led
digitalWrite(led, LOW);
}
lcd.print("TEMP: ");
lcd.print(temp); // display the temperature
lcd.print("C ");
lcd.setCursor(0,1); // move cursor to next line
lcd.print("FANS: ");
lcd.print(fanLCD); // display the fan speed
lcd.print("%");
delay(200);
lcd.clear();
}
int readTemp() { // get the temperature and convert it to celsius
temp = analogRead(tempPin);
return temp * 0.48828125; }

我使用LCD屏蔽來顯示風扇的當前溫度和速度,但是您可以使用沒有LCD顯示器的電路。您還需要根據(jù)您使用的風扇類型來選擇晶體管。就我而言,我使用著名的 BD139 晶體管和 9V 電池為風扇和晶體管供電。LM35 溫度傳感器和紅色 LED 由 Arduino 板的 5V 電壓供電。
電路是如何工作的?
正如您在第一行的草圖中看到的,我包含了 LiquidCrystal 庫(標頭),其中包含在將 LCD 連接到 Arduino 板時可以使用的有用功能。然后,我為傳感器、LED 和風扇設(shè)置了引腳。
最重要的部分是使用所需的值設(shè)置變量 tempMin 和 tempMax。tempMin 是風扇開始旋轉(zhuǎn)的溫度,tempMax 是紅色 LED 燈警告您已達到最高溫度時的溫度。例如,如果將 tempMin 設(shè)置為 30,將 tempMax 設(shè)置為 35,則風扇將在 30°C 時開始旋轉(zhuǎn),并在 35°C 時達到其最高速度。
將溫度值存儲在 temp 變量中,然后使用一些 if() 函數(shù)來檢查溫度是否低于 tempMin,如果是,則讓風扇關(guān)閉 (LOW)。下一個 if() 是檢查溫度是否高于 minTemp 并低于 tempMax,如果是,則使用 map() 函數(shù)將溫度值從一個值重新映射到另一個值。在我們的例子中,fanSpeed 在 tempMin 的值為 32,在 tempMax 的值為 255。這些值用于通過 PWM 和 analogWrite() 控制風扇的速度。
風扇LCD 重新映射溫度,允許在 0 到 100% 范圍內(nèi)顯示風扇速度,因此您可以說風扇的速度直接取決于 LM35 的溫度。當溫度達到 tempMax 中設(shè)置的值時,風扇將處于其最大旋轉(zhuǎn)速度,并且 LCD 將顯示 FANS:100%,即使溫度可能會升高到 tempMax 以上。
版權(quán)與免責聲明
凡本網(wǎng)注明“出處:維庫電子市場網(wǎng)”的所有作品,版權(quán)均屬于維庫電子市場網(wǎng),轉(zhuǎn)載請必須注明維庫電子市場網(wǎng),http://www.hbjingang.com,違反者本網(wǎng)將追究相關(guān)法律責任。
本網(wǎng)轉(zhuǎn)載并注明自其它出處的作品,目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點或證實其內(nèi)容的真實性,不承擔此類作品侵權(quán)行為的直接責任及連帶責任。其他媒體、網(wǎng)站或個人從本網(wǎng)轉(zhuǎn)載時,必須保留本網(wǎng)注明的作品出處,并自負版權(quán)等法律責任。
如涉及作品內(nèi)容、版權(quán)等問題,請在作品發(fā)表之日起一周內(nèi)與本網(wǎng)聯(lián)系,否則視為放棄相關(guān)權(quán)利。














