I2C - communication between Arduino Uno, and Arduino Mega connected to a sensor

Hi,

I do not receive the same data that I send.

Here is my slave code:

#include <Wire.h>

void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(57600);  // start serial for output
  Serial.flush();
  
}


void loop() {
  byte i2cAddress1 = 0x04; // Slave address (SingleTact), default 0x04
  short data1 = readDataFromSensor(i2cAddress1);
   

  Serial.println(data1);

  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(data1); 
  Wire.endTransmission();
   
  delay(100); // Change this if you are getting values too quickly 
}

short readDataFromSensor(short address)
{
  byte i2cPacketLength = 6;//i2c packet length. Just need 6 bytes from each slave
  byte outgoingI2CBuffer[3];//outgoing array buffer
  byte incomingI2CBuffer[6];//incoming array buffer


//Read Request Operation, master write to slave
  outgoingI2CBuffer[0] = 0x01;//I2c read command
  outgoingI2CBuffer[1] = 128;//Slave data offset (Read offset in register block)
  outgoingI2CBuffer[2] = i2cPacketLength;//require 6 bytes (number of byte to read)

  Wire.beginTransmission(address); // Send a request to begin communication with the device at the specified address
  Wire.write(outgoingI2CBuffer, 3);// send out command
  byte error = Wire.endTransmission(); // stop transmitting and check slave status
  if (error != 0) return -1; //if slave not exists or has error, return -1

//this now reads the pression
  Wire.requestFrom(address, i2cPacketLength);//require 6 bytes from slave

  byte incomeCount = 0;
  while (incomeCount < i2cPacketLength)    // slave may send less than requested
  {
    if (Wire.available())
    {
      incomingI2CBuffer[incomeCount] = Wire.read(); // receive a byte as character
      incomeCount++;
    }
    else
    {
      delayMicroseconds(10); //Wait 10us 
    }
  }

  short rawData = (incomingI2CBuffer[4] << 8) + incomingI2CBuffer[5]; //get the raw data

  return rawData;
}

Here is my master code

#include <Wire.h>

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

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int bytes) {

   if(Wire.available() != 0)
  {
    for(int i = 0; i< bytes; i++)
    {
      short x = Wire.read();
      Serial.println(x); 
    }
  }


}

This is te data that I receive (Arduino Uno, master): 1 1 1 2 2
and this is the data that I send (Arduino Mega, Slave) : 260 258 260 258 258

Thank you

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int bytes) {

   if(Wire.available() != 0)
  {
    for(int i = 0; i< bytes; i++)
    {
      short x = Wire.read();
      Serial.println(x);
    }
  }

The receiveEvent() routine is called in interrupt context and inside interrupt context you must not call the Serial object because it also depends on interrupts which are disabled in interrupt context.

You did misunderstood the roles of master and slave. The master is commanding the bus and it’s the Arduino which runs the first code. The Arduino which runs the second code is the slave.

  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(data1);
  Wire.endTransmission();

Wire.write() sends 1 Byte! If you need to send objects or values that have more than 8 bits you have to divide them into individual bytes and reconstruct their values on the receiver side.