P2 I2C compatibility issue

Hi all, I am having a strange issue with I2C code compatibility between Argon and P2 devices. I have been using an I2C scan function for development based on a code snippet from the Arduino project hub:

int i2c_scan(String command)   {
  byte error, address; //variable for error and I2C address
  int nDevices;


  nDevices = 0;
  for (address = 1; address < 127; address++ )
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    error = Wire.endTransmission();

    if (error == 0)
      Serial.print("I2C device found at address 0x");
      if (address < 16)
      Serial.print(address, HEX);
      Serial.println("  !");
    else if (error == 4)
      Serial.print("Unknown error at address 0x");
      if (address < 16)
      Serial.println(address, HEX);
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");

  delay(5000); // wait 5 seconds for the next I2C scan
  return nDevices;

This works reliably, as expected on Argon hardware, showing the addresses of all connected devices. On P2 hardware, however, it incorrectly detects devices on every single I2C address. Observing the I2C pins with an oscilloscope shows no activity on SDA or SCL during the complete scan.

The I2C port is working on the P2. I can toggle GPIO on I2C expanders, read temp sensors, etc. It is just the scan function that doesn’t work. Is there an alternative method I can use for I2C device scanning ? I am also worried that this apparent change in functionality might cause other issues with I2C bus error detection and recover.

@jcsparky, I’m going to ask the obvious but do you have pull-up resistors on SDA and SCL and do you have a Wire.begin() somewhere in your code before calling the i2c_scan() function?

@peekay123 thanks for your quick response. Yes I do have pullups on both lines and also do have a Wire.begin() as part of the setup loop. My current test code has one particle function for the i2c scan and another to write to an I2C gpio expander output to toggle a LED (using the Wire lib). The gpio LED toggle function works and the corresponding SDA / SCL waveforms look very. Conversely running the i2c scan does not generate any I2C activity.

I have acutally built a couple of Featherwing compatible P2 boards, plugged into a breakout board so can quickly swap from an Argon hardware to P2 hardware with the only real change being the Particle processor.

@jcsparky, I’ll tested the i2c_scanner on my P2 dev board with two Adafruit featherwing I2C boards and it found both. Have you tried running the i2c scanner code as the only compiled code on the device to see if that works?

@peekay123 I have just tried the i2c scan as the only code and the result was the same for me, ie. no I2C output but devices detected at every address:

I2C device found at address 0x01 !
I2C device found at address 0x02 !
I2C device found at address 0x03 !
I2C device found at address 0x04 !
I2C device found at address 0x05 !
I2C device found at address 0x06 !

I2C device found at address 0x7B !
I2C device found at address 0x7C !
I2C device found at address 0x7D !
I2C device found at address 0x7E !
I2C device found at address 0x7F !

By slightly changing the i2c scan code I could generate activity on the lines:

No activity:

error = Wire.endTransmission();

Standard i2c activity (although still incorrectly detecting i2c devices where none are present):

error = Wire.endTransmission();

What version of Device OS are you targeting? This was a bug prior to 5.0.0 so if you are using an older version of Device OS, that would definitely occur.


@rickkas7 thank you. That was it. Changed my code to target 5.1.0 and it now works, only detecting connected devices. Hopefully I won’t see any other issues with targeting a prerelease firmware, but so far everything working as per my reference Argon hardware.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.