Arduino 小白的 DIY 空气质量检测仪(4)- 温湿度模块、UV紫外线模块、时钟模块

25 阅读2分钟

2025第一个工作日,新年快乐!

关门关窗睡觉,即使开了新风(低档),今早测得二氧化碳1800+、TVOC280+,看来还是要想想办法提高一下换气效率,且不能噪音太大呀~

接上回

逐个分析

m_Temperature_Tumidity.h

#include <Arduino.h>

#include <DHT22.h>

// 温湿度
// 型号:dht_22
// 接口:DAT->A1(当作D用)、VCC->VCC(5V)、GND->GND
// 协议:1-Wire(单总线)

#define _Pin_Temperature_Humidity A1

namespace Module {

DHT22 _m_dht_22(_Pin_Temperature_Humidity);

struct _Temperature {
  float lastValue = 25;
  float getValue() {
    float value = _m_dht_22.getTemperature();
    if (value > 50 || value < -50) {
      value = lastValue;
    }
    lastValue = value;
    return value;
  }

  char unit[3] = " C";
} Temperature;

struct _Humidity {
  float lastValue = 70;
  float getValue() {
    float value = _m_dht_22.getHumidity();
    if (value > 100 || value < 0) {
      value = lastValue;
    }
    lastValue = value;
    return value;
  }

  char unit[2] = "%";
} Humidity;

}

通讯方式是 1-Wire(单总线)

温湿度模块直接使用第三方库读取非常方便,由于 1-Wire 接收的电压影响比较大,例如引脚接触不良、震动会影响读数,出现偶发异常的数值。所以上面定义了一个 lastValue 变量记录上一个合理的数值,用于后面替换掉异常的数值。

m_UV.h

#include <Arduino.h>

// 来源于网络示例
// https://www.sohu.com/a/694634336_120248280
// https://wiki.dfrobot.com.cn/_SKU_SEN0162__UV_Sensor


// 紫外线指数
// 型号:guva_s12sd
// 接口:VCC->VCC(5V)、GND->GND、SIO->A7
// 协议:ADC

#define _Pin_UV A7

namespace Module {

struct _UV {
  int getValue() {
    // 0~1023
    int analogValue = analogRead(_Pin_UV);
    // mV
    float voltageValue = (float)analogValue / 1024 * 5 * 1000;

    return voltageValue;
  }

  char unit[6] = "index";
} UV;

}

通讯方式是 ADC

UV 紫外线模块更加简单了,直接读取模拟信号,根据这里的文档GUVA-S12SD,直接输出对应紫外线指数(UV INDEX)的线性电压,输出电压范围大约0~1100mV(对应UV INDEX值为0~11)。

在这里插入图片描述

(float)analogValue / 1024 * 5 * 1000

这里的“5”对应的是接入 VCC 电压 5V,如果接入的是 3.3V,就需要替换为“3.3”,建议保持和示例一样接入 5V。

这里 getValue 输出的是图中的 Vout(mV),通过 Analog Value 转换得出,数值较大,可以显得 UV 紫外线指数更灵敏。

如果想直接输出 1~11 指数,可以自行通过 if else 进行转换。

clock.h

#include <Arduino.h>

// 时钟
#include <RTClib.h>

namespace Clock {

// 时钟
// 接口:VCC->VCC(5V)、GND->GND、CLK->D2、DAT->D4、RST-D5
// 协议:SPI
DS1302 _rtc(5, 2, 4);

struct _RTC {
  void init() {
    // 时钟初始化
    _rtc.begin();

    // 更新时间
    DateTime now = _rtc.now();
    DateTime cNow = DateTime(__DATE__, __TIME__);
    if (!(now.year() > cNow.year()
          || now.month() > cNow.month()
          || now.day() > cNow.day()
          || now.hour() > cNow.hour()
          || now.minute() > cNow.minute()
          || now.second() > cNow.second())) {
      _rtc.adjust(cNow);
    }
    // _rtc.adjust(cNow);
  }

  DateTime now() {
    return _rtc.now();
  }
} RTC;

}

通讯方式是 SPI

获取时钟模块时间:

DateTime now = _rtc.now();

获取编译时间:

DateTime cNow = DateTime(__DATE__, __TIME__);

如果时钟模块时间慢于编译时间,则以编译时间为准。

    if (!(now.year() > cNow.year()
          || now.month() > cNow.month()
          || now.day() > cNow.day()
          || now.hour() > cNow.hour()
          || now.minute() > cNow.minute()
          || now.second() > cNow.second())) {
      _rtc.adjust(cNow);
    }

未完待续。。。

多多支持其它文章 juejin.cn/user/155656…

又或者请我喝杯奶茶😍 vue3-zoom-drag