基于Arduino UNO设计一个温控制系统

本文详细介绍了使用ArduinoUNO构建一个智能温控系统,包括硬件组件如DS18B20、L298N和OLED的连接,以及软件设计中的定时器和PWM技术的应用。系统能根据环境温度自动控制风扇运行。
摘要由CSDN通过智能技术生成

目录

概述

1 硬件结构

1.1 整体硬件介绍

1.2 硬件连接结构

2 软件设计

2.1 软件功能介绍

2.2 关于Arduino的一些知识点

2.2.1 定时器 

2.2.2 PWM

2.3 代码实现

2.3.1 编译工具

2.3.2 详细代码

3 测试

3.1 温度数据监控

3.2 温控测试


概述

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

1 硬件结构

1.1 整体硬件介绍

1)Arduino UNO: 主控板卡

2)控制L298N:用于控制电机系统

3)控制OLED模块:用于显示当前温度数据

4)控制DS18B20:获取环境温度数据

5)直流电机:驱动扇叶

1.2 硬件连接结构

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

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

2 软件设计

2.1 软件功能介绍

软件主要实现功能如下:

1) 控制DS18B20,读取该传感器采集到的温度值

2) 在OLED显示温度数据

3)通过串口打印调试信息

4)根据门限值,控制电机转速(PWM)

2.2 关于Arduino的一些知识点

2.2.1 定时器 

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

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


//回调函数 
void timer_irq()
{

}

2.2.2 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);  
}

2.3 代码实现

2.3.1 编译工具

2.3.2 详细代码

/*
Copyright  2024-2029. All rights reserved.
文件名     : motorCtrl
作者       : tangmingfei2013@126.com
版本       : 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++;
}

3 测试

3.1 温度数据监控

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

3.2 温控测试

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

  • 36
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值