Try to connect them two gather .Do you have any examples,sugestions or ideas?
What is the purpose of connecting them together ?
Is it to pass data between them and, if so, what data and where does it come from ?
Users can access the IP address of R4 WiFi through their mobile phone or computer browser, view real-time temperature and humidity data collected by Uno R3, just make attempts
Why not collect the data using the R4 and eliminate the R3 ?
Of course that's possible, but I already have an R4 and bought an R3 out of curiosity, so I'm exploring some of the features they can use together
Adding the R3 to your project will only make things more complicated but if you are determined to connect the two boards then a serial link would be easiest
Use the SoftwareSerial library on the R3 to provide a second serial port on tw pins of your choice other than 0 and 1. Connect the hardware Tx pin on the R4 to your SS Rx pin on the R3 and vice versa. Connect GND on the R3 to GND on the R4
Note that the R4 has two serial ports. Serial is connected directly to USB and Serial1 uses pins 0 and 1
with the connections above, what you send from you SS port on the R3 will be available() on Serial1 of the R4 and vice versa. The received data can be written to Serial on either of the boards
The sketches are easier to write than to describe !
//Transmit from R3
#include <SoftwareSerial.h>
SoftwareSerial softSerial(2, 4); //Rx, Tx
void setup()
{
softSerial.begin(9600);
}
void loop()
{
static byte x;
softSerial.println(x++);
delay(1000);
}
//Receive on R4
void setup()
{
Serial.begin(115200);
Serial1.begin(9600);
}
void loop()
{
if (Serial1.available())
{
Serial.write(Serial1.read());
}
}
Try using I2C Bus and then SPI Port for more learning.
#include <Wire.h>
#define SLAVE_ADDR 0x08 // 从设备I2C地址
void setup() {
Wire.begin(SLAVE_ADDR); // 加入I2C总线作为从设备
Wire.onReceive(receiveEvent); // 注册接收数据回调函数
Wire.onRequest(requestEvent); // 注册主设备数据请求回调函数
Serial.begin(9600);
Serial.println("Slave Ready!");
}
void loop() {
delay(100); // 避免阻塞
}
// 当主设备发送数据时触发
void receiveEvent(int bytes) {
while (Wire.available()) {
char c = Wire.read(); // 读取字节
Serial.print("Received: ");
Serial.println(c);
}
}
// 当主设备请求数据时触发
void requestEvent() {
Wire.write('S'); // 回复字符 'S'
Serial.println("Sent: S");
}
#include <Wire.h>
#define SLAVE_ADDR 0x08 // 从设备地址
void setup() {
Wire.begin(); // 作为主设备加入I2C总线
Serial.begin(9600);
Serial.println("Master Ready!");
}
void loop() {
// 发送数据到从设备
Wire.beginTransmission(SLAVE_ADDR);
Wire.write('M'); // 发送字符 'M'
Wire.endTransmission();
Serial.println("Sent: M");
// 请求从设备发送1字节数据
Wire.requestFrom(SLAVE_ADDR, 1);
if (Wire.available()) {
char c = Wire.read(); // 读取回复
Serial.print("Reply: ");
Serial.println(c);
}
delay(2000); // 每2秒执行一次
}
Here it is.
Wire.requestFrom(); is a looping function which is terminated once requested number of data bytes arrive from Slave to Master. If this is correct, then do we need to execute
if (Wire.available())?
this is AI told me
Blocking ≠ Complete Data
Wire.requestFrom() blocks until the I2C transaction finishes, not until all requested bytes arrive. The slave might send fewer bytes than requested.
Safety Check
Wire.available() verifies if data actually exists in the buffer before reading. Without it:
Your code might crash if no data was received
You risk reading garbage values
Real-World Scenarios
Slaves can legally send fewer bytes (e.g., sending termination byte early)
Transmission errors can occur (electrical noise, loose wires)
Slave might not implement request handling correctly
Wire.available() is your data safety net. Never skip it after requestFrom(). I2C protocol allows variable-length responses, so this check is mandatory for reliable communication.
The Slave's sendEvent(){} is designed to transfer the same number of data bytes being requested by Master. Then how the Slave can transfer less number of bytes unless the I2C bus is broken/hanged?
Kindly, don't post AI's opinion; rather, post your own opinion based on how much you have experinced on the I2C Protocol.
Thanks.I'm just a new one learn arduino for about 1month long



