Example Code for Raspberry Pi - Read VOC Index
Last revision 2025/12/18
Print the VOC index once every 1 second.
Hardware Preparation
- Raspberry Pi B4(or similar) x 1
- SGP40 Air Quality Sensor x1
- Jumper wires
Software Preparation
Wiring Diagram

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
Print the VOC index once every 1 second.

Was this article helpful?
