Skip to main content
Fixed code quoting.
Source Link
timemage
  • 5.7k
  • 1
  • 15
  • 27

I tried i2c EPROM and that worked pretty fine with arduino master. then i wanted to establish i2c communication between 2 arduinos such that both exchange each other's sensors data. I am not able to receive bytes from slave arduino. Unable to do what i was upto i resorted to example sketches of Wire library to assess what i was doing wrong and to my surprise even example sketches were not working. Definitely there is something wrong with my approach. Problem only appears when master requests data from another arduino slave (master sends data successfully to slave arduino but vice versa is not true) . i have assessed that problem is in requestEvent function of slave as that either doesnt send anything. or returns mysterious character. I am runnign proteous simulation. Master sends data seamlessly ( although here master is just reading but i was able to send data from master to slave successfully) but when master requests data from slave it does not get desired response. Please suggest some solution. Here is the code which is just copy pasted from Wire library examples ( master reader for master arduino and slave sender for slave arduino. // slave slave is just to send 6 bytes ("Hello ") to master when prompted // Wire Master Reader // by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library // Reads data from an I2C/TWI slave device // Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

Master code

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

    // Wire SlaveMaster SenderReader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// SendsReads data asfrom an I2C/TWI slave device
// Refer to the "Wire MasterSlave Reader"Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


    #include <Wire.h>
    
    void setup() {
      Wire.begin(8);                // join i2c bus with (address #8
  optional for master)
  WireSerial.onRequestbegin(requestEvent9600);  // register event
 start serial for output
}
    
    void loop() {
  Wire.requestFrom(8, 6);   delay(100);
 // request 6 }
bytes from slave device #8

  while (Wire.available()) { // function thatslave executesmay wheneversend dataless isthan requested by master
    // this function is registered as anchar event,c see= setupWire.read()
 ; // receive a voidbyte requestEvent()as {character
      WireSerial.writeprint("hello "c); // respond with message of 6 bytes
      // as expectedprint bythe mastercharacter
  }

  delay(500);
}

Slave code

// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}

I tried i2c EPROM and that worked pretty fine with arduino master. then i wanted to establish i2c communication between 2 arduinos such that both exchange each other's sensors data. I am not able to receive bytes from slave arduino. Unable to do what i was upto i resorted to example sketches of Wire library to assess what i was doing wrong and to my surprise even example sketches were not working. Definitely there is something wrong with my approach. Problem only appears when master requests data from another arduino slave (master sends data successfully to slave arduino but vice versa is not true) . i have assessed that problem is in requestEvent function of slave as that either doesnt send anything. or returns mysterious character. I am runnign proteous simulation. Master sends data seamlessly ( although here master is just reading but i was able to send data from master to slave successfully) but when master requests data from slave it does not get desired response. Please suggest some solution. Here is the code which is just copy pasted from Wire library examples ( master reader for master arduino and slave sender for slave arduino. // slave is just to send 6 bytes ("Hello ") to master when prompted // Wire Master Reader // by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library // Reads data from an I2C/TWI slave device // Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

    // Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


    #include <Wire.h>
    
    void setup() {
      Wire.begin(8);                // join i2c bus with address #8
      Wire.onRequest(requestEvent); // register event
    }
    
    void loop() {
      delay(100);
    }
    
    // function that executes whenever data is requested by master
    // this function is registered as an event, see setup()
     void requestEvent() {
      Wire.write("hello "); // respond with message of 6 bytes
      // as expected by master
    }

I tried i2c EPROM and that worked pretty fine with arduino master. then i wanted to establish i2c communication between 2 arduinos such that both exchange each other's sensors data. I am not able to receive bytes from slave arduino. Unable to do what i was upto i resorted to example sketches of Wire library to assess what i was doing wrong and to my surprise even example sketches were not working. Definitely there is something wrong with my approach. Problem only appears when master requests data from another arduino slave (master sends data successfully to slave arduino but vice versa is not true) . i have assessed that problem is in requestEvent function of slave as that either doesnt send anything. or returns mysterious character. I am runnign proteous simulation. Master sends data seamlessly ( although here master is just reading but i was able to send data from master to slave successfully) but when master requests data from slave it does not get desired response. Please suggest some solution. Here is the code which is just copy pasted from Wire library examples ( master reader for master arduino and slave sender for slave arduino. slave is just to send 6 bytes ("Hello ") to master when prompted.

Master code

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

Slave code

// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}

Source Link

Arduino Wire library master reader, slave sender example not working

I tried i2c EPROM and that worked pretty fine with arduino master. then i wanted to establish i2c communication between 2 arduinos such that both exchange each other's sensors data. I am not able to receive bytes from slave arduino. Unable to do what i was upto i resorted to example sketches of Wire library to assess what i was doing wrong and to my surprise even example sketches were not working. Definitely there is something wrong with my approach. Problem only appears when master requests data from another arduino slave (master sends data successfully to slave arduino but vice versa is not true) . i have assessed that problem is in requestEvent function of slave as that either doesnt send anything. or returns mysterious character. I am runnign proteous simulation. Master sends data seamlessly ( although here master is just reading but i was able to send data from master to slave successfully) but when master requests data from slave it does not get desired response. Please suggest some solution. Here is the code which is just copy pasted from Wire library examples ( master reader for master arduino and slave sender for slave arduino. // slave is just to send 6 bytes ("Hello ") to master when prompted // Wire Master Reader // by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library // Reads data from an I2C/TWI slave device // Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

    // Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


    #include <Wire.h>
    
    void setup() {
      Wire.begin(8);                // join i2c bus with address #8
      Wire.onRequest(requestEvent); // register event
    }
    
    void loop() {
      delay(100);
    }
    
    // function that executes whenever data is requested by master
    // this function is registered as an event, see setup()
    void requestEvent() {
      Wire.write("hello "); // respond with message of 6 bytes
      // as expected by master
    }

but proteous simulation shows that master is getting 6 bytes from slave instead of "Hello " which was sent by slave master is getting "| " "proteous simulation screen shot of the problem