I was trying to complete an assignment my teacher gave me where I had to get two UNO R4 WiFi boards to communicate with each other over I2C, but it wasn't working at all. This was how I wired the boards (pretend these are R4 boards, TinkerCAD only had R3 boards):
I tried swapping the wiring to be A4 <-> A4 & A5 <-> A5, swapping the R4 boards with brand new R4s, using different wires, no luck. However, as soon as I swapped to using R3 boards, my sketch ran perfectly despite no modifications to it other then changing the board.
I did a quick search and found a few forum posts here with similar issues and I've gathered the following:
- The R4 boards don't have pull-up resistors on SCL and SDA while the R3 boards do
- The R4 boards have a qwiic connector which is specifically for connecting I2C devices
- The qwiic connector does have pull-up resistors
- I2C devices are active-low, which makes pull-up resistors necessary
- The R4's microcontroller has pull-up resistors built-in that might be able to handle I2C
I don't have qwiic wires, so how do I use I2C on an Arduino R4 WiFi normally like how it would work on an Arduino R3? Or is that not possible?
Edit: I forgot to include my code snippets.
Here's the sketch I uploaded to the parent/ master board:
#include <Wire.h>
byte childAddress = 8;
byte answerSize = 10;
void setup() {
Wire.begin(); // Initialize as a parent (no address)
Serial.begin(9600); // Start serial communication
Serial.println("Parent Ready.");
}
void loop() {
Serial.print(" Sent: ");
Wire.beginTransmission(childAddress); // Start transmission to device with address 8
Wire.write("Hey Kiddo!"); // Send a string
Wire.endTransmission(); // End the transmission
Serial.println("Hey Kiddo!");
delay(1000); // Wait for a second
Serial.println("Requesting data... ");
Wire.requestFrom(childAddress, answerSize); // Request 6 bytes from the child device
Serial.print("Recieved: ");
while (Wire.available()) { // Child may send less than requested
char c = Wire.read(); // Receive a byte as character
Serial.print(c); // Print the character
}
Serial.println();
Serial.print(" Sent: ");
Wire.beginTransmission(childAddress); // Start transmission to device with address 8
Wire.write("Knock knock"); // Send a string
Wire.endTransmission(); // End the transmission
Serial.println("Knock knock");
delay(1000);
Serial.println("Requesting data... ");
Serial.print("Recieved: ");
Wire.requestFrom(childAddress, answerSize); // Request 6 bytes from the child device
while (Wire.available()) { // Child may send less than requested
char c = Wire.read(); // Receive a byte as character
Serial.print(c); // Print the character
}
Serial.println();
Serial.print(" Sent: ");
Wire.beginTransmission(childAddress); // Start transmission to device with address 8
Wire.write("Chicken butt"); // Send a string
Wire.endTransmission(); // End the transmission
Serial.println("Chicken butt");
delay(1000);
Serial.println("Requesting data... ");
Wire.requestFrom(childAddress, answerSize);
Serial.print("Recieved: ");
while (Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
Serial.println();
}
And this the sketch for the child:
#include <Wire.h>
byte childAddress = 8;
String message; // create a global var to store message from parent
void setup() {
Wire.begin(childAddress); // Initialize as a child with address 8
Wire.onReceive(receiveEvent); // Function to run when data is received
Wire.onRequest(requestEvent); // Function to run when data is requested by parent
Serial.begin(9600); // Start serial communication
Serial.println("Child Ready.");
}
void loop() {
delay(100);
}
void receiveEvent(int howMany) {
Serial.print("Received: ");
message = ""; // set message string to null
while (Wire.available()) {
char c = Wire.read();
message += c; // concatenate the new character onto the message string
}
Serial.println(message);
}
void requestEvent() {
Serial.print(" Sent: ");
String reply;
if (message == "Hey Kiddo!") {
reply = "Hi big vro";
};
if (message == "Knock knock") {
reply = "Whos there";
};
if (message == "Chicken butt") {
reply = "BAHAHAHAHA";
};
Wire.write(reply.c_str(), reply.length());
Serial.println(reply);
}
