Arduino与Proteus仿真实例-温控风扇仿真
温控风扇仿真
1、仿真应用介绍
在实例将使用 Arduino 构建一个温控风扇。 此实例,模拟够根据室温调节我们家或办公室的风扇速度,并在 16x2 LCD 显示屏上显示温度和风扇速度的变化。因此,在实例中将使用 Arduino UNO 板、LCD、DHT11 传感器模块和使用 PWM 控制的直流风扇。
关于LCD1602的驱动,请参考前面文章:
关于DHT11驱动,请参考前面文章:
关于PWM,请参考前面文章:
关于直流电机驱动,请参考前面文章:
2、仿真原理电路

2、仿真应用代码实现
1)导入依赖头文件及相关定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <Adafruit\_Sensor.h> #include <DHT.h> #include <DHT\_U.h> #include <Wire.h> #include <LiquidCrystal\_I2C.h>
#include <L298N.h>
const unsigned int IN1 = 2; // 输入引脚1 const unsigned int IN2 = 3; // 输入引脚2 const unsigned int EN1 = 4; // 使能引脚 L298N motor1(EN1, IN1, IN2); // L298N对象
LiquidCrystal_I2C lcd(0x27,16,2); // PCF8574的通讯地址为0x27
#define DHTTYPE DHT11 // DHT11类型 #define DHTPIN 2 // DHT11引脚 DHT_Unified dht(DHTPIN, DHTTYPE); uint32\_t delayMS;
byte degree[8] = { 0b00011, 0b00011, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 };
|
2)设备初始化
在setup函数中实现串口初始化,LCD初始化,DHT11初始化,LN298N初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| void setup(){ // 串口初始化 Serial.begin(9600); // LCD初始化 lcd.init(); lcd.backlight(); lcd.createChar(1, degree); lcd.clear(); lcd.print(" Fan Speed "); lcd.setCursor(0,1); lcd.print(" Controlling "); lcd.clear(); lcd.print("Circuit Digest "); // DHT11初始化 dht.begin(); sensor\_t sensor; delayMS = sensor.min_delay / 1000; // L298N停止 motor1.stop(); }
|
在loop函数中,实现温度采集,电机转速控制。
DHT温度采集如下:
1 2 3 4
| sensors\_event\_t event; dht.temperature().getEvent(&event); int temp= event.temperature;
|
LCD显示当前温度:
1 2 3 4 5 6 7
| lcd.setCursor(0,0); lcd.print("Temperature:"); lcd.print(temp); lcd.write(1); lcd.print("C"); lcd.setCursor(0,1);
|
根据温度控制电机转速:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| if(temp <26 ){ motor1.stop(); // 电机停止 lcd.print("Fan OFF "); printSomeInfo(); delay(100); }else if(temp==26){ motor1.setSpeed(51); // 设置电机速度 motor1.forward(); // 电机向前转动 lcd.print("Fan Speed: 20% "); printSomeInfo(); delay(100); }else if(temp==27){ motor1.setSpeed(102); motor1.forward(); lcd.print("Fan Speed: 40% "); printSomeInfo(); delay(100); }else if(temp==28){ motor1.setSpeed(153); motor1.forward(); lcd.print("Fan Speed: 60% "); printSomeInfo(); delay(100); }else if(temp==29){ lcd.print("Fan Speed: 80% "); motor1.setSpeed(204); motor1.forward(); printSomeInfo(); delay(100); }else if(temp>29){ lcd.print("Fan Speed: 100% "); motor1.setSpeed(255); motor1.forward(); printSomeInfo(); delay(100); } delay(delayMS);
|
电机信息打印:
1 2 3 4 5 6 7 8
| void printSomeInfo() { Serial.print("Motor is moving = "); Serial.print(motor1.isMoving()); Serial.print(" at speed = "); Serial.println(motor1.getSpeed()); }
|
3、仿真结果

文章来源: https://iotsmart.blog.csdn.net/article/details/127829399
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!