ENS160空气质量传感器

简介

Fermion: ENS160 空气质量传感器,搭载了ScioSense公司新推出ENS160空气质量传感器。该传感器专为室内空气质量检测而设计,能直接输出多种IAQ(TVOC、eCO2、AQI)数据。创新的TrueVOC技术结合金属氧化物(MOX)技术使得该传感器有优越准确性、快速响应、抗干扰等特性。 ENS160空气质量传感器内置算法,直接输出TVOC、eCO2、AQI数据,环境数据更丰富、容易理解。该传感器的预热时间小于3分钟,可以更快速的获得准确数据,内置基线自动校准算法,确保了传感器的长期稳定性。

空气质量参考指标

AQI等级参考


eCO2/CO2浓度参考


TVOC浓度参考


产品特性

应用场景

技术规格

引脚说明


序号 丝印 功能描述
1 3V3 电源正极3.3V
2 GND 电源负极
3 SCL I2C时钟线
4 SDA I2C数据线
5 SCK 串行时钟线
6 SDI 串行数据输入
7 SDO 串行数据输出
8 CS 片选引脚
9 INT 中断引脚

Arduino使用教程

按接线图所示将传感器与FireBeetle Board ESP32-E(或其它3.3V主板)相连接.

准备

关于如何安装库文件,点击链接

接线图


样例代码1 - 轮询获取数据

传感器运行状态

状态 解释
0 正常运行
1 预热,上电后前3分钟(需传感器不再出现初次启动状态)
2 初次启动,上电后前1小时(传感器连续工作24小时后将不再出现该状态,如果中途出现断电,重新上电后传感器仍会进入初次启动状态)

更详细的解释请看芯片手册第10章

注意:环境温湿度会影响数据准确度,请在setTempAndHum(/*temperature=*/temp, /*humidity=*/hum);函数中填入当前环境温湿度。

/*!
 * @file  getMeasureData.ino
 * @brief  通过轮询方式获取传感器数据(Fermion版本请使用3.3V主控)
 * @details  配置传感器电源模式, 测量参数(测量气体时用于补偿校准的温度和相对湿度)
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license  The MIT License (MIT)
 * @author  [qsjhyy](yihuan.huang@dfrobot.com)
 * @version  V1.0
 * @date  2021-10-26
 * @url  https://github.com/DFRobot/DFRobot_ENS160
 */
#include <DFRobot_ENS160.h>

#define I2C_COMMUNICATION  //use I2C for communication, 屏蔽此行代码则使用SPI进行通信

#ifdef  I2C_COMMUNICATION
  /**
   *   Fermion版本I2C地址默认为0x53, 将SDO引脚连接到GND I2C地址为0x52
   */
  DFRobot_ENS160_I2C ENS160(&Wire, /*iicAddr*/ 0x53);
#else
  uint8_t csPin = D3;
  DFRobot_ENS160_SPI ENS160(&SPI, csPin);
#endif

void setup(void)
{
  Serial.begin(115200);

  // 初始化传感器
  while( NO_ERR != ENS160.begin() ){
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }

  /**
   * 设置电源模式
   * mode 可配置的电源模式:
   *   ENS160_SLEEP_MODE: DEEP SLEEP mode (low power standby)
   *   ENS160_IDLE_MODE: IDLE mode (传感器不进行数据测量)
   *   ENS160_STANDARD_MODE: STANDARD Gas Sensing Modes
   */
  ENS160.setPWRMode(ENS160_STANDARD_MODE);

  /**
   * 设置温湿度环境参数,用于测量数据的校准补偿
   * temperature 当前环境温度, float类型, 单位: C
   * humidity    当前环境湿度, float类型, 单位: %rH
   */
  ENS160.setTempAndHum(/*temperature=*/25.0, /*humidity=*/50.0);

}

void loop()
{
  /**
   * 获取传感器运行状
   * 返回值:0-Normal operation, 
   *         1-Warm-Up phase, During first 3 minutes after power-on.
   *         2-Initial Start-Up phase, During first full hour of operation after initial power-on.Only once in the sensor’s lifetime.
   *           Note that the status will only be stored in the non-volatile memory after an initial 24h of continuous
   *           operation. If unpowered before conclusion of said period, the ENS160 will resume "Initial Start-up" mode
   *           after re-powering.
   */
  uint8_t Status = ENS160.getENS160Status();
  Serial.print("传感器运行状态 : ");
  Serial.println(Status);
  /**
   * 获取空气质量指数
   * 返回值: 1-Excellent, 2-Good, 3-Moderate, 4-Poor, 5-Unhealthy
   */
  uint8_t AQI = ENS160.getAQI();
  Serial.print("Air quality index : ");
  Serial.println(AQI);

  /**
   * 获取TVOC数据
   * 返回值范围为: 0–65000, 单位: ppb
   */
  uint16_t TVOC = ENS160.getTVOC();
  Serial.print("Concentration of total volatile organic compounds : ");
  Serial.print(TVOC);
  Serial.println(" ppb");

  /**
   * 获取eCO2数据
   * 返回值范围为: 400–65000, 单位: ppm
   */
  uint16_t ECO2 = ENS160.getECO2();
  Serial.print("Carbon dioxide equivalent concentration : ");
  Serial.print(ECO2);
  Serial.println(" ppm");

  Serial.println();
  delay(1000);
}

