Example Code for Arduino - Get Sensor Data

Last revision 2026/10/21

This tutorial covers setting up and using an Arduino UNO R4 WiFi with a Gravity: STCC4 CO2 Sensor. It includes hardware and software preparation, wiring, and sample code to read sensor data. The setup involves downloading the necessary libraries, connecting the sensor, and initializing it in the Arduino IDE. The provided code demonstrates how to continuously read and print CO2 concentration, temperature, and humidity. Ensure you have the correct library version for your Arduino IDE. It's like following a recipe to make a dish, step by step.

Prepare

Hardware Prepare

Name SKU Quantity Purchase Link Description
Arduino UNO R4 WiFi DFR1084 1 https://www.dfrobot.com/product-2700.html Controller
Gravity: STCC4 CO2 Sensor SEN0678 2 https://www.dfrobot.com/product-3098.html Sensor
Gravity to Qwiic Adapter DFR1271 1 Adapter
Qwiic cable Come with Qwiic Adapter 1 Cable
Gravity cable Come with Gravity sensor 2 Cable

Software Prepare

Wiring Diagram

Prepare Wiring Diagram

Sensor Pin Adapter board Pin
Gravity STCC4 CO2 Sensor VCC Gravity to qwiic board VCC
Gravity STCC4 CO2 Sensor GND Gravity to qwiic board GND
Gravity STCC4 CO2 Sensor SCL Gravity to qwiic board SCL
Gravity STCC4 CO2 Sensor SDA Gravity to qwiic board SDA

Sample code 2- Get sensor data

#include "DFRobot_STCC4.h"

#define ARDUINO_UNO_R4_WIFI //If you want to use this code on a non-UNO R4 WiFi device, delete this line.

const uint16_t tCompensation = 26;
const uint16_t hCompensation = 55;
const uint16_t pCompensation = 950;
float temperature;
float humidity;
uint16_t co2Concentration;
uint16_t sensorStatus;

const uint8_t ADDR = 0x64;

DFRobot_STCC4_I2C* sensor = NULL;

void setup() {
  Serial.begin(115200);
  unsigned long serialWaitStart = millis();
  while(!Serial && (millis() - serialWaitStart < 3000)) {
    delay(10); 
  }

#if defined(ARDUINO_UNO_R4_WIFI)
  static DFRobot_STCC4_I2C sensorInstance(&Wire1, ADDR);
#else
  static DFRobot_STCC4_I2C sensorInstance(&Wire, ADDR); 
#endif
  sensor = &sensorInstance;

  Serial.println("[STCC4] setup start");
  Serial.println("This is a demo of continuous reading sensor data.\n");

  /* Initialize the sensor */
  while(!sensor->begin()){
    Serial.println("Init error!");
    delay(500);
  }

  /* Wake up the sensor */
  sensor->wakeup();
  delay(10);

  /**
   * Get the ID of the sensor.
   * The ID values read should all be 0x901018A.
   */
  Serial.print("ID: 0x");
  Serial.println(sensor->getID(), HEX);

  /**
    * @brief Set temperature and humidity compensation
    * @param temperature Temperature compensation value
    * @param humidity Humidity compensation value
    * @return true if successful, false otherwise
    */
  if(sensor->setRHTcompensation(tCompensation, hCompensation)){
    Serial.println("Set RHT compensation successful.");
  }else{
    Serial.println("Set RHT compensation error!");
  }

  /**
    * @brief Set pressure compensation
    * @param pressure Pressure compensation value
    * @return true if successful, false otherwise
    */
  if(sensor->setPressureCompensation(pCompensation)){
    Serial.println("Set pressure compensation successful.");
  }else{
    Serial.println("Set pressure compensation error!");
  }

  sensor->startMeasurement(); // Start measurement
}

void loop() {
  delay(2000);
  if(sensor == NULL) {
    return;
  }
  /**
    * @brief Read measurement data
    * @param co2Concentration Pointer to store CO2 concentration
    * @param temperature Pointer to store temperature
    * @param humidity Pointer to store humidity
    * @param sensorStatus Pointer to store sensor status, 0 means normal, otherwise error.
    * @return true if successful, false otherwise
    */
  if(sensor->measurement(&co2Concentration,&temperature,&humidity,&sensorStatus)){
    Serial.print("CO2:");
    Serial.print(co2Concentration);
    Serial.print(" ppm ");
    Serial.print(" temperature:");
    Serial.print(temperature);
    Serial.print(" ℃ ");
    Serial.print(" humidity:");
    Serial.print(humidity);
    Serial.print(" % ");
    Serial.print(" status:");
    Serial.println(sensorStatus);
  }
}

Result

Sample code 2- Get sensor data Result
The serial monitor will display the CO2 concentration, temperature, humidity, and sensor status every 2 seconds.

Was this article helpful?

TOP