0

i'm trying to send multiple commands to multiple slaves controlling motors. Slave Controller(Arduino Nano) will receive the command, and returns the current position of the motors to the Master(Arduino Mega).

What data type would be best to use in this scenario. I am considering, string or byte data type.

4
  • 1
    Why are you considering string? Is it going to be read by humans? I hav not heared of I2C to human interfaces. - Imagine there was an image file format based on XML. Do you think that would be efficient?
    – Kwasmich
    Commented Nov 19, 2019 at 7:55
  • I2C is usually a one byte register address and one to four bytes of binary data (interpreted by the I2C device as a 8-bit to 32-bit bitstring). The interface is designed as a high speed, low volume, low cost command and control (master-slave) system. You should read the details at i2c.info/i2c-bus-specification
    – Dougie
    Commented Nov 19, 2019 at 8:14
  • i plan on using string, to make the command unique. but i get your point, thankyou for pointing that out.
    – Julius
    Commented Nov 19, 2019 at 13:41
  • String as in array of characters makes sense. Using a C++ String does not make sense. I'd suggest using fixed length arrays of characters without null terminators vs. null-terminated C strings however. That might make your commands easier to read. You can also use integer enum values. That would likely make for shorter byte counts, but make the data a little harder to read if you log it to the console.
    – Duncan C
    Commented Nov 19, 2019 at 18:04

2 Answers 2

2

I2C works with fixed size byte arrays.

Single bytes for commands should be sufficient. For positions you might need int16_t or even bigger numbers. Unsigned positions relative to (above) zero might make things easier, eventually (?)

As you have the same architecture on both sides, mapping of byte arrays to bigger numbers via union will work well.

1
  • ...or, assuming both sides speak the same 'protocol' you can use hton* and ntoh* to make your code architecture-independent.
    – ipmcc
    Commented Jun 11 at 9:52
1

Strings (aka byte arrays of arbitrary length) are perfect for asynchronous and full duplex protocols like UART, essentially because those are no master-slave protocols. Here, both master and slave can transmit a byte stream independent of each other at any time and with any length.

But using strings on I2C is quite uncommon. As I2C is a master-slave bus where any communication (read and write) is initiated by the master generating the bus clock, using strings has the big disadvantage, that the master has to know the number of bytes the slave has to transmit. Of course you could implement it, but as long as you don't need the data to be human-readable, I suggest the more efficient way of transmitting bytes. Usually, an i2c read transmission is done by

  1. addressing the slave in write mode
  2. Writing the target value's register address
  3. Addressing the slave in read mode
  4. Reading an arbitrary number of bytes (e.g. four bytes for float values)

A write transmission is a bit simpler:

  1. addressing the slave in write mode
  2. Writing the target value's register address
  3. Writing an arbitrary number of bytes (e.g. four bytes for float values)

This is the standard protocol that e.g. I2C sensors use and it's convenient to implement the same behaviour in the ISR when using a microcontroller (you could think of "register addresses" as "variable IDs").

To prepare values of any datatype for transmission (e.g. float to byte[4]) you can use a union.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.