结果

实时的eCO2、TVOC浓度、AQI等级以及传感器运行状态已经打印在了串口监视器中


样例代码2 - 中断获取数据

通过Arduino IDE将程序下载到FireBeetle Board ESP32-E,当中断触发时我们可以在串口监视器看到打印出的TVOC、eCO2浓度以及AQI等级。

/*!
 * @file  interruptDataDrdy.ino
 * @brief  通过中断获取传感器数据(Fermion版本请使用3.3V主控,该示例仅适用于Fermion版本)
 * @details  配置传感器中断模式, 当传感器有新数据时产生中断
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license  The MIT License (MIT)
 * @author  [qsjhyy](yihuan.huang@dfrobot.com)
 * @version  V1.0
 * @date  2021-10-27
 * @url  https://github.com/DFRobot/DFRobot_ENS160
 */
#include <DFRobot_ENS160.h>

#define I2C_COMMUNICATION  //use I2C for communication, 屏蔽此行代码则使用SPI进行通信

#ifdef  I2C_COMMUNICATION
  /**
   *   Fermion版本I2C地址默认为0x53, 将SDO引脚连接到GND I2C地址为0x52
   */
  DFRobot_ENS160_I2C ENS160(&Wire, /*iicAddr*/ 0x53);
#else
  uint8_t csPin = D3;
  DFRobot_ENS160_SPI ENS160(&SPI, csPin);
#endif

/* Interrupt flag */
volatile uint8_t flag = 0;
/* External interrupt flag */
void interrupt()
{
  if(flag ==0){
    flag = 1;
  }
}

void setup(void)
{
  Serial.begin(115200);

  // 初始化传感器
  while( NO_ERR != ENS160.begin() ){
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }
  /**
   * 设置温湿度环境参数,用于用于测量数据的校准补偿
   * temperature 当前环境温度, float类型, 单位: C
   * humidity    当前环境湿度, float类型, 单位: %rH
   */
  ENS160.setTempAndHum(/*temperature=*/25.0, /*humidity=*/50.0);

  #if defined(ESP32) || defined(ESP8266)
    // D2 pin is used as interrupt pin by default, other non-conflicting pins can also be selected as external interrupt pins.
    attachInterrupt(digitalPinToInterrupt(D4)/* Query the interrupt number of the D4 pin */, interrupt, RISING);
  #elif defined(ARDUINO_SAM_ZERO)
    // Pin 5 is used as interrupt pin by default, other non-conflicting pins can also be selected as external interrupt pins
    attachInterrupt(digitalPinToInterrupt(5)/* Query the interrupt number of the 5 pin */, interrupt, RISING);
  #endif

  /**
   * 中断配置(INT)
   * mode 需要设置的中断模式,下列模式相与为mode:
   *   中断设置(有新数据时产生中断): eINTModeDIS-禁用中断, eINTModeEN-启用中断
   *   中断引脚输出驱动模式: eINTPinOD-开漏输出, eINTPinPP-推挽输出
   *   中断引脚有效电平: eINTPinActiveLow-低电平有效, eINTPinActiveHigh-高电平有效
   */
  ENS160.setINTMode(ENS160.eINTModeEN | 
                    ENS160.eINTPinPP | 
                    ENS160.eINTPinActiveHigh);

  Serial.println();
  delay(1000);
}

void loop()
{
  if(flag == 1){
    flag = 0;
    /**
     * 获取根据UBA计算出的空气质量指数
     * 返回值范围为: 1-5(对应Excellent, Good, Moderate, Poor, Unhealthy这五个等级)
     */
    uint8_t AQI = ENS160.getAQI();
    Serial.print("Air quality index : ");
    Serial.println(AQI);

    /**
     * 获取总挥发性有机化合物(TVOC)的浓度
     * 返回值范围为: 0–65000, 单位: ppb
     */
    uint16_t TVOC = ENS160.getTVOC();
    Serial.print("Concentration of total volatile organic compounds : ");
    Serial.print(TVOC);
    Serial.println(" ppb");

    /**
     * 获取根据检测到的VOCs和氢报告计算出的二氧化碳当量浓度(eCO2 – Equivalent CO2)
     * 返回值范围为: 400–65000, 单位: ppm
     * 分为五个等级: Excellent(400 - 600), Good(600 - 800), Moderate(800 - 1000), 
     *               Poor(1000 - 1500), Unhealthy(> 1500)
     */
    uint16_t ECO2 = ENS160.getECO2();
    Serial.print("Carbon dioxide equivalent concentration : ");
    Serial.print(ECO2);
    Serial.println(" ppm");

  }
}

