1
电子说
第1步:您需要的零件
我用来执行此操作的零件
·Genuino UNO
·温度传感器–关键部件
·1KΩ电阻器
·100µF/16V电容器
·PN2222A NPN晶体管–打开风扇的关键部分
·12V DC无刷风扇–关键部分
·1N4007二极管
·AC – DC插入适配器120VAC至12VDC
·液晶显示器
·额外面包板
步骤2:示意图
步骤3:源代码//注释
#include
// library for LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
float temp; // set temp as a whole number
int tempPin = A0; //arduino pin used for temperature sensor
int tempMin = 23; // the temperature to start the buzzer
int tempMax = 26; // temperature full speed
int fan = 6; // the pin where fan is connected
int fanSpeed = 0; // fan speed variable void setup() {
pinMode(fan, OUTPUT); // declaring fan pin 6 as output
pinMode(tempPin, INPUT); // declaring tempPin A0 as input
lcd.begin(16, 2); // LCD intialize
lcd.clear(); // clears lcd
lcd.print(“Fan Speed:”); // prints “Fan Speed” 50-255 PWM
lcd.setCursor(0,1); // Sets cursor for next line
lcd.print(“Temperature:”); // Prints “Temperature” below “Fan Speed”
Serial.begin(9600); // begins the serial monitor
} void loop() {
temp = analogRead(tempPin);
float voltage = (temp/1024)*5.0;
float temperature = (voltage - 0.5) * 100; // formula for degrees celcius
Serial.println(temperature); // prints temp in serial monitor
lcd.setCursor(12,1); // 12 character to the right on the first column
lcd.print(temperature); // prints the number on the 12 character
delay(1000); // delay in between reads for stability
if(temp 《 tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW); // pin 6 output is low
}
if((temperature 》= tempMin) && (temperature 《= tempMax)) //if temperature is higher than the minimmum range
{
fanSpeed = map(temperature, tempMin, tempMax, 50, 255); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
Serial.println(fanSpeed); // prints fan speed in serial monitor
lcd.setCursor(12,0); // sets cursor
lcd.print(fanSpeed); // prints
lcd.print(“ ”); // fixes an error of random number display
}
}
责任编辑:wv
全部0条评论
快来发表一下你的评论吧 !