问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

基于Arduino UNO的智能温控系统设计与实现

创作时间:
作者:
@小白创作中心

基于Arduino UNO的智能温控系统设计与实现

引用
CSDN
1.
https://blog.csdn.net/mftang/article/details/136140915

本文将详细介绍如何使用Arduino UNO设计一个智能温控系统。该系统能够实时监测环境温度,并根据设定的门限值自动控制风扇的启停。通过本文,你将学习到硬件连接、软件编程以及系统测试的完整流程。

概述

本文介绍如何使用Arduino UNO作为主控制板,设计一个智能温控系统,其实现功能如下:当环境温度达到一定的门限值时,开始风扇,当环境温度低于该门限值则关闭风扇。系统使用DS18B20采集环境温度,L298N驱动电机,OLED显示当前环境温度。软件设计上使用Arduino自带的定时器中断功能,用于控制时间间隔。还使用了PWM技术,以控制电机的转速。

硬件结构

整体硬件介绍

  1. Arduino UNO: 主控板卡
  2. 控制L298N:用于控制电机系统
  3. 控制OLED模块:用于显示当前温度数据
  4. 控制DS18B20:获取环境温度数据
  5. 直流电机:驱动扇叶

硬件连接结构

模块引脚与Arduino UNO主板之间关系:

Arduino UNO IO
应用模块IO
注释
PIN-2
DS18B20 DQ
PIN-5
L298N in-1
用于电机控制
PIN-6
L298N in-2
用于电机控制
SCL
OLED-scl
SDA
OLED-sda

软件设计

软件功能介绍

软件主要实现功能如下:

  1. 控制DS18B20,读取该传感器采集到的温度值
  2. 在OLED显示温度数据
  3. 通过串口打印调试信息
  4. 根据门限值,控制电机转速(PWM)

关于Arduino的一些知识点

定时器

在Arduino中使用定时器,必须要包含该头文件 <MsTimer2.h>,然后调用如下函数启动定时器,并且还要实现一个中断回调函数。

void startTime()
{
   // 中断设置函数,每 500ms 进入一次中断
    MsTimer2::set(500, timer_irq);
    //开始计时
    MsTimer2::start(); 
}
//回调函数 
void timer_irq()
{
}  

PWM

在Arduino UNO板卡中使用PWM功能,其能使用的引脚为pin( 3, 5, 6, 9, 10, 11),使用方法如下:

  1. 配置端口为模拟引脚
  2. 使用analogWrite( pin, cycle )函数来配置占空比参数含义如下:
  • pin - 引脚号;
  • cycle - 占空比(范围: 0 ~ 255 )

一个使用案例:

//for motor port 
const int output1 = 5; 
const int output2 = 6; 
// 初始化IO
void setup()
{
   pinMode(output1, OUTPUT); 
   pinMode(output2, OUTPUT); 
}
// 配置占空比
void pwmCycle()
{
   analogWrite(output1, 150);  
   analogWrite(output2, 0);  
}  

代码实现

详细代码

/*
Copyright  2024-2029. All rights reserved.
文件名     : motorCtrl
作者       : tangmingfei2013@126.com
![](https://wy-static.wenxiaobai.com/chat-rag-image/2464703061417598044)
版本       : V1.0
描述       : 自动温控系统
其他       : 无
日志       : 初版V1.0 2024/2/15  
*/
#include <MsTimer2.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define ONE_WIRE_BUS 2
              
// for ds18b20 port 
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
//for motor port 
const int output1 = 5; 
const int output2 = 6; 
// for SSD1306
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
unsigned int cnt = 0;
int pwmcycle = 0;
void timer_irq();
void setup() {
    Serial.begin(9600);
  // put your setup code here, to run once:
    pinMode(output1, OUTPUT); 
    pinMode(output2, OUTPUT); 
    analogWrite(output1, 0);  
    analogWrite(output2, 0);  
    // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
    display.display();
    // Show the display buffer on the hardware.
    // NOTE: You _must_ call display after making any drawing commands
    // to make them visible on the display hardware!                                                                                                                                            
    display.clearDisplay();
    // 中断设置函数,每 500ms 进入一次中断
    MsTimer2::set(500, timer_irq);
    //开始计时
    MsTimer2::start(); 
}
void loop() {
    int tempPwmCycle = 0;
    // put your main code here, to run repeatedly:
    if( cnt%2 == 0 )
    {
      sensors.requestTemperatures(); // 发送命令获取温度
      if( cnt%3 == 0 )
      {
        display.clearDisplay();
        Serial.print("Temperature for the device 1 (index 0) is: ");
        Serial.println(sensors.getTempCByIndex(0)); 
        display.setTextSize(1);      
        display.setCursor(0,0);                  // Start at top-left corner
        display.println(F("Current temp: "));
        display.setTextSize(2);                 // Normal 1:1 pixel scale
        display.setTextColor(SSD1306_WHITE);    // Draw white text
        display.setCursor(25,15);                // Start at top-left corner
        display.println(sensors.getTempCByIndex(0));
        display.display();
      }
    }
    if( sensors.getTempCByIndex(0) >= 20  )
    {
          tempPwmCycle = 100;
    }
    else
    {
          tempPwmCycle = 0;
    }
    if( pwmcycle !=  tempPwmCycle )
    {
        pwmcycle = tempPwmCycle;
        analogWrite(output1, pwmcycle);  
        analogWrite(output2, 0);  
    }
}
void timer_irq()
{                       
  cnt++;
}
  

测试

温度数据监控

采集和打印温度数据信息:

温控测试

温度值 Value > 20 ℃ ,开启风扇

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号