Example Code for STCC4 Continuous Measurement on Arduino UNO

Last revision 2026/03/06

Explore continuous CO2 measurement using Gravity STCC4 with Arduino UNO. This guide covers hardware setup, software requirements, and example code for reading sensor data via the I2C interface. The setup outputs CO2, temperature, and humidity data to the serial monitor. Follow the tutorial for a seamless project experience. Ideal for learning environmental sensing and data acquisition.

Hardware

Software

Wiring Diagram

Example 1 — Continuous Measurement

Make sure the onboard jumpers are set to address 0x64 and the SHT40 compensation is enabled. After uploading the sketch, open the serial monitor and wait for ~20 seconds.

/*!
 * @file continueRead.ino
 * @brief This routine continuously reads sensor data via the I2C interface and can obtain one piece of data per second.
 * @n If you connect the humidity and temperature sensor, you can obtain the concentration of carbon dioxide and temperature and humidity.
 * @n If the temperature and humidity sensors are not connected, the obtained temperature and humidity values are default values.
 * @n The demo supports FireBeetle-ESP32, FireBeetle-ESP8266, Arduino Uno, Leonardo, Mega2560, FireBeetle-M0
 * @details Experimental phenomenon: The read data will be output in the serial port monitor.
 *
 * @copyright Copyright (c) 2025 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license The MIT License (MIT)
 * @author [lbx]([email protected])
 * @version V1.0
 * @date 2025-08-14
 * @url https://github.com/DFRobot/DFRobot_STCC4
 */

#include "DFRobot_STCC4.h"


DFRobot_STCC4_I2C sensor(&Wire, 0x64); // Create an instance of the DFRobot_STCC4_I2C class with the I2C address 0x64.

void setup() {
  char id[18];
  Serial.begin(115200);
  while(!Serial) delay(100); // Wait for the serial port to be ready.

  Serial.println("This is a demo of continuous reading sensor data.\n\n");
  delay(500);
  while(sensor.begin()){
    Serial.println("Init error!");
    delay(500);
  }
  sensor.wakeup();
  /* Read sensor ID */
  while(!sensor.getID(id)){
    delay(500);
    Serial.println("Get ID error!");
  }
  sprintf(id, "%02x%02x%02x%02x", id[0], id[1], id[3], id[4]);
  Serial.print("ID: ");
  Serial.println(id);
  sensor.startMeasurement();
}

uint16_t co2Concentration;
float temperature;
float humidity;
uint16_t sensorStatus;

void loop() {
  delay(1000);
  /* Read sensor data */
  if(sensor.measurement(&co2Concentration,&temperature,&humidity,&sensorStatus)){
    String output = "CO2: " + String(co2Concentration) + 
                    " ppm   Temperature: " + String(temperature, 2) + 
                    " C   Humidity: " + String(humidity, 2) + " %";
    Serial.println(output);
  }
}

Result

The serial monitor displays CO2 concentration and temperature/humidity readings. Blowing on the sensor will cause the CO2 reading to rise noticeably.

Was this article helpful?

TOP