agners / micropython-scd30

MicroPython I2C driver for SCD30 CO2 sensor module
MIT License
24 stars 15 forks source link

OSError: I2C bus error with example #3

Open tiborrr opened 4 years ago

tiborrr commented 4 years ago

This error occurred on a Pycom Wipy

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)

Results in the following output:

[97]
(655.4566, 23.21889, 56.63605)
(653.791, 23.24559, 56.63147)
(653.7228, 23.27497, 56.55975)
(654.9095, 23.28832, 56.5094)
(655.1393, 23.31503, 56.50024)
(656.0949, 23.28832, 56.49567)
(656.3989, 23.28832, 56.56281)
(655.6221, 23.30168, 56.47736)
(654.5003, 23.28832, 56.37054)
(654.9923, 23.28832, 56.43463)
(654.4737, 23.24559, 56.51245)
(653.8462, 23.24559, 56.53229)
(653.3164, 23.27497, 56.52618)
(652.7819, 23.27497, 56.53687)
(651.5202, 23.27497, 56.58112)
(651.8386, 23.25895, 56.56738)
(651.4281, 23.24559, 56.55518)
(652.6042, 23.24559, 56.5033)
(661.3499, 23.25895, 57.32422)
(667.6893, 23.24559, 56.85577)
(674.1215, 23.24559, 56.63147)
(682.467, 23.21889, 56.53076)
(677.9596, 23.24559, 56.4682)
(676.1273, 23.25895, 56.53534)
(673.8711, 23.24559, 56.40411)
(673.1847, 23.27497, 56.37665)
(669.824, 23.25895, 56.27899)
(667.0184, 23.24559, 56.30798)
(664.3038, 23.24559, 56.39343)
(662.7124, 23.23224, 56.50024)
(660.7636, 23.25895, 56.4621)
(658.1758, 23.25895, 56.4621)
(656.9217, 23.25895, 56.4621)
(656.0261, 23.23224, 56.45752)
(655.1484, 23.25895, 56.4621)
(655.5229, 23.27497, 56.48499)
(655.0932, 23.25895, 56.3858)
(655.0857, 23.25895, 56.34308)
(654.7072, 23.24559, 56.44836)
(656.6269, 23.21889, 56.48651)
(659.3814, 23.21889, 56.71082)
**Traceback (most recent call last):
  File "<stdin>", line 19, in <module>
  File "/flash/lib/scd30.py", line 95, in get_status_ready
  File "/flash/lib/scd30.py", line 161, in __read_bytes**
kertxo commented 3 years ago

I have the same problem.

The error is: OSError: [Errno 110] ETIMEDOUT

As described here: [https://forum.micropython.org/viewtopic.php?f=14&t=9547]

The board is an ESP32 Dev KITv1

McSpider commented 3 years ago

I have encountered the same error and this issue and the aforementioned forum post are the only references to it.
After some poking around I found this note in the SCD30 interface specification pdf:

Maximal I2C speed is 100kHz and the master has to support clock stretching. Sensirion recommends to operate the SCD30 at a baud rate of 50 kHz or smaller. Clock stretching period in write-and read-frames is 30ms, however, due to internal calibration processes a maximal clock stretching of 150ms may occur once per day.

Running the I2C bus with freq=40000, timeout=151000 got rid if all timeout errors for me.

Note that the timeout parameter is only valid for the SoftI2C driver. (If your using a ESP8266, machine.I2C actually aliases machine.SoftI2C) Additionally, I believe having a timeout of 151ms may trigger your watchdog timer if your are using one.

tiborrr commented 3 years ago

Will definitely try this tomorrow.

kertxo commented 3 years ago

I've been testing this for the last 10 days:

SoftI2C with freq=40000, timeout=50000

SCD30 init with pause=2000

With this config there are no problems for the moment

tiborrr commented 3 years ago

For me setting the baud rate back to 40.000 has no effect. I now just catch the error and then continue, which is not optimal, but works.

This is on the Gpy from Pycom

McSpider commented 3 years ago

Ah that's a shame. Just to confirm, you did try changing the timeout to 151000us with a SoftI2C object? (151ms, default is 255us) I believe that to be the important part, specifically to get it to work with the extended Clock Stretching period.

tiborrr commented 3 years ago

I'd love to will it not that Pycom firmware does not support a timeout in its I2C object.

https://docs.pycom.io/firmwareapi/pycom/machine/i2c/

I thought it had something to do with clock stretching... You can see the following issue I posted on this: https://github.com/pycom/pycom-micropython-sigfox/issues/462