Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

if I remove the transmission code from slave this works properly but I need to transmit from slave. Here is my master code

#include <Wire.h>

void setup()
{
  Wire.begin(3); 
  Wire.onReceive(receiveEvent);
}

byte x = 0;

void loop()
{
  Wire.beginTransmission(9); 
  Wire.write("s");        
  Wire.write(x);                
  Wire.endTransmission();    

  x++;
  delay(500);
}

void receiveEvent(int howMany)
{

  if(Wire.available()){
      int x = Wire.read();
      Serial.println(x);

  }

}

Here is my slave code :

#include <Wire.h>

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

void loop()
{
  delay(100);
}


void receiveEvent(int howMany)
{

  if(Wire.available()){
    char c = Wire.read(); 
    Serial.print(c);
    if(c == 's'){
      int x = Wire.read();
      Serial.println(x);

      //transmit to master 
      Wire.beginTransmission(3);
      Wire.write(1);
      Wire.endTransmission();

    }else{
      Wire.read();
    }
  }

}

Is it possible not to use Wire.onRequest(requestCallback); to send data to master kindly help.

share|improve this question

migrated from electronics.stackexchange.com Jun 23 at 10:39

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

    
When a sketch calls Wire.begin() and passes a number, it has nominated itself as a slave on the bus. So you appear to have two slaves. Normally one would have one master and one slave. –  James Cameron Jun 23 at 8:44
    
What you seem to want is a combination of the Arduino Wire Tutorial arduino.cc/en/Tutorial/MasterReader and arduino.cc/en/Tutorial/MasterWriter do you agree? –  James Cameron Jun 23 at 8:50
    
i had a look but not able to implement it properly any suggestion ? if i give 2 wire.write the master is not reading it properly –  srinivas Jun 23 at 9:21
    
I don't know what you mean, sorry. –  James Cameron Jun 23 at 9:37
    
the Wire.requestFrom has only one wire.write i need multiple wire.write which is not supported so is there any workarounds ? –  srinivas Jun 23 at 14:30

1 Answer 1

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 would suggest you start looking at the I2C protocol and understand there are 4 Modes:

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

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.

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

Master Code

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

Slave Code

#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");
    }
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.