Multiple i2c addresses

Majority of i2c devices have a main address and then address of registers that store data.

I want to create an arduino i2c slave with the same config.

How can I have an arduino slave with multiple addresses each storing different values.

Thanks.

I've been playing with this for a while. Maybe these will help you. The master sends the slave a number from 0 to 3 over and over. The slave returns 2 byte registers from the "array" array.

I2C Master

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop() {
  Wire.beginTransmission(13); // transmit to device #13
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting

  delayMicroseconds(100);
  
  if(Wire.requestFrom(13, 2) > 0) {    // request 2 bytes from slave device #13
    while(Wire.available())    // slave may send less than requested
    { 
      char c = Wire.read(); // receive a byte as character
      Serial.print(c,DEC);         // print the character
      Serial.print(" ");
    }
    Serial.println();
  }
  else {
    Serial.println(F("Fail"));
  }

  x++;

  if(x > 3) x = 0;
  delay(500);
}

I2C Slave

#include <Wire.h>

// these are the "registers"
byte array[8] = {2,4,6,8,10,12,14,16};
volatile byte regReq = 0;
volatile byte* rtnPtr;
 
void setup()
{
  Serial.begin(115200);
  Wire.begin(13);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}

void loop()
{

}

void receiveEvent(int byteCount)
{
  regReq = Wire.read();
  rtnPtr = &array[regReq*2];    
}

void requestEvent()
{
  Wire.write((byte*)rtnPtr,2);
}

Hmm.. interesting method.
But i was looking for more like a one way communication, like in a mouse without the master "requesting" any thing, it just reads said registers.

ric96:
Hmm.. interesting method.
But i was looking for more like a one way communication, like in a mouse without the master "requesting" any thing, it just reads said registers.

How would your slave arduino know which "register" to send?

ric96:
Hmm.. interesting method.
But i was looking for more like a one way communication, like in a mouse without the master "requesting" any thing, it just reads said registers.

That's not how it works. You will have to send requests from the master to the slave. If you don't want to send requests from the master, I2C is not the right 'tool'.

You can obviously swap the functionalities around so what you call master now will be slave and vice versa and the new master just spits out data to the slave.

sterretje:
That's not how it works. You will have to send requests from the master to the slave. If you don't want to send requests from the master, I2C is not the right 'tool'.

You can obviously swap the functionalities around so what you call master now will be slave and vice versa and the new master just spits out data to the slave.

What should i use then?

Depends, what EXACTLY are you trying to do?

Typical XY problem