diff --git a/libraries/Wire/examples/i2c_scanner/i2c_scanner.ino b/libraries/Wire/examples/i2c_scanner/i2c_scanner.ino index 3febbf441..8029c45eb 100644 --- a/libraries/Wire/examples/i2c_scanner/i2c_scanner.ino +++ b/libraries/Wire/examples/i2c_scanner/i2c_scanner.ino @@ -21,6 +21,8 @@ // A sensor seems to use address 120. // Version 6, November 27, 2015. // Added waiting for the Leonardo serial communication. +// Version 6, July 10, 2020 by G. Frank Paynter +// Demonstrates the use of the new Wire library timeout feature // // // This sketch tests the standard 7-bit addresses @@ -29,15 +31,32 @@ #include +uint16_t wireTimeoutCount; //capture I2C bus timeout events + void setup() { Wire.begin(); + #if defined(WIRE_HAS_TIMEOUT) + // Prevent lockups on bus problems by aborting after a timeout + Wire.setWireTimeout(3000, true); //timeout value in uSec, true to reset I2C bus on timeout + wireTimeoutCount = 0; + #endif + Serial.begin(9600); while (!Serial); // Leonardo: wait for serial monitor Serial.println("\nI2C Scanner"); } void loop() { + #if defined(WIRE_HAS_TIMEOUT) + if (Wire.getWireTimeoutFlag()) + { + wireTimeoutCount++; + Wire.clearWireTimeoutFlag(); + Serial.print("Wire timeout detected; count now "); Serial.println(wireTimeoutCount); + } + #endif + int nDevices = 0; Serial.println("Scanning...");