Skip to content

Added code to demonstrate use of new I2C timeout feature #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions libraries/Wire/examples/i2c_scanner/i2c_scanner.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,15 +31,32 @@

#include <Wire.h>

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...");
Expand Down