Sendeing data from one board to another

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.

https://docs.arduino.cc/learn/built-in-libraries/software-serial

Someone will correct me if I'm wrong.

char Mymessage[5] = "Hello"; //String data

The "Hello" string uses 6 bytes of storage allowing for the terminating '\0'
Try

char Mymessage[] = "Hello"; //String data

instead and let the compiler work out the size of the array for you

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.

Ok thank you going to try that.

Thanks but still nothing on the receiving end

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

oooops sorry just saw the suggested change to serial1. trying now

same. nothing sowing.

On the sending side use println() to add CR quand LF at the end of the message. That will serve as end of command

On the receiver I would suggest to study Serial Input Basics to handle the incoming messages

1 Like

Thanks Jackson but where does that go? The code says Serial.write ..... change it to Serial.println? I tried it says function not found.

Sending code

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Hello"); //Write the serial data
  delay(1000);
}

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);
  }

Yes because Serial goes to the serial monitor if it’s connected

Your diagram showed your uno R3 had wires from pins 0 and 1 so that’s Serial too, but you need to close the serial monitor

Alternately use Software Serial

Serial1.readBytes(Mymessage,5);

It is not the cause of your problem but how many bytes are you sending ?
A hint, it is not 5

    Serial1.println(Mymessage); //Print data on Serial Monitor

Using Serial1 does not print to the Serial monitor, it prints to pins 0 and 1. Use Serial here instead of Serial1

right now my head is spinning like the blades on UKHeliBob helicopter. :rofl: Ok. Stand by I am going to look for an example and switch to Software Serial. Seems easier. Thank you.

You don't need to use SoftwareSerial on the R4

Check if a byte is available on Serial1 (pins 0 and 1) and if so, send it to Serial (Serial monitor)

Something like

if (Serial1.available())
{
    Serial.write(Serial1.read());
}

thanks Bob. eating a quick lunch and trying. I will let you all know

@jimmy747

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).

1. Connect UNO-1 and UNO-2/NANO as per Fig-1:
uartUnoUno-23
Figure-1:

2. Upload the following sketch in UNO-1 (Sender):

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);  //SRX = 2, STX = 3

char myMessage[] = "Hello"; //string data

void setup() 
{
 Serial.begin(9600);
 SUART.begin(9600);
}

void loop() 
{
 SUART.write(myMessage, sizeof myMessage);
 Serial.print('\n');  //Newline charcater (end-of-message mark)
 delay(1000);
}

3. Upload the following sketch into UNO-2 (Receiver):

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);  //SRX = 2, STX = 3

char myMessage[10];

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
}

void loop()
{
  byte n = SUART.available();  //check that at least one charcater haas arrived
  if (n != 0)
  {
    byte m = SUART.readBytesUntil('\n', myMessage, sizeof myMessage - 1);
    myMessage[m] = '\0';  //insert null-character
    Serial.println(myMessage);  //show Hello on SM2
  }
}

4. Serial Monitor-2 (Receiver) Output:

Hello
Hello
Hello