以下是一个基于Arduino的智能温控风扇的代码示例,可以根据设定的温度自动控制风扇开关:

// 定义温度传感器引脚
const int sensorPin = A0;
// 定义风扇引脚
const int fanPin = 9;

// 设置目标温度
const int targetTemp = 25;

void setup() {
  // 初始化串口通信
  Serial.begin(9600);
  // 设置风扇引脚为输出模式
  pinMode(fanPin, OUTPUT);
}

void loop() {
  // 读取温度传感器的值
  int sensorValue = analogRead(sensorPin);

  // 将传感器读数转换为摄氏温度
  float temperature = (sensorValue * 5.0 / 1024.0 - 0.5) * 100;

  // 打印当前温度
  Serial.print('Current temperature: ');
  Serial.print(temperature);
  Serial.println(' °C');

  // 如果当前温度高于目标温度,则打开风扇
  if (temperature > targetTemp) {
    digitalWrite(fanPin, HIGH);
    Serial.println('Fan turned on');
  } else {
    digitalWrite(fanPin, LOW);
    Serial.println('Fan turned off');
  }

  // 延迟500毫秒
  delay(500);
}

这个代码会读取连接到Arduino的温度传感器的值,并将其转换为摄氏温度。然后,根据目标温度决定是否打开风扇。如果当前温度高于目标温度,就会打开风扇;否则,关闭风扇。代码通过串口输出当前温度和风扇状态。

你可以根据实际情况调整目标温度、传感器引脚和风扇引脚。

Arduino 智能温控风扇代码:自动调节温度

原文地址: https://www.cveoy.top/t/topic/pwdF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录