confused about i2c address and data

Hello,

I have a setup where I am connecting with arduino mega with intel edison-arduino breakout board with i2c for master-slave communication. I have nodejs on edison and arduino code on mega running. It works fine. but from the code Wire.begin(8) I dont quite understand this address 8.

Can it be something else?
If so can i have multiple addresses in my setup for communicating in one code? (what i mean is like one
address to control one output)
If not then how could I send data of multiple output in slave from master?
Also I see that at the moment I can send some characters/strings. Can I send more data like an array or object?

master code:

var five = require("johnny-five");
var Edison = require("edison-io");
var board = new five.Board({
  io: new Edison()
});

board.on("ready", function () {
  var write = (message) => {
    this.i2cWrite(0x08, Array.from(message, c => c.charCodeAt(0)));
  };
  this.i2cConfig();
  this.repl.inject({ write });

  write("Hello World");
});

slave code:

#include <Wire.h>

const int maxlength = 64;

char buffer[maxlength];
char printable[maxlength];

int received = 0;

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (received > 0) {
    memcpy(printable, buffer, maxlength);

    for (int i = 0; i < received; i++) {
      Serial.print(printable[i]);
      if(printable[i]=='1'){
        digitalWrite(LED_BUILTIN, HIGH);
      }
      if(printable[i]=='0'){
        digitalWrite(LED_BUILTIN, LOW);
      }
    } 
    received = 0;
  }
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  received = howMany;
  memset(buffer, 0, maxlength);

  for (int i = 0; i < howMany; i++) {
    buffer[i] = Wire.read();
  }
}

I am new at this. I tried to look up in the web for answers but I couldn’t find something that was understandable for me, I just lack education on this things. If someone could kindly explain or provide useful sources it would be awesome! Thank you :slight_smile:

Your slave can basically have any address; there are a few reserved ones if I'm not mistaken (0 and 127 ?).

So you can have multiple slaves, each with their own address. wire.begin(8) just tells that slave that it's address is 8 and it will react on packets with that address and ignore other packets.

Let us recall few definitions of I2C Bus Protocol where, we hope, you find the answers of your queries/questions.

1. There is a Master (simple case; we don't talk about mult-master), and there can be as many as 127 slaves connected with the Master over the single I2C Bus.

2. every slave has a 7-bit address called slave address/device address. the address range is 0b0000001 to 0b111111 (0x01 - 0x7F). The address 0x00 is a reserved address which should not be used for the address of a slave.

3. In the slave codes, you have the definition like : Wire.begin(8). It is actually --
Wire.begin(slaveAddress);
===> wire.begin(0x08);

It means that a I2C bus is formed and the the MEGA will be seen by a Master at the address 0b0001000 (0x08).

4. If we declare in the master code as: Wire.begin(0x07), it will mean that the master can also be addressed as a slave at this address (0x07) by another Master (if any). In your case, it is possible for the MEGA to acquire I2C Bus, and it can work as a Master to address Edison-Arduino Board.

5. You can connect 5 or more Arduino Boards or sensors/devices like RTC, 24C512, BME280, and etc. with the I2C Bus. The Master will be able to communicate with each of them as they have separate slave addresses.

If you want to know more about I2C bus, do not hesitate to ask.

  1. If we declare in the master code as: Wire.begin(0x07), it will mean that the master can also be addressed as a slave at this address (0x07) by another Master (if any). In your case, it is possible for the MEGA to acquire I2C Bus, and it can work as a Master to address Edison-Arduino Board

DON'T DO THAT

The main Arduino I2C library cannot be both master and slave. Giving it an address makes it a slave.

If multi-master was supported then this would be the way to do it. But it isn't.

GolamMostafa,
You are wrong about the reserved addresses.
0 to 7 reserved.
https://www.i2c-bus.org/addressing/

The reason the Wire slave example now uses 8 is because I pounded on the Arduino team for a while to get them to use an address that was not in the reserved address space. They finally fixed the example to use the first address that was not reserved which was 8.

--- bill

GolamMostafa:
To operate the following setup as Master/Slave (UNO) and Master/Slave(NANO) with slave addresses 0x05 and 0x06 respectively, I have not faced any complain neither from the IDE nor from the Wire.h Library. I would appreciate to receive comments as to justify the validity of the setup and its functional behavior.

Have a read of the i2c spec. Addresses 0 to 7 are reserved.
Just because the AVR 2wire h/w and Arduino Wire library happen to allow and work with reserved i2c addresses does not change the fact they are reserved and should not be used.
Other hardware and s/w, may not work with those reserved i2c addresses since they may be more compliant with the i2c specification.
For example, addresses under 8 will not work on the pic32 and associated s/w in the Arduino chipkit core,

It is never a good idea to knowingly try to use or do things that violate a specification.

--- bill

It is never a good idea to knowingly try to use or do things that violate a specification.

This is absolutely correct. I have noted it down. I knew from the ATmega32A data sheets that the 0x00 address has been reserved as the general call address, and there are 127 slave devices that can be connected with the I2C Bus.

Thanks for providing inspiration to remain motivated with the latest information/specifications.