title: I2C communication issues between Arduino and NVIDIA Jetson
body: <p>I am trying to use I2C to send a signal with data from an NVIDIA Jetson TX1 to an Arduino Uno, and then have the Arduino send data back. Eventually the Arduino's data to send back will be collected from sensors. The Arduino is hooked up the the Jetson via a <a href="https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/" rel="nofollow noreferrer">multiplexer</a>.</p>
  
  <p>When I send a string from the Jetson, the Arduino receives and displays it on the serial output correctly, but the signal that the Jetson receives seems to be two random characters followed by the rest of the 32 bytes being 255. Also spaces don't seem to work, but that's not really an issue, more of just an observation.</p>
  
  <p>This is the code I am using on the Jetson</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/ioctl.h&gt;
  #include &lt;linux/i2c-dev.h&gt;
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  
  using namespace std;
  
  int addr1 = 0x40;
  int addr2 = 0x04;
  int file_i2c;
  int file_i2c2;
  const int numBytes = 32;
  char *filename = (char*)"/dev/i2c-1";
  unsigned char buffer[numBytes] = {0};
  
  bool writeStream(int file_i2c, string output) {
    if(write(file_i2c, output.c_str(), output.length()) != output.length()) {
      printf("\tFailed to write to the i2c bus.\n");
    }
    return true;
  }
  
  int main() {
    //Open i2c bus
    if((file_i2c = open(filename, O_RDWR)) &lt; 0) {
      printf("\tFailed to open the i2c bus\n");
      //exit(1);
    }
    if(ioctl(file_i2c, I2C_SLAVE, addr2) &lt; 0) {
      printf("\tFailed to acquire bus access and/or talk to slave.\n");
      //exit(1);
    }
    string input;
    cin &gt;&gt; input;
    writeStream(file_i2c, input);
    cin &gt;&gt; input; //Using this as a breakpoint
    if(ioctl(file_i2c, I2C_SLAVE, addr1) &lt; 0) {
      printf("\tFailed to acquire bus access and/or talk to slave.\n");
      //exit(1);
    }
    if(read(file_i2c, buffer,numBytes) != numBytes) {
      printf("\tFailed to read from the i2c bus.");
    }
    cout &lt;&lt; "Data read:\n";
    for (int i = 0; i &lt; numBytes; i++) {
      cout &lt;&lt; buffer[i] &lt;&lt; "\t" &lt;&lt; (int)buffer[i] &lt;&lt; endl;
    }
    cout &lt;&lt; "End!\n";
    return 0;
  }
  </code></pre>
  
  <p>Here is my Arduino code:</p>
  
  <pre><code>#include &lt;Wire.h&gt;
  #include &lt;Adafruit_Sensor.h&gt;
  #include &lt;Adafruit_TSL2591.h&gt;
  
  #define TCAADDR 0x70
  
  int clockSpeed = 100000;
  char myOutput[32] = {0};
  char sent[32] = {0};
  
  void setup() {
    digitalWrite(6, HIGH);
    Wire.begin(0x04);
    Wire.setClock(clockSpeed);
    Wire.onReceive(receiveEvent);
    Serial.begin(9600);
    tcaselect(1);
  }
  
  void loop() {
    delay(100);
    if(strcmp(myOutput, sent)) {
      send(myOutput);
      strncpy(sent, myOutput, 32);
    }
  }
  
  void tcaselect(uint8_t i) {
    if (i &gt; 7) return;
    Wire.beginTransmission(TCAADDR);
    Wire.write(1 &lt;&lt; i);
    Wire.endTransmission();
  }
  
  void receiveEvent() {
    char c[32] = {0};
    int count = 0;
    while (0 &lt; Wire.available()) {
      c[count] = Wire.read();
      Serial.print(c[count]);
      count++;
    }
    Serial.println();
    strncpy(myOutput, c, 32);
  }
  
  void send(char c[]) {
    Wire.beginTransmission(0x40);
    Serial.print("Sending signal:\t");
    Wire.write(c);
    Serial.print(c);
    Serial.println();
    Wire.endTransmission();
  }
  </code></pre>