diff --git a/src/SparkFun_SCD30_Arduino_Library.cpp b/src/SparkFun_SCD30_Arduino_Library.cpp index a537db7..e1246a4 100644 --- a/src/SparkFun_SCD30_Arduino_Library.cpp +++ b/src/SparkFun_SCD30_Arduino_Library.cpp @@ -40,6 +40,23 @@ boolean SCD30::begin(TwoWire &wirePort) //We expect caller to begin their I2C port, with the speed of their choice external to the library //But if they forget, we start the hardware here. _i2cPort->begin(); + + /* Especially during obtaining the ACK BIT after a byte sent the SCD30 is using clock stretching (but NOT only there)! + * The need for clock stretching is described in the Sensirion_CO2_Sensors_SCD30_Interface_Description.pdf + * + * The default clock stretch (maximum wait time) on the ESP8266-library (2.4.2) is 230us which is set during _i2cPort->begin(); + * In the current implementation of the ESP8266 I2C driver there is NO error message when this time expired, while + * the clock stretch is still happening, causing uncontrolled behaviour of the hardware combination. + * + * To set ClockStretchlimit() a check for ESP8266 boards has been added in the driver. + * + * With setting to 20000, we set a max timeout of 20mS (> 20x the maximum measured) basically disabling the time-out + * and now wait for clock stretch to be controlled by the client. + */ + + #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) + _i2cPort->setClockStretchLimit(200000); + #endif //Check for device to respond correctly if(beginMeasuring() == true) //Start continuous measurements