I2C scanner ?

BS2p has this I2CIN and I2COUT set of commands that might help to interface the I2C devices.
I wonder if anybody came up with a code that figures out the addresses of what's been connected to the bus.

Sort of like a "I2C scanner"


  • 5 Comments sorted by Date Added Votes
  • Shouldn't be hard to scan through all the addresses to see which ones return an ACK, but I'm not sure how difficult it would be to identify the chip at that address.
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.
  • I made one in Spin, but it should be easy to transcribe:
    PUB WhoOnBus(ptrOnBusArr) | onbus, addr                             'Fills an array with max 119 elements with addresses that get a response
                                                                        'and writes how many is onbus to the 0th element
       onbus:= 1
       REPEAT addr FROM %0000_1000 TO %0111_0111                        'Scan the entire address space, exept for reserved spaces
         IF CallChip(addr << 1)== ACK                                   'If a chip acknowledges,
           LONG[ptrOnBusArr][onbus]:= addr                              'put that address in the callers array
           LONG[ptrOnBusArr][0]:= onbus                                 'and update the total count of chips on the bus
           IF onbus> 119                                                'until loop expires or maximum number of elements in the array is reached
         Stop                                                           'After each call send a stop signal to avoid confusion

    21st century - when everything changes
    "Better with a DAT and a COG than with a CAT and a DOG"
  • Erlend, don't you need to put some provision for time gap expired while waiting for ACK?
  • True, but I2C is so fast, and Spin is so comparatively slow, that the few executions in the loop is sufficient. Same with any intrepeted lanuage I would think.

    21st century - when everything changes
    "Better with a DAT and a COG than with a CAT and a DOG"
  • Tracy AllenTracy Allen Posts: 5,932
    edited February 11 Vote Up0Vote Down
    While it is not documented, the I2CIN and I2COUT commands will themselves time out if they do not receive the expected data. I forget the exact duration. Unlike SERIN there is no provision for a timeout vector, and there is no error code associated with a timeout. It is easy enough to test for i2c inputs that you expect to be present by reading a register for valid response. However, just to say "hello" to an arbitrary address, I think maybe the only thing to go on would be the timing of the response, and the Stamp does not have a built-in timer to support that. Good question.
Sign In or Register to comment.