Slave reader I2C

From Pinguino
Jump to: navigation, search


Example for functions: Wire.begin Wire.onReceive Wire.available Wire.read

// Slave MCU as reader

void receiveEvent(u8);
u8 I2C_address = 0x2C; //slave address

void setup() {
  Serial.begin(9600);
  Wire.begin(I2C_address,0); // the second parameter (0) is not used in the present slave mode    
  Wire.onReceive(receiveEvent);
}

void loop() {
}

void receiveEvent(u8 count)
{
u8 rxbuffer[32]; //length to adapt according your requirement
u8 i;
u8 x;

  i=0;
  Serial.printf("\r\n");
  while(Wire.available())
  {
     rxbuffer[i] = Wire.read();
     Serial.printf("%c", rxbuffer[i]);
     i++;
  }
}