Master reader I2C

From Pinguino
Jump to: navigation, search

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

u8 I2C_address = 0x2C; //slave address

void setup() {
  Wire.begin(0,100); // 0=master mode and 100=100 KHz as bus speed    
  Serial.begin(9600);
  delay(100);
}
void loop() {
u8 c[32]; // length to adapt to your requirement
u8 i;
  Wire.requestFrom(I2C_address,32); // here 32 characters as maxi are requested
  delay(500);
  Serial.printf("\r\n<");
  delay(200);
  while(!Wire.available());
  i=0;
  while(Wire.available())    // slave may send less than requested
  { 
    c[i] = Wire.read(); // receive a byte as character
	Serial.printf("%c",c[i]); // print the character
	i++;
  }

  delay(1000);
}