I2C

HI

---------------------MASTER-------------------

#include <Wire.h>
int volt;
int volt1;
void setup()
{

Wire.begin();
Serial.begin(9600);
}

void loop()
{

Wire.beginTransmission(5);
int available = Wire.requestFrom(5,2);

if (available == 2)
{
volt = (Wire.read() << 8)| (Wire.read());
Serial.print("volt =");
Serial.println(volt);
}

Wire.endTransmission();
delay (100);
Wire.beginTransmission(5);
available = Wire.requestFrom(5,2);

if (available == 2)
{
volt1 = (Wire.read() << 8)| (Wire.read());
Serial.print("volt1 =");
Serial.println(volt1);
}

Wire.endTransmission();
delay(1000);
}

-------------------------------------SLAVE--------------------------------

#include <Wire.h>
int volt;
int volt1;
byte c[2];
byte b[2];
void setup() {
Wire.begin(5);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}

void loop() {

//volt = analogRead(0);
//Serial.println(volt);
//delay(1000);
}
void requestEvent()
{
volt = analogRead(0);
Serial.println(volt);
c[0] = highByte(volt);
c[1] = lowByte(volt);
Wire.write(c,2);
delay (100);
volt1 = analogRead(1);
Serial.println(volt1);
b[0] = highByte(volt1);
b[1] = lowByte(volt1);
Wire.write(b,2);
delay(100);
}

Values are wrong when read

Does someone can help

thanks

Have you looked at the slave sender and master reader examples that come with the Wire library? Please read the "how to use the forum" and "how to ask a programming question" stickies. You will see how to properly format and post code (without :slight_smile: ).

sorry

-------------------master----------------
#include <Wire.h>
int volt;
int volt1;
void setup()
{

Wire.begin();
Serial.begin(9600);
}

void loop()
{

Wire.beginTransmission(5);
int available = Wire.requestFrom(5,2);

if (available == 2)
{
volt = (Wire.read() << 8 ) | (Wire.read());
Serial.print("volt =");
Serial.println(volt);
}

Wire.endTransmission();
delay (100);
Wire.beginTransmission(5);
available = Wire.requestFrom(5,2);

if (available == 2)
{
volt1 = (Wire.read() << 8 ) | (Wire.read());
Serial.print("volt1 =");
Serial.println(volt1);
}

Wire.endTransmission();
delay(1000);
}

-----------------slave-----------------
#include <Wire.h>
int volt;
int volt1;
byte c[2];
byte b[2];
void setup() {
Wire.begin(5);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}

void loop() {

//volt = analogRead(0);
//Serial.println(volt);
//delay(1000);
}
void requestEvent()
{
volt = analogRead(0);
Serial.println(volt);
c[0] = highByte(volt);
c[1] = lowByte(volt);
Wire.write(c,2);
delay (100);
volt1 = analogRead(1);
Serial.println(volt1);
b[0] = highByte(volt1);
b[1] = lowByte(volt1);
Wire.write(b,2);
delay(100);
}

Values are wrong when read

Does someone can help

thanks

Those are quote tags, not code tags. You should go back and edit the original message and delete the subsequent one.

This works for me. The master sends a write to the slave to set which "register" you want for the readFrom.

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);
}

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);
}