I2C issue

Hello, I have the following master code:

#include <Wire.h>

byte firstByte;  // LED display, top row
byte secondByte;  // LED display, bottom row

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

void loop()
{
  Wire.requestFrom(4, 2); 

  if (Wire.available())
  { 
    firstByte = Wire.read();
    secondByte = Wire.read();
  }

  Serial.println(firstByte);  
  Serial.println(secondByte);
  
  delay(1000);
}

and the following slave code:

// Libraries:
#include <Wire.h>  // I2C library

void setup()
{
  Wire.begin(4); // Joins I2C bus
  Wire.onRequest(dataRqst); 
}

void loop()
{
  // Do noting
}

void dataRqst()
{
  Wire.write(127); 
  Wire.write(128); 
}

When I run the code, I get:
0 
128
0
128
...

even I am expecting to get:
127
128
127
128
...

Any advice?

And the problem is what ?

When I run the codes on 2 Unos I get:

127
128

127
128

127
128

Note: I addded a Serial.println() to separate the input iterations.

Serial.println(firstByte); 
Serial.println(secondByte);
Serial.println();

If I change the numbers in the slave, those numbers are sent to the master.

What Arduinos are you using? Post a wiring diagram. How far apart are the master and slave? Perhaps you need pullups?

I am using 2 Unos. They are connected to each other with abt 10 cm wires.

Are the grounds connected?

How far apart are the master and slave (how long are the wires)? Perhaps you need pullups?

It helps if you answer all the questions.

I modified the slave program so that you could enter numbers from the serial monitor. That way you do not have to compile and upload every time that you want to change the numbers while testing. No change to master (except the previous println()). Tested on my Unos and works fine.

// Libraries:
#include <Wire.h>  // I2C library

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

byte firstByte = 128;
byte lastByte = 23;
char *strings[3];
char *ptr = NULL;

void setup()
{
   Serial.begin(115200);
   Serial.println("<Arduino is ready>");
   Serial.println("enter 2 bytes (0 - 255) separated by a comma and ending with enter");
   Serial.println("make sure serial monitor line endings set to Newline or Both");
   Wire.begin(4); // Joins I2C bus
   Wire.onRequest(dataRqst);
}

void loop()
{
   recvWithEndMarker();
   if (newData)
   {
      parse();
      firstByte = atoi(strings[0]);
      lastByte = atoi(strings[1]);
      newData = false;
   }
}

void dataRqst()
{
   Wire.write(firstByte);
   Wire.write(lastByte);
}

void parse()
{
   byte index = 0;
   ptr = strtok(receivedChars, ",");
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
   Serial.println("strings");
   for (int n = 0; n < index; n++)
   {
      Serial.println(strings[n]);
   }
   Serial.println();
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial.available() > 0 && newData == false)
   {
      rc = Serial.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         if(rc == '\r')
         {
            return;
         }
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

ELohiiri, we know what is going on: this is a psychological experiment to check how far we are willing to go with a "problem" ;) No ? Are you sure ? Then the cause of the problem is something that you are not telling us.

Use the newest Arduino IDE 1.8.13. If you use an older version or an other development environment, then that is the problem. Have you installed Arduino years ago and you never deleted the hidden "arduino15" folder ? Then your Arduino build environment might be broken. Uninstall Arduino, remove the "Program Files (x86)\Arduino" folder, removed the hidden "arduino15" folder, check your "Documents\Arduino\libraries" folder for libraries that you don't use anymore. Then re-install the newest Arduino IDE. Be sure to update both Uno boards after installing the newest Arduino IDE version.

Did you install a "better" Wire library ? Is there a Wire library in your own "libraries" folder ?

Breadboards make bad contacts, jumper wires can be broken. Verify that everything is connected. Add 4k7 pullup resistors from SDA to 5V and from SCL to 5V. Connect the GNDs.

Check the 5V pin of both boards. Is it above 4.5V ?

Hi all,

Thank you for your questions and comments. Based on that I think that my code is not causing the issue. Need to investigate further. I am using IDE version 1.8.12. - must to update that. Operating voltage may be an issue as I am feeding the other Uno and a number of LEDs via primary Uno's +5V pin. I don't know how much current I can take from it.

I suggest to close this topic.

Are you still getting the following output (incorrect) in your hardware setup: 0 128 0 128 ......

I could not reproduce your fault even by powering the Slave from 5V supply of the Master. I am getting the expected output: 127 128 127 128 .........

@ELohiiri, this forum does not have something to close a topic.

It is very intriguing, so if you find what was wrong, please let us know.

Version 1.8.12 is okay, unless you had a version 1.5 or lower installed before. Then some things might be left behind. You could install the Arduino IDE on a new computer and try with a fresh install.

Did you know that you can start the Arduino IDE twice ? Connect both boards to the computer via USB. Start the Arduino IDE, select the com port, then start another instance of the Arduino IDE and the select the com port of the other Arduino. That way, you can also open a serial monitor for each board, and see the messages of both 8)

Déjà-vu, I made the same joke four years ago :confused: