Skip to main content
Made communication a 2-way exchange
Source Link
Wayne
  • 161
  • 3

Below you will find 2 sketches that successfully sends data to a slave. I know this example just shows one way, but I would suggest you start looking at the I2C protocol and understand there are 4 Modes:

All communications regardless of how the data is transferred requires a master. It's the master that controls the clock pulses, thats why its always the master that initiates the communcation.

This code shows how a master sends data to a slave, and then how it requests data from the Masterslave. If we look at this code from the master point->Transmitter and Slaveof->Receiver modes.view, you can see it essentially says, send this byte to the client (m->t & s->r), then request data from the slave (m->r,s->t)

#include <Wire.h>

#define SLAVE_ADDRESS 0x60

void setup()
{
  Wire.begin(); 
  Wire.onRequest(requestEvent);
  randomSeed(analogRead(3));
  Serial.begin(9600);  
}

byte x = 0;

void loop()
{
  Wire.beginTransmission(9); 
  x = random(0,255);
  Serial.print("Sent: ");
  Serial.print(x, HEX);
  Serial.print("\n");
  Wire.beginTransmission(0x60);   
  Wire.write(x);                
  Wire.endTransmission();    
  delay(500);
}
  Serial.println("Requesting Data"); 
void requestEvent Wire.requestFrom(SLAVE_ADDRESS, 1);
{  
  int bytes = Wire.available();
  Serial.print("Slave sent ");
  Serial.print(bytes);
  Serial.print(" of information\n");
  for(int i = 0; i <i< bytes; i++)
  {
      int x = Wire.read();
      Serial.print("Slave Sent: ");
      Serial.print(x, HEX);
      Serial.print("\n");
  }  
  delay(500);
}
#include <Wire.h>

#define SLAVE_ADDRESS 0x60
byte x = 0x00;
void setup()
{
  Wire.begin(9SLAVE_ADDRESS);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600);
}

void loop()
{
  delay(100);
} 

void requestEvent() 
{
  Serial.print("Request from Master. Sending: ");
  Serial.print(x, HEX);
  Serial.print("\n");
  
  Wire.write(x);
}

void receiveEvent(int bytes)
{
  if(Wire.available() != 0)
  {
    for(int i = 0; i< bytes; i++)
    {
      byte x = Wire.read();
      Serial.print("Received: ");
      Serial.print(x, HEX);
      Serial.print("\n");
    }
  }
}

Below you will find 2 sketches that successfully sends data to a slave. I know this example just shows one way, but I would suggest you start looking at the I2C protocol and understand there are 4 Modes:

This code shows the Master->Transmitter and Slave->Receiver modes.

#include <Wire.h>

void setup()
{
  Wire.begin(); 
  Wire.onRequest(requestEvent);
  randomSeed(analogRead(3));
  Serial.begin(9600);  
}

byte x = 0;

void loop()
{
  Wire.beginTransmission(9); 
  x = random(0,255);
  Serial.print("Sent: ");
  Serial.print(x, HEX);
  Serial.print("\n");
  Wire.write(x);                
  Wire.endTransmission();    
  delay(500);
}

void requestEvent()
{
  int bytes = Wire.available();
  for(int i = 0; i < bytes; i++)
  {
      int x = Wire.read();
      Serial.print("Slave Sent: ");
      Serial.print(x, HEX);
      Serial.print("\n");
  }
}
#include <Wire.h>

void setup()
{
  Wire.begin(9);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void loop()
{
  delay(100);
}


void receiveEvent(int bytes)
{
  if(Wire.available() != 0)
  {
    for(int i = 0; i< bytes; i++)
    {
      byte x = Wire.read();
      Serial.print("Received: ");
      Serial.print(x, HEX);
      Serial.print("\n");
    }
  }
}

Below you will find 2 sketches that successfully sends data to a slave. I would suggest you start looking at the I2C protocol and understand there are 4 Modes:

All communications regardless of how the data is transferred requires a master. It's the master that controls the clock pulses, thats why its always the master that initiates the communcation.

This code shows how a master sends data to a slave, and then how it requests data from the slave. If we look at this code from the master point-of-view, you can see it essentially says, send this byte to the client (m->t & s->r), then request data from the slave (m->r,s->t)

#include <Wire.h>

#define SLAVE_ADDRESS 0x60

void setup()
{
  Wire.begin(); 
  randomSeed(analogRead(3));
  Serial.begin(9600);  
}

byte x = 0;

void loop()
{
  x = random(0,255);
  Serial.print("Sent: ");
  Serial.print(x, HEX);
  Serial.print("\n");
  Wire.beginTransmission(0x60);   
  Wire.write(x);                
  Wire.endTransmission();   
  delay(500);
  Serial.println("Requesting Data"); 
  Wire.requestFrom(SLAVE_ADDRESS, 1);
  
  int bytes = Wire.available();
  Serial.print("Slave sent ");
  Serial.print(bytes);
  Serial.print(" of information\n");
  for(int i = 0; i< bytes; i++)
  {
    x = Wire.read();
    Serial.print("Slave Sent: ");
    Serial.print(x, HEX);
    Serial.print("\n");
  }  
  delay(500);
}
#include <Wire.h>

#define SLAVE_ADDRESS 0x60
byte x = 0x00;
void setup()
{
  Wire.begin(SLAVE_ADDRESS);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600);
}

void loop()
{
  delay(100);
} 

void requestEvent() 
{
  Serial.print("Request from Master. Sending: ");
  Serial.print(x, HEX);
  Serial.print("\n");
  
  Wire.write(x);
}

void receiveEvent(int bytes)
{
  if(Wire.available() != 0)
  {
    for(int i = 0; i< bytes; i++)
    {
      x = Wire.read();
      Serial.print("Received: ");
      Serial.print(x, HEX);
      Serial.print("\n");
    }
  }
}
Source Link
Wayne
  • 161
  • 3

To join the bus as a master you cannot supply a 7-Bit slave address so your code has two slaves.

Below you will find 2 sketches that successfully sends data to a slave. I know this example just shows one way, but I would suggest you start looking at the I2C protocol and understand there are 4 Modes:

  • Master->Transmitter
  • Master->Receiver
  • Slave->Transmitter
  • Slave->Receiver

The MasterReader/MasterWriter tutorials on the arduino forum should now possibly start to make a little more sense when understanding those modes.

This code shows the Master->Transmitter and Slave->Receiver modes.

Master Code

#include <Wire.h>

void setup()
{
  Wire.begin(); 
  Wire.onRequest(requestEvent);
  randomSeed(analogRead(3));
  Serial.begin(9600);  
}

byte x = 0;

void loop()
{
  Wire.beginTransmission(9); 
  x = random(0,255);
  Serial.print("Sent: ");
  Serial.print(x, HEX);
  Serial.print("\n");
  Wire.write(x);                
  Wire.endTransmission();    
  delay(500);
}

void requestEvent()
{
  int bytes = Wire.available();
  for(int i = 0; i < bytes; i++)
  {
      int x = Wire.read();
      Serial.print("Slave Sent: ");
      Serial.print(x, HEX);
      Serial.print("\n");
  }
}

Slave Code

#include <Wire.h>

void setup()
{
  Wire.begin(9);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void loop()
{
  delay(100);
}


void receiveEvent(int bytes)
{
  if(Wire.available() != 0)
  {
    for(int i = 0; i< bytes; i++)
    {
      byte x = Wire.read();
      Serial.print("Received: ");
      Serial.print(x, HEX);
      Serial.print("\n");
    }
  }
}