SCD30 --> OSError: I2C bus error



  • When using the micropython-scd30 library I randomly get an OSError: I2C bus error.
    https://github.com/agners/micropython-scd30

    FYI

    1. Sensirion recommends a baudrate of 50.000 for this sensor. https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/9.5_CO2/Sensirion_CO2_Sensors_SCD30_Interface_Description.pdf
    2. I've used a pull-up resistor on the SDA. This did not improve the situation.

    Is there something wrong in the I2C library? I haven't had this problem with my Particle.io devices.

    What is the best way to debug this?

    code I used:

    import time
    from scd30 import SCD30
    from machine import I2C
    
    #I2C setup
    print("setup i2c")
    i2c = I2C(0, I2C.MASTER, baudrate=50000)
    print(i2c.scan())
    
    #CO2 sensor setup
    co2sensor = SCD30(i2c, 0x61)
    co2sensor.set_measurement_interval(5)
    
    while True:
        while co2sensor.get_status_ready() != 1:
            time.sleep_ms(200)
        print(co2sensor.read_measurement())
        time.sleep_ms(200)
    

    Error:
    Traceback (most recent call last):
    File "<stdin>", line 18, in <module>
    File "/flash/lib/scd30.py", line 95, in get_status_ready
    File "/flash/lib/scd30.py", line 161, in __read_bytes
    OSError: I2C bus error

    Library code from the stack trace:

        def __read_bytes(self, cmd, count):
            self.__write_command(cmd)
            utime.sleep_us(self.pause)
            return self.i2c.readfrom(self.addr, count)
    

    Output from code:

    setup i2c
    [97]
    (709.5289, 23.95056, 55.8548)
    (713.7881, 23.95056, 55.78003)
    (728.4892, 23.95056, 55.7251)
    (767.0008, 23.93454, 55.83038)
    (774.832, 23.89449, 55.71594)
    (785.6069, 23.87846, 55.77698)
    (778.6751, 23.86511, 55.63507)
    (776.8719, 23.83841, 55.51453)
    (775.5601, 23.85176, 55.44281)
    (771.5703, 23.80904, 55.44586)
    (769.3459, 23.79569, 55.43365)
    (763.463, 23.79569, 55.75409)
    (760.2902, 23.76631, 55.867)
    (757.4645, 23.7396, 55.93719)
    (755.4028, 23.75296, 56.0791)
    (755.1325, 23.71023, 56.2561)
    (753.7018, 23.7396, 56.26068)
    (754.1715, 23.71023, 56.10504)
    (756.3364, 23.71023, 55.93262)
    (758.8167, 23.69421, 56.31561)
    (760.5779, 23.69421, 56.59485)
    (763.1572, 23.68086, 56.74133)
    (765.9698, 23.69421, 56.62689)
    (769.019, 23.6675, 56.75201)
    (770.4882, 23.68086, 56.83899)
    (772.0147, 23.6675, 56.48193)
    (774.628, 23.6675, 56.18286)
    (778.1257, 23.6675, 56.10962)
    (778.8581, 23.65148, 56.10504)
    (780.5051, 23.6675, 56.20575)
    (782.4248, 23.68086, 56.30341)
    (784.6935, 23.65148, 56.43616)
    Traceback (most recent call last):
      File "<stdin>", line 18, in <module>
      File "/flash/lib/scd30.py", line 95, in get_status_ready
      File "/flash/lib/scd30.py", line 161, in __read_bytes
    OSError: I2C bus error
    


  • @Gijs I've build the firmware from the source and increased the timing from 10 ms to 250 ms but the result is still the same:

    code i used:

    STATIC bool mp_hal_i2c_wait_for_scl(machine_i2c_obj_t *self) {
        uint32_t count = 0;
        // clock stretching?
        while (!mp_hal_i2c_scl_read(self)) {
            ets_delay_us(2);
            if (count++ > 1250000) {       // timeout after 250ms
                return false;
            }
        }
        return true;
    }
    

    result:

    [88, 97]
    (1815.366, 23.09071, 54.73633)
    (1633.699, 23.10673, 53.06244)
    (1330.066, 23.13344, 51.31378)
    (1166.51, 23.13344, 50.28687)
    (1124.82, 23.17616, 50.61646)
    (1082.248, 23.14679, 50.34637)
    Traceback (most recent call last):
      File "<stdin>", line 18, in <module>
      File "/flash/lib/scd30.py", line 95, in get_status_ready
      File "/flash/lib/scd30.py", line 161, in __read_bytes
    OSError: I2C bus error```

  • Global Moderator

    @tiborrr said in SCD30 --> OSError: I2C bus error:

    I2C

    Hi,

    I believe you are correct. In any case, you should be able to compile your own firmware and test whether increasing the 5000 to 15000 stops the issue from occurring?
    Best



  • Could the issue be clock stretching?

    The SCD30 docs read the following:

    Clock stretching period in write- and read-frames is 30ms

    However in the source code I read that clock stretching only goes up to 10 ms
    https://github.com/pycom/pycom-micropython-sigfox/blob/605f40ff07e24683d16c28d27a4d1fd8fa6fb743/esp32/mods/machine_i2c.c#L120

    Do i understand this correctly?


Log in to reply
 

Pycom on Twitter