Example Code for Raspberry Pi-Read VOC Index

Last revision 2025/12/17

This article details the process of setting up a Raspberry Pi with an SGP40 air quality sensor to read VOC index, including hardware requirements, software setup, wiring diagram, and Python code execution for effective air quality monitoring.

Hardware Preparation

Software Preparation

Wiring Diagram

Connection

Other Preparation Work

Step 1. Enable Raspberry Pi I2C. Skip this step if it is already enabled. Open the terminal, input the following commands:

pi@raspberrypi:~ $ sudo raspi-config

Select "5 Interfacing Options" with the up/down arrows, press "enter", then select "P5 I2C", click "YES". Restart the Raspberry Pi board.

Step 2. Install Python Dependent Library and git(Your Raspberry Pi board needs to access to the Internet). Skip this step if they have been installed already. Input the following commands into the terminal:

pi@raspberrypi:~ $ sudo apt-get update
pi@raspberrypi:~ $ sudo apt-get install build-essential python-dev python-smbus git

Step 3. Download SGP40 Driver library. Input the following commands into the terminal:

pi@raspberrypi:~ $ cd Desktop
pi@raspberrypi:~/Desktop $ git clone https://github.com/DFRobot/DFRobot_SGP40

Sample Code

  • Connect the sensor with your Raspberry Pi board as the diagram shown above. I2C default address: 0x59.

  • Input the following commands into the terminal and run the codes:

pi@raspberrypi:~/Desktop $ cd /home/pi/Desktop/DFRobot_SGP40/Python/raspberrypi/examples/
pi@raspberrypi:~/Desktop/DFRobot_SGP40/Python/raspberrypi/examples $ python get_voc_index.py

Result

Result 2

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?

TOP