Shop OBEX P1 Docs P2 Docs Learn Events
I2C devices with identical addresses — Parallax Forums

I2C devices with identical addresses

Paul MPaul M Posts: 95
edited 2012-07-30 19:47 in General Discussion
I'm doing a project where I have to read 4 pressure sensors with an I2C bus. The sensors all have the same address and they do not have any external address lines so I can't put them all on the same bus. To save some I/O lines I thought about using a common SCL and separate SDA lines to each sensor.
Will this work? Also, as long as I monitor the SCL line (in case one slave is slower and holds the line low) can I read the sensors simultaneously?

Paul

Comments

  • MacTuxLinMacTuxLin Posts: 821
    edited 2012-07-26 06:10
    Alternatively, if it is possible in your project, you could use an 8-Channel I2C Switch like TI's PCA9548A. Using 2 pin on Prop to control 8 I2C devices. Just a thought....
  • tdlivingstdlivings Posts: 437
    edited 2012-07-26 07:40
    Take a look at the method used here using an hc138
    http://learn.adafruit.com/delorean-time-circuit/circuit-trickery

    Tom
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-07-26 07:49
    I'm not sure it the other guys answered your questions (though they did give good replies).
    Paul M wrote: »
    Will this work?

    Yes.
    Paul M wrote: »
    Also, as long as I monitor the SCL line (in case one slave is slower and holds the line low) can I read the sensors simultaneously?

    Again, I think this is a yes. Does your sensor use clock stretching? With all the I2C devices I've used (not a whole lot), only the master controls the clock. I have read about clock stretching, but I don't see why you wouldn't be able to read from all devices at once (each on with their own SDA lines) with the precautions you mentioned.

    It's relatively common on the forum for people to read from multiple SPI devices (each with a separate data line) at once.
  • Paul MPaul M Posts: 95
    edited 2012-07-26 09:12
    Thanks everyone. Useful links filed for future reference.
    I'll go without any extra IC's as I can then easily modifiy an existing pcb design.
  • StephenMooreStephenMoore Posts: 188
    edited 2012-07-26 20:41
    I've got the same problem with the compass units all having the same address. Please let us know how it goes.

    sm
  • jmgjmg Posts: 15,190
    edited 2012-07-26 23:32
    Paul M wrote: »
    I'm doing a project where I have to read 4 pressure sensors with an I2C bus.

    Depends on what you want the wiring to look like.
    If you can tolerate running a cable to each sensor, and have the pins, then the split bus you describe would work.
    You could use separate SCL lines to make it more fail-safe. OR' ing SCL might save pins, but it loads one pin more, and gives a single point of failure, and is not easy to fault find. "All sensors dead" messages ?

    If you want a single cable to do all sensors, then you will need something small at the remote end, with a i2c address,
    something like a PCA9542 has 8 ID's itself, so one of those per sensor, would allow a single cable, up to 8 (16?) sensors
  • StephenMooreStephenMoore Posts: 188
    edited 2012-07-28 00:05
    I gave it a try with four HMC5883L compass units sharing one I2C SCL line and each having their own SDA pins. It works fine: I do not need the maximum refresh rate. In fact I do not have the RDY pin wired at all for any of the units.

    sm
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-07-28 18:59
    What you propose will work. Just be sure to issue a STOP condition before switching to a different device, and leave unused devices' SDA lines pulled high when not in use.

    -Phil
  • StephenMooreStephenMoore Posts: 188
    edited 2012-07-29 23:27
    After more testing I have a question about my compass program. I start a cog that continuously writes data to a hub array at compassData.

    Do I need to use a LOCK when reading from another cog?
    PRI  dataLoop(compassData) | idx
    
    
    waitcnt(clkfreq*4+cnt)
    pst.str(string("x     y     z"))                              
    repeat
      pst.position(0,1)
     waitcnt(clkfreq/2  + cnt) 
      repeat idx from 0 to 3       'get measurements from each sensor in array
        datapin  :=  idx + 1
        clockpin :=  0 
       setpointer(OUTPUT_X_MSB)         'Start with Register OUT_X_MSB    
       getRaw                           'Gather raw data from compass
       long[compassData][idx * 3]     := x
       long[compassData][idx * 3 + 1] := y
       long[compassData][idx * 3 + 2] := z
       pst.dec(x)
       pst.str(string("    "))
       pst.dec(y)
       pst.str(string("    "))
       pst.dec(z)
       pst.str(string("    "))
       pst.char($0D)
    

    Regards,

    sm
  • msrobotsmsrobots Posts: 3,709
    edited 2012-07-30 01:32
    Hi Stephen,

    I try to explain:

    Locks are there to syncronize concurrent access between cogs. They do not really lock anything. They are just 'Flags' so your software can wait for the other cog to finish something. If you need them depends - hm - on your needs ....

    Hub-Access is round robin, max one long each cog. So writing a long is 'atomic' and does not get interrupted by some other cog. But when your cog 1 writes 3 longs after each other it will take 3 hub-cycles and the cog 2 might read one of them inbetween.

    In case of your compass-example this will not matter at all - so you do NOT need locks.

    If you - lets say - have some serial driver writing bytes into hub-ram (taking one hub-cycle per byte) and you are transferring 4 bytes representing a long - then you may get in trouble when the second cog reads that long while it is getting changed by the first cog, writing one byte after the other.

    Then you need a lock so the second cog waits until all 4 bytes are written, to make sure you are not reading a 'incorrect' long, half the new value and half the old one or something like that.

    Locks are very useful to mark resources as 'temporary blocked' but in fakt you will use them very rarely.

    hope this helps

    Enjoy!

    Mike
  • StephenMooreStephenMoore Posts: 188
    edited 2012-07-30 19:47
    Yes. That helps. I really am writing only one long at a time and reading one long at a time from the other cog.

    Many thanks,

    sm
Sign In or Register to comment.