Example Code for Arduino-Read VOC Index
Last revision 2025/12/17
Serial print the currect 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
- Gravity: SGP40 Air Quality Sensor x1
- Jumper wires
Software Preparation
- Arduino IDE
- Download and install the SGP40 Library and Sample Code. (About how to install the library?)
Wiring Diagram

Other Preparation Work
The sensor adopts easy-to-use standard Gravity I2C interface. Connect the sensor to your mainboard as the diagram. The product features short preheat time and 10s algorithms.
Note: The sensor will output more accurate and stable VOC data after using for 5 minutes.
Main API Function List
/**
* @brief Initialization function
* @param duration Warm-up time
* @return return true succeed ;return false failed.
*/
bool begin(uint32_t duration = 10000);
/**
* @brief Set the temperature and humidity
* @param relativeHumidityRH Current environmental relative humidity value, range 0-90, unit: %RH
* @param temperatureC Current ambient temperature, range -10~50, unit: °C
*/
void setRhT(float relativeHumidity = 50,float temperatureC=25);
/**
* @brief Measure VOC index after humidity compensation
* @note VOC index can indicate the quality of the air directly. The larger the value, the worse the air quality.
* @note 0-100,no need to ventilate, purify
* @note 100-200,no need to ventilate, purify
* @note 200-400,ventilate, purify
* @note 400-500,ventilate, purify intensely
* @return The VOC index measured, ranged from 0 to 500
*/
uint16_t getVoclndex(void);
Sample Code
/*!
* @file getVocIndex.ino
* @brief Read the environmental VOC index. Range: 0-500;\
* @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

Additional Information
The sensor provides the change of air quality trend, and the algorithm will continuously adjust the baseline of typical air according to historical data. The longer the sensor is used, the better the sensitivity and accuracy of trend and change.
In the normal air environment, the sensor can obtain the accurate VOC index immediately after use; in the heavily-polluted environment, the sensor can obtain the accurate VOC index after use for about 1 hour. It is recommended to integrate it into the long-term indoor projects.
Was this article helpful?
