Install the SGP4x Driver

The driver installation consists of creating a Python virtual environment, activating it, and installing the driver using pip.

Create a Python virtual environment:

python -m venv .venv

Activate the virtual environment:

source .venv/bin/activate

Install the driver:

python -m pip install sensirion_i2c_sgp4x

Use the SensorBridge on Windows

  1. Install the driver for the Sensirion SEK-SensorBridge:

    python -m pip install sensirion-shdlc-sensorbridge
    
  2. Connect the SEK-SensorBridge to your PC over USB.

    If the SEK-SensorBridge is not detected by your system, follow the SensorBridge FTDI Driver Installation.

  3. Connect the SGP4x sensor to the SEK-SensorBridge.

  4. Run an example script in the activated virtual environment.

    By default, the script assumes that the SensorBridge is connected to the COM1 serial port. If a different port is used, specify it with the --serial-port parameter:

    python example_sensorbridge_sgp40.py --serial-port <your COM port>
    

Example script SGP40

import time
from sensirion_shdlc_driver import ShdlcSerialPort, ShdlcConnection
from sensirion_shdlc_sensorbridge import SensorBridgePort, \
    SensorBridgeShdlcDevice, SensorBridgeI2cProxy
from sensirion_i2c_driver import I2cConnection
from sensirion_i2c_sgp4x import Sgp40I2cDevice

# Connect to the SensorBridge with default settings:
#  - baudrate:      460800
#  - slave address: 0
with ShdlcSerialPort(port='COM1', baudrate=460800) as port:
    bridge = SensorBridgeShdlcDevice(ShdlcConnection(port), slave_address=0)
    print("SensorBridge SN: {}".format(bridge.get_serial_number()))

    # Configure SensorBridge port 1 for SGP40
    bridge.set_i2c_frequency(SensorBridgePort.ONE, frequency=100e3)
    bridge.set_supply_voltage(SensorBridgePort.ONE, voltage=3.3)
    bridge.switch_supply_on(SensorBridgePort.ONE)

    # Create SGP40 device
    i2c_transceiver = SensorBridgeI2cProxy(bridge, port=SensorBridgePort.ONE)
    sgp40 = Sgp40I2cDevice(I2cConnection(i2c_transceiver))

    print("SGP40 Serial Number: {}".format(sgp40.get_serial_number()))

    # Measure every second for one minute
    for _ in range(60):
        time.sleep(1)
        sraw_voc = sgp40.measure_raw()
        # use default formatting for printing output:
        print("SRAW VOC: {}".format(sraw_voc))

    bridge.switch_supply_off(SensorBridgePort.ONE)

Example script SGP41

import time
from sensirion_shdlc_driver import ShdlcSerialPort, ShdlcConnection
from sensirion_shdlc_sensorbridge import SensorBridgePort, \
    SensorBridgeShdlcDevice, SensorBridgeI2cProxy
from sensirion_i2c_driver import I2cConnection
from sensirion_i2c_sgp4x import Sgp41I2cDevice

# Connect to the SensorBridge with default settings:
#  - baudrate:      460800
#  - slave address: 0
with ShdlcSerialPort(port='COM1', baudrate=460800) as port:
    bridge = SensorBridgeShdlcDevice(ShdlcConnection(port), slave_address=0)
    print("SensorBridge SN: {}".format(bridge.get_serial_number()))

    # Configure SensorBridge port 1 for SGP40
    bridge.set_i2c_frequency(SensorBridgePort.ONE, frequency=100e3)
    bridge.set_supply_voltage(SensorBridgePort.ONE, voltage=3.3)
    bridge.switch_supply_on(SensorBridgePort.ONE)

    # Create SGP40 device
    i2c_transceiver = SensorBridgeI2cProxy(bridge, port=SensorBridgePort.ONE)
    sgp41 = Sgp41I2cDevice(I2cConnection(i2c_transceiver))

    print("SGP41 Serial Number: {}".format(sgp41.get_serial_number()))

    # Run conditioning for 10 seconds
    # WARNING: To avoid damage to the sensing material the conditioning must not exceed 10s!
    for _ in range(10):
        time.sleep(1)
        sraw_voc = sgp41.conditioning()
        # use default formatting for printing output:
        print("SRAW VOC: {}\t\tSRAW NOx: conditioning".format(sraw_voc))

    # Measure every second for one minute
    for _ in range(60):
        time.sleep(1)
        sraw_voc, sraw_nox = sgp41.measure_raw()
        # use default formatting for printing output:
        print("SRAW VOC: {}\t\tSRAW NOx: {}".format(sraw_voc, sraw_nox))

    bridge.switch_supply_off(SensorBridgePort.ONE)

Use the Linux I²C Driver

On Linux systems, the sensor can alternatively be accessed directly through the Linux user-space I²C driver.

  1. Connect the SGP4x sensor to an I²C port of your system, for example I²C port 1 of a Raspberry Pi.

  2. Run an example script in the activated virtual environment.

    By default, the script assumes that the sensor is connected to /dev/i2c-1. If a different port is used, specify it with the --i2c-port parameter:

    python example_linux_sgp40.py --i2c-port <your I2C port>
    

Example script SGP40

import time
from sensirion_i2c_driver import LinuxI2cTransceiver, I2cConnection
from sensirion_i2c_sgp4x import Sgp40I2cDevice

# Connect to the I²C 1 port /dev/i2c-1
with LinuxI2cTransceiver('/dev/i2c-1') as i2c_transceiver:
    # Create SGP40 device
    sgp40 = Sgp40I2cDevice(I2cConnection(i2c_transceiver))

    print("SGP40 Serial Number: {}".format(sgp40.get_serial_number()))

    # Measure every second for one minute
    for _ in range(60):
        time.sleep(1)
        sraw_voc = sgp40.measure_raw()
        # use default formatting for printing output:
        print("SRAW VOC: {}".format(sraw_voc))

Example script SGP41

import time
from sensirion_i2c_driver import LinuxI2cTransceiver, I2cConnection
from sensirion_i2c_sgp4x import Sgp41I2cDevice

# Connect to the I²C 1 port /dev/i2c-1
with LinuxI2cTransceiver('/dev/i2c-1') as i2c_transceiver:
    # Create SGP41 device
    sgp41 = Sgp41I2cDevice(I2cConnection(i2c_transceiver))

    print("SGP41 Serial Number: {}".format(sgp41.get_serial_number()))

    # Run conditioning for 10 seconds
    # WARNING: To avoid damage to the sensing material the conditioning must not exceed 10s!
    for _ in range(10):
        time.sleep(1)
        sraw_voc = sgp41.conditioning()
        # use default formatting for printing output:
        print("SRAW VOC: {}\t\tSRAW NOx: conditioning".format(sraw_voc))

    # Measure every second for one minute
    for _ in range(60):
        time.sleep(1)
        sraw_voc, sraw_nox = sgp41.measure_raw()
        # use default formatting for printing output:
        print("SRAW VOC: {}\t\tSRAW NOx: {}".format(sraw_voc, sraw_nox))