Managing communication on i2c

Hi I was just wondering how people handle communication over i2c when you have multiple devices… How do you ensure writes reads are queued and executed at the right time? I was thinking of extending my servo project to include a colour LCD and some leds

You first send the address of the device you want to talk to, talk, finish and mive to the next device.

I’m not aware of an async I2C protocol.

Thanks Scruffr what about handling a read op I.e. say from multiple servo positions

I don’t quite understand.
The I2C communication gets initiated form the MCU side and not from the device.
If your device wants the MCU to initiate such communication you’d use an interrupt or poll for some event that tells the MCU that it’s time to.

Normally your I2C device has a 7bit address, the last bit indicates if you are starting a read or write op.

As @ScruffR notes I2C is always initiated from the master.
IO devices like MCP23017 has interrupt pins that can be used to let the master know a pin changed, and it needs to reread them.

I think the scenario I was thinking about was what if I have a listener and 2 events come in at roughly the same time.

The listener action might be to drive a number of servos depending on the incoming request.

Say servo 1 is currently moving (I assume it’s reporting it’s position back over i2c at the time)
and a request comes in to start running servo 2… do I need to be queuing that request until servo 1 has completed it’s rotation?

Are you using some sort of specialty or continuous rotation servos? Because the typical small hobby-type servos don’t work that way. They’re continuously sent a PWM signal that indicates the angle rotate to based on the pulse width and automatically position themselves there. In the case of an I2C PWM driver, you just send the I2C command set the pulse width for each servo’s PWM driver and that’s it. While your case may very well may be more complicated, I thought I’d mention it in case someone finds this post in a search because the common case is usually much simpler.

2 Likes

No, this would mean the slave was in charge of the communication while moving, but it's not.

But instead of guessing what's going on, what servo and servo controler are you using?
Any datasheets, command tables, communication guidelines for it?

1 Like

I’m going to run 5 servos with a pca9685 and am thinking I will also try and run an LCD as well and was watching a meetup video where the speaker said that if you have multiple devices on i2c you have to be cautious that read writes don’t clash between multiple devices on the bus and prevent it… It was a general comment so ib thought I’d ask here what people usually do…

I wonder what he actually meant with that.
OK, in a multi master setup or multi threaded system this might be of concern, but for that reason the want-to-be master has the possibility to watch the CLK line for any ongoing communication by another master/thread and if the bus is free, signal a bus busy by pulling CLK low for some minimum time (can’t quite remember how long) before he initiates his conversation (this would even force any slave that’d be going to send data to back off).

But I guess you are not doing such “exotic” things, so you should be save.

BTW: The controler you intend to use does not really tell you the position of the servos, but only takes the desired position (PWM ratio) from you in an instant and then keeps sending that signal to the servo which just moves accordingly and stays there as long as that PWM ratio stands.