Example Code for Arduino-Concentration Alarm
Last revision 2025/12/17
Set up a concentration alarm where an interrupt is triggered when CO2 levels cross predefined thresholds (low→medium or medium→high). The current CO2 and TVOC values are printed on interrupt.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- CCS811 Air Quality Sensor x 1
- Jumper wires
Software Preparation
- Arduino IDE
- Download and install the CCS811 Library and examples. (About how to install the library?)
Wiring Diagram

Note: Connect the sensor's INT pin to the UNO's D2 pin (interrupt pin 0).
Other Preparation Work
- Please run the sensor for 48hours when using it for the first time.
- Input the baseline value to
sensor.writeBaseLine();.
Sample Code
/*!
* @file setInterrupt.ino
* @brief Set interrupt parameter, when CO2 concentration range changes, get an interrupt
* @n Experiment phenomenon: read data every 1s, and print it out on serial port.
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [LuoYufeng]([email protected])
* @version V1.0
* @date 2019-07-13
* @get from https://www.dfrobot.com
* @url https://github.com/DFRobot/DFRobot_Sensor
*/
#include "DFRobot_CCS811.h"
volatile int8_t GPIO1TRIG = 0;
/*
* IIC address default 0x5A, the address becomes 0x5B if the ADDR_SEL is soldered.
*/
//DFRobot_CCS811 CCS811(&Wire, /*IIC_ADDRESS=*/0x5A);
DFRobot_CCS811 CCS811;
void setup(void)
{
Serial.begin(115200);
/*wait for the chip to be initialized completely, and then exit*/
while(CCS811.begin() != 0){
Serial.println("failed to init chip, please check if the chip connection is fine");
delay(1000);
}
attachInterrupt(0, interrupt, RISING);
/**
* @brief Measurement parameter configuration
* @param mode:in typedef enum{
* eClosed, //Idle (Measurements are disabled in this mode)
* eCycle_1s, //Constant power mode, IAQ measurement every second
* eCycle_10s, //Pulse heating mode IAQ measurement every 10 seconds
* eCycle_60s, //Low power pulse heating mode IAQ measurement every 60 seconds
* eCycle_250ms //Constant power mode, sensor measurement every 250ms 1xx: Reserved modes (For future use)
* }eCycle_t;
* @param thresh:0 for Interrupt mode operates normally; 1 for interrupt mode only asserts the nINT signal (driven low) if the new
* @param interrupt:0 for Interrupt generation is disabled; 1 for the nINT signal is asserted (driven low) when a new sample is ready in
*/
CCS811.setMeasurementMode(CCS811.eCycle_250ms, 1, 1);
/**
* @brief Set interrupt thresholds
* @param lowToMed: interrupt triggered value in range low to middle
* @param medToHigh: interrupt triggered value in range middle to high
*/
CCS811.setThresholds(1500,2500);
}
void loop() {
if(GPIO1TRIG == 1){
Serial.println("CO2 range has changed");
Serial.print("CO2: ");
Serial.print(CCS811.getCO2PPM());
Serial.print("ppm, TVOC: ");
Serial.print(CCS811.getTVOCPPB());
Serial.println("ppb");
delay(1000);
}
GPIO1TRIG = 0;
Serial.print("CO2: ");
Serial.print(CCS811.getCO2PPM());
Serial.print("ppm, TVOC: ");
Serial.print(CCS811.getTVOCPPB());
Serial.println("ppb");
CCS811.writeBaseLine(0x447B);
delay(1000);
}
void interrupt(){
GPIO1TRIG = 1;
}
Result
When the CO2 concentration crosses a threshold, an interrupt is generated and the current values are printed.

Additional Information
- AVR Series Interrupt Pin and Interrupt Number
| 328 Mainboards: Uno, Nano, Mini... | Mega2560 | 32u4 Mainboards: Leonardo... | |
|---|---|---|---|
| Interrupt Pin | D2, D3 | D2, D3, D21, D20, D19, D18 | D3, D2, D0, D1, D7 |
| Interrupt Number | 0, 1 | 0, 1, 2, 3, 4, 5 | 0, 1, 2, 3 ,4 |
Was this article helpful?
