Quick Start

Execute measurements with SensorBridge

The following steps show how to use this driver on a Windows system using the Sensirion SEK-SensorBridge to execute a simple measurement.

  1. Install the SCD4x driver and all required packages as described in Installation.

  2. Install the driver for the Sensirion SEK-SensorBridge

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

  4. Connect the SCD4x sensor to the SEK-SensorBridge

  5. Run the example script from the root of the repository.

    By default the script assumes the SensorBridge is connected to COM1 serial port. If this is different on your system, pass the port in use with the --serial-port parameter as outlined below.

    python examples/example_usage_sensorbridge_scd4x.py --serial-port <your COM port>
    

Example script

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2023 Sensirion AG, Switzerland

import time
import argparse
from sensirion_shdlc_driver import ShdlcSerialPort, ShdlcConnection
from sensirion_shdlc_sensorbridge import SensorBridgePort, \
    SensorBridgeShdlcDevice, SensorBridgeI2cProxy
from sensirion_i2c_driver import I2cConnection
from sensirion_i2c_scd import Scd4xI2cDevice

parser = argparse.ArgumentParser()
parser.add_argument('--serial-port', '-p', default='COM1')
args = parser.parse_args()

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

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

    # Create SCD4x device
    i2c_transceiver = SensorBridgeI2cProxy(bridge, port=SensorBridgePort.ONE)
    scd4x = Scd4xI2cDevice(I2cConnection(i2c_transceiver))

    # Make sure measurement is stopped, else we can't read serial number or
    # start a new measurement
    scd4x.stop_periodic_measurement()

    print("scd4x Serial Number: {}".format(scd4x.read_serial_number()))

    # start periodic measurement in high power mode
    scd4x.start_periodic_measurement()

    # Measure every 5 seconds for 5 minute
    for _ in range(60):
        time.sleep(5)
        co2, temperature, humidity = scd4x.read_measurement()
        # use default formatting for printing output:
        print("{}, {}, {}".format(co2, temperature, humidity))
        # custom printing of attributes:
        print("{:d} ppm CO2, {:0.2f} °C ({} ticks), {:0.1f} %RH ({} ticks)".format(
            co2.co2,
            temperature.degrees_celsius, temperature.ticks,
            humidity.percent_rh, humidity.ticks))
    scd4x.stop_periodic_measurement()

Execute measurements using internal Linux I²C driver

On Linux systems it is furthermore possible to use the Linux user space I²C driver directly. How this can be done is shown in the following.

  1. Install the SCD4x driver and all required packages as described in Installation.

  2. Connect the SCD4x sensor to the I²C port of your system (for example to the I²C port 1 of a Raspberry Pi).

  3. Run the example script from the root of the repository.

    By default the script assumes you have the sensor connected to /dev/i2c-1. If this is different on your system, pass the port in use with the --i2c-port parameter as outlined below.

    python examples/example_usage_linux_scd4x.py --i2c-port <your I2C port>
    

Example script

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2023 Sensirion AG, Switzerland

import time
import argparse
from sensirion_i2c_driver import LinuxI2cTransceiver, I2cConnection
from sensirion_i2c_scd import Scd4xI2cDevice

parser = argparse.ArgumentParser()
parser.add_argument('--i2c-port', '-p', default='/dev/i2c-1')
args = parser.parse_args()

# Connect to the I²C 1 port
with LinuxI2cTransceiver(args.i2c_port) as i2c_transceiver:
    # Create SCD4x device
    scd4x = Scd4xI2cDevice(I2cConnection(i2c_transceiver))

    # Make sure measurement is stopped, else we can't read serial number or
    # start a new measurement
    scd4x.stop_periodic_measurement()

    print("scd4x Serial Number: {}".format(scd4x.read_serial_number()))

    scd4x.start_periodic_measurement()

    # Measure every 5 seconds for 5 minute
    for _ in range(60):
        time.sleep(5)
        co2, temperature, humidity = scd4x.read_measurement()
        # use default formatting for printing output:
        print("{}, {}, {}".format(co2, temperature, humidity))