Hello everyone, I am trying to learn how to send data from one arduino to another. I searched and found 2 basic examples. But on the receiving Arduino serial monitor, nothing shows up. I upload the sender code to my UNO. I plug the UNO with a battery. Then I plug my R4 to USB, upload the receiving code and open monitor. Nothing shows up. Both programs and serial monitors are set to 9600 baud. Thank you.
//SENDING CODE
char Mymessage[5] = "Hello"; //String data
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write(Mymessage,5); //Write the serial data
delay(1000);
}
// RECEIVING CODE
char Mymessage[10]; //Initialized variable to store recieved data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
Serial.readBytes(Mymessage,5); //Read the serial data and store in var
Serial.println(Mymessage); //Print data on Serial Monitor
delay(1000);
}
If you're using the R4 to both receive data from the other Uno, and send output to the serial monitor on the same pins, that might be the problem. I could be wrong, but I believe the Uno communicates to serial to USB on the RX and TX pins.
I think you need to use SoftwareSerial to set up other pins to be a second serial port.
Thank you Benjamin Honestly not sure I follow you here. The sender Arduino UNO is not conected USB. It is powered from a battery. so it sends data (I think) using the TX pin to the receiving Arduino R4 on the RX pin. The R4 receiver is plugged in my laptop so I can open serial monitor of Arduino IDE and see the receiving data. Is my understanding totally wrong? Thank you.
You are not wrong but your reply has highlighted a problem
The classic Uno uses pins 0 and 1 for Serial communication but the Uno R4 doesn't. When using pins 0 and 1 on the R4 you are using the Serial1 interface rather than Serial and the code has to reflect that
@jimmy747 take note. Change references to Serial in the R4 code to Serial1
Thank you Benjamin, Based on your comment, what is easier to do for me (newbee).
While waiting for forum feedback i checked SoftwareSerial Library. Should I try that or do you have a simpler code example?
/*
Software serial multple serial test
Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.
The circuit:
RX is digital pin 2 (connect to TX of other device)
TX is digital pin 3 (connect to RX of other device)........... etc
ok thanks. that writes on the UNO serial monitor. I unplugged the UNO from my PC and power it with a battery. nothing shows up on the R4 monitor. Here the receiving code
char Mymessage[10]; //Initialized variable to store recieved data
void setup() {
// Begin the Serial at 9600 Baud
Serial1.begin(9600);
}
void loop() {
Serial1.readBytes(Mymessage,5); //Read the serial data and store in var
Serial1.println(Mymessage); //Print data on Serial Monitor
delay(1000);
}
right now my head is spinning like the blades on UKHeliBob helicopter. Ok. Stand by I am going to look for an example and switch to Software Serial. Seems easier. Thank you.
I don't have UNO R4; so, I give you a Tutorial between UNO-1 (Sender) and UNO-2/NANO (receiver). For clear understanding, both Arduinos will be powered from PC and the communication between them will be using Software UART Ports (SUART). After that you can try to operate them using Battery and Hardware UART Ports (UART).