1

How could I fix this runtime error? I'd like it to skip the reading and carry on with the program if it gets an error but it stops the program

I've got the Adafruit SCD-30 Co2 sensor and when reading the data using their example code I get this error

Traceback (most recent call last):
File "pm.py", line 43, in <module>
print("CO2:", scd.CO2, "PPM")
File "/usr/local/lib/python3.7/dist-packages/adafruit_scd30.py", line 183, in CO2
self._read_data()
File "/usr/local/lib/python3.7/dist-packages/adafruit_scd30.py", line 246, in _read_data
raise RuntimeError("CRC check failed while reading data")
RuntimeError: CRC check failed while reading data

This is the line it fails on

    print("CO2:", scd.CO2, "PPM")

This is the crc checking section of their code

    def _read_data(self):
    self._send_command(_CMD_READ_MEASUREMENT)
    with self.i2c_device as i2c:
        i2c.readinto(self._buffer)

    crcs_good = True

    for i in range(0, 18, 3):
        crc_good = self._check_crc(self._buffer[i : i + 2], self._buffer[i + 2])
        if crc_good:
            continue
        crcs_good = False
    if not crcs_good:
        raise RuntimeError("CRC check failed while reading data")

    self._co2 = unpack(">f", self._buffer[0:2] + self._buffer[3:5])[0]
    self._temperature = unpack(">f", self._buffer[6:8] + self._buffer[9:11])[0]
    self._relative_humidity = unpack(
        ">f", self._buffer[12:14] + self._buffer[15:17]
    )[0]

Is there a way to tell it to do something like this

    if not crcs_good:
        skip reading and wait for the next one
2
  • have you looked at the ty, except pattern?
    – itprorh66
    Apr 23, 2021 at 20:18
  • yes, it still does it.
    – Lewis
    Apr 24, 2021 at 16:53

1 Answer 1

1

I was running into the same issue. I finally figured out that I needed to slow down my clock speed on my I2C on my raspberry pi. Not sure if you are using a pi as well but here's a link to a good write up on how to do it https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/i2c-clock-stretching . I am running the sensor at 10khz or 10000hz and haven't had a crc error crash my code since.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.