Example Code for Arduino - Read VOC Index

Last revision 2025/12/18

Serial print the current VOC index after the sensor is powered on for 10s. The chip has on-chip compensation, so users can use it without calibration. If you need to get a more accurate value, you can open the setrht() function and fill in the RH% of the ambient relative humidity value obtained by the external humidity detection sensor and the ° C of the ambient temperature detected by the temperature sensor.

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • SGP40 Air Quality Sensor x1
  • Jumper wires

Software Preparation

Wiring Diagram

Connection

Other Preparation Work

The product uses standard I2C wiring, which very easy to use. Connect the sensor with a main-board(UNO or others).

The sensor will output more accurate and stable VOC index data after using for 5 minutes.

Sample Code


/*!
 * @file getVocIndex.ino
 * @brief Read the environmental VOC index.  Range: 0-500;\n * @n Experimental phenomena: read environmental VOC index once per second and print the value in serial port
 *
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [yangfeng]<[email protected]>
 * @version  V1.0
 * @date  2020-12-18
 * @get from https://www.dfrobot.com
 * @url  https://github.com/DFRobot/DFRobot_SGP40
 */
#include <DFRobot_SGP40.h>

/* 
 * Method 1: pass in the specified I2C object address 
 * #include <Wire.h>
 * DFRobot_SGP40    mySgp40(&Wire);
 
 * Method 2: use the default I2C object&Wire
 * I2C Default Address:0x59
 */

//#include <Wire.h>
//DFRobot_SGP40    mySgp40(&Wire);
DFRobot_SGP40    mySgp40;


void setup() {
  Serial.begin(115200);
  Serial.println("sgp40 is starting, the reading can be taken after 10 seconds...");
  /*
   * Sensor preheat time: 10s
   * duration: init wait time. Unit: ms. It is suggested: duration>=10000ms
   */
  while(mySgp40.begin(/*duration = */10000) !=true){
    Serial.println("failed to init chip, please check if the chip connection is fine");
    delay(1000);
  }
  Serial.println("----------------------------------------------");
  Serial.println("sgp40 initialized successfully!");
  Serial.println("----------------------------------------------");
  /* 
   * Set the relative humidity and temperature of current environment 
   * The sensor has internal temerpature & humidity calibration. For more accurate VOC index, please open the function setRhT().
   * relativeHumidity:ambient relative humidity, refer to the moisture content in air. Range:0-90, unit: %RH,e.g. 50%
   * temperatureC:ambient temperature. Range: -10~50, unit: °C, e.g. 20°C
   */
  //mySgp40.setRhT(/*relativeHumidity = */ 50, /*temperatureC = */ 20);
  
}

void loop() {
  /* 
   * Get VOC index 
   * VOC index can directly indicate the condition of air quality. The larger the value, the worse the air quality
   *    0-100,no need to ventilate,purify
   *    100-200,no need to ventilate,purify
   *    200-400,ventilate,purify
   *    400-500,ventilate,purify intensely
   * Return VOC index, range: 0-500
   */
  uint16_t index = mySgp40.getVoclndex();
  
  Serial.print("vocIndex = ");
  Serial.println(index);
  delay(1000);
}

Result

Result

Was this article helpful?

TOP