结果

实时的eCO2、TVOC浓度和AQI等级已经打印在了串口监视器中


主要API接口函数列表

  /**
   * @fn setPWRMode
   * @brief 设置电源模式
   * @param mode 可配置的电源模式:
   * @n       ENS160_SLEEP_MODE: DEEP SLEEP mode (low power standby)
   * @n       ENS160_IDLE_MODE: IDLE mode (low-power)
   * @n       ENS160_STANDARD_MODE: STANDARD Gas Sensing Modes
   * @return None
   */
  void setPWRMode(uint8_t mode);

  /**
   * @fn setINTMode
   * @brief 中断配置(INT)
   * @param mode 需要设置的中断模式,下列模式相与为mode:
   * @n       在DATA_XXX寄存器中出现新数据(可获取新的测量数据)时产生中断: eINTModeDIS, 禁用中断; eINTModeEN, 启用中断
   * @n       中断引脚输出驱动模式: eINTPinOD, 开漏; eINTPinPP, 推挽
   * @n       中断引脚有效电平: eINTPinActiveLow, 低电平有效; eINTPinActiveHigh, 高电平有效
   * @return None
   */
  void setINTMode(uint8_t mode);

  /**
   * @fn setTempAndHum
   * @brief 用户将环境温度和相对湿度写入ENS160, 用于气体测量数据的校准补偿。
   * @param ambientTemp 用于补偿的当前环境温度, float类型, 单位: C
   * @param relativeHumidity 用于补偿的当前环境湿度, float类型, 单位: %rH
   * @return None
   */
  void setTempAndHum(float ambientTemp, float relativeHumidity);

  /**
   * @fn getENS160Status
   * @brief 这个API获取传感器的运行状态信息
   * @return 运行状态:
   * @n        eNormalOperation: Normal operation; 
   * @n        eWarmUpPhase: Warm-Up phase; 
   * @n        eInitialStartUpPhase: Initial Start-Up phase; 
   * @n        eInvalidOutput: Invalid output
   */
  uint8_t getENS160Status(void);

  /**
   * @fn getAQI
   * @brief 获取根据UBA计算出的空气质量指数
   * @return 返回值范围为: 1-5(对应Excellent, Good, Moderate, Poor, Unhealthy这五个等级)
   */
  uint8_t getAQI(void);

  /**
   * @fn getTVOC
   * @brief 获取总挥发性有机化合物(TVOC)的浓度
   * @return 返回值范围为: 0–65000, 单位: ppb
   */
  uint16_t getTVOC(void);

  /**
   * @fn getECO2
   * @brief 获取根据检测到的VOCs和氢报告计算出的二氧化碳当量浓度(eCO2 – Equivalent CO2)
   * @return 返回值范围为: 400–65000, 单位: ppm
   * @note 分为五个等级: Excellent(400 - 600), Good(600 - 800), Moderate(800 - 1000), 
   * @n                  Poor(1000 - 1500), Unhealthy(> 1500)
   */
  uint16_t getECO2(void);

树莓派使用教程

特别注意:第一次使用请让传感器先运行1小时后以确保数据准确,之后每次使用先运行(预热)三分钟。同时还需环境温湿度对测量数据进行补偿。

准备

接线图


        I2C接线图                                                              SPI接线图

安装驱动

  1. 启动树莓派的I2C接口。如已开启,可跳过该步骤。 打开终端(Terminal),键入如下指令,并回车:
    pi@raspberrypi:~ $ sudo raspi-config
    然后用上下键选择“Interfacing Options ”, 按回车进入,选择 “ P5 I2C ”, 按回车确认“ YES ”即可。重启树莓派主控板。

  2. 安装Python依赖库与git,树莓派需要联网。如已安装,可跳过该步骤。 在终端中,依次键入如下指令,并回车: pi@raspberrypi:~ $ sudo apt-get update pi@raspberrypi:~ $ sudo apt-get install build-essential python-dev python-smbus git

  3. 下载ENS160驱动库。在终端中,依次键入如下指令,并回车: pi@raspberrypi:~ $ cd Desktop/ pi@raspberrypi:~/Desktop $ git clone https://github.com/DFRobot/DFRobot_ENS160

样例代码1 - 轮询获取数据

pi@raspberrypi:~/Desktop $ cd DFRobot_ENS160/python/raspberrypi/examples/get_measure_data

pi@raspberrypi:~/Desktop/DFRobot_ENS160/python/raspberrypi/examples/get_measure_data $python get_measure_data.py

样例代码2 - 中断方式数据

pi@raspberrypi:~/Desktop $ cd DFRobot_ENS160/python/raspberrypi/examples/interrupt_data_drdy

pi@raspberrypi:~/Desktop/DFRobot_ENS160/python/raspberrypi/examples/interrupt_data_drdy $python interrupt_data_drdy.py

常见问题

还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!

更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。

更多