I2C scan error handling



  • Hello!
    I'm trying to scan a range of I2C addresses to identify all devices on the wire.
    In the case when the device is absent, an error occurs and I try to process it.
    But it does not work and I can not determine by what address there is a device.
    In this code, I make five attempts to write. First, the console.log function works, and after it 4 errors appear (because among these addresses there is one working).
    What am I doing wrong and how I can scan addresses?

    Thank you in advance for your response.
    Best regards,
    Alexey.

    //--------------------------------------------------------------------------------------------
    //initialization I2C
    i2c.start({mode:"master", sda:3, scl:4, clock:100000});

    //iterate over range of I2C addresses
    for (i = 115; i <= 119; i++) {
    try{
    i2c.write(i, [0x00]);
    console.log(i +' done');
    }
    catch(e){
    console.log('addr_miss '+ i) ;
    };
    };
    //--------------------------------------------------------------------------------------------
    0_1552999165069_i2c_1.JPG
    😿



  • Hi @Alexey ,

    Some module handle errors on onerror functions.
    i2c is also.

    Please try this code.

    i2c.start({mode:"master", sda:3, scl:4, clock:100000});
    i2c.onerror = (err)=>{
      console.log("error occured" + err);
     }
     i2c.write(0x73, [0x00]);
              
    


  • @kido Thank you for helping!
    I tried use "onerror", and that helping in "hide" error of I2C communication.
    But i try to call for all addresses of I2C diapason, and save all of them whom responded.
    For that i use a cycle, wherein i making a successively write and read for I2C address.
    I can't do it yet. "Onerror" don't work in cycle, how i see it.

    I also encountered a strange work of I2C and cannot understand why this is happening.
    If I perform two write operations in a row, then the time between them is short. But if I use write-read-write, the second record will be executed only after ~ 500 ms after reading.
    0_1555938060199_1.png



  • @Alexey Hello,

    Is reading data successful?
    If the strange behavior is only just delay of communication, the firmware on obniz is suspicious. I need to investigate.

    "500 msec" seems something timeout. waiting response or something.
    What is the device you are trying to connect to?


Log in to reply
 

SUGGESTED TOPICS

  • 1
  • 7
  • 2
  • 5
  • 2
  • 2
  • 4
  • 3