// The pins SDA and SCL are defined and are the pins of the I2C bus. // The weak internal pullup resistor should be enough to make it high. // A delay is added to give it time to go high with a weak internal pullup resistor. // A byte needs 9 clock pulses, to be sure 20 clock pulses are given. // While keeping the SDA high, a rising SCL is also a STOP on the bus. // The SCL is never made a hard HIGH for safety, it toggles as if it was a open-collector output. // Check if SDA or SCL is stuck to GND #include void setup() { Serial.begin(9600); Serial.println("START"); pinMode( SDA, INPUT_PULLUP); pinMode( SCL, INPUT_PULLUP); delay( 1); if ( digitalRead( SDA) == LOW) Serial.println( "Error, SDA is low"); if ( digitalRead( SCL) == LOW) Serial.println( "Error, SCL is low"); // Check if SDA and SCL are shortcut to each other digitalWrite( SCL, LOW); pinMode( SCL, OUTPUT); delay( 1); if ( digitalRead( SDA) == LOW) Serial.println( "Error, SDA is shortcut to SCL"); pinMode( SCL, INPUT_PULLUP); // Give clock pulses while keeping SDA high. for (uint8_t i = 0; i < 20; i ++) { digitalWrite( SCL, LOW); // preset output LOW, also disable internal pullup resistor pinMode( SCL, OUTPUT); // OUTPUT and also a hard LOW delay( 1); pinMode( SCL, INPUT_PULLUP); // INPUT with weak internal pullup resistor. delay( 1); } // Final check, there is no more that we can do. if ( digitalRead( SDA) == LOW || digitalRead( SCL) == LOW) Serial.println( "Error, I2C bus is not okay"); Wire.begin(); } void loop() { }