Not able to establish an I2C connection

Hi guys,
I tried for several days now to make an I2C connection between an Arduino UNO R4 Wifi and an Arduino UNO R3. I tried programming it myself first, but it didn't work so I tried examples online with the same outcome. Here the example(https://www.instructables.com/I2C-between-Arduinos/): Master:

// Chapter 7 - Communications
// I2C Master
// By Cornel Amariei for Packt Publishing

// Include the required Wire library for I2C
#include <Wire.h>

int x = 0;

void setup() {
  // Start the I2C Bus as Master
  Wire.begin(); 
}

void loop() {
  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(x);              // sends x 
  Wire.endTransmission();    // stop transmitting
 
  x++; // Increment x
  if (x > 5) x = 0; // `reset x once it gets 6
  
  delay(500);
}

Slave:

// Chapter 7 - Communications
// I2C Slave
// By Cornel Amariei for Packt Publishing

// Include the required Wire library for I2C
#include <Wire.h>

int LED = 13;
int x = 0;

void setup() {
  // Define the LED pin as Output
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Slave on address 9
  Wire.begin(9); 
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
}

void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}

void loop() {
  //If value received is 0 blink LED for 200 ms
  if (x == '0') {
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }
  //If value received is 3 blink LED for 400 ms
  if (x == '3') {
    digitalWrite(LED, HIGH);
    delay(400);
    digitalWrite(LED, LOW);
    delay(400);
  }
}

Thank you for helping

Your Slave sketch s revised as follows:

#include <Wire.h>

int LED = 13;
byte x = 0;     //I2C network handles data byte by byte
volatile bool flag = false;

void setup() 
{
  // Define the LED pin as Output
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Slave on address 9
  Wire.begin(9); 
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
}

void loop() 
{
  //If value received is 0 blink LED for 200 ms
  if(flag == true)
  {  
     if (x == 0x00)   //'0' is equal to 0x30
     {
        digitalWrite(LED, HIGH);
        delay(200);
        digitalWrite(LED, LOW);
        delay(200);
     }
     //If value received is 3 blink LED for 400 ms
     if (x == 0x03)   //'3' is equal to 0x33
     {
       digitalWrite(LED, HIGH);
       delay(400);
       digitalWrite(LED, LOW);
       delay(400);
     }
     flag = false;
  }
}

void receiveEvent(int bytes) 
{
  x = Wire.read();    // read one character from the I2C
  flag =  true;   //to tell loop() function that data has arrived from Master
}

I see no 4.7K pullup resistors to VCC in the schematic you refer to. There should be on the SDA and SCL line.

You send 2 bytes but receive only 1.

Please note the difference between char and int. The slave cannot react because it will never receive any of the coded characters.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.