Serial communication


Hwello, I'm trying to connect 2 Arduino to communicate with each other via serial communication using the tx and Rx pins. It is transmitting the data but not receiving the data. I'm using chatgpt to code. The code I'm using is:
sender:
void setup() {
Serial.begin(9600); // Initialize serial communication
}

void loop() {
Serial.println("hello"); // Send "hello" over serial
delay(1000); // Delay for stability
}
receiver:
void setup() {
Serial.begin(9600); // Initialize serial communication
}

void loop() {
if (Serial.available() > 0) {
String receivedData = Serial.readStringUntil('\n');
if (receivedData.equals("hello")) {
Serial.println("goodbye"); // Respond with "goodbye"
}
}
}
whats wrong?? thx

How do you know it does not work?

Note that the RX/TX pins are also used for communication with the PC. I suggest that you use SoftwareSerial instead of Serial.

Please fix your post

  1. Remove any inappropriate language; I'm not the sensitive type but this is a public forum and children might find your topic.
  2. Apply code tags to your code as described in How to get the best out of this forum

1. Connect two Arduinos as per Fig-1 using Software UART Port (SUART Port).


Figure-1:

2. Upload the followng sketches: (untested)
Sketch for UNO-1:

#include<SoftwareSerial.h>
SoftwareSerial UART(A4, A5);

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

void loop()
{
     SUART.print("Hello");
     SUART.println();
     delay(1000);
}

Sketch for UNO-2:

#include<SoftwareSerial.h>
SoftwareSerial UART(A4, A5);

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

void loop()
{
     byte  n = SUART.available();
     if(n !=0 )
     {
           char y =  SUART.read();
           Serial.print(y);
     }
}

3. Check that the Serial Monitor shows the word Hello at 1-sec interval.

when looking at the serial monitor on the receiver Arduino, nothing comes up

Hi @UwUsername69. I see from your picture that you are using Leonardo clones. On the Leonardo, the hardware serial instance for pins 0 and 1 is Serial1:

https://www.arduino.cc/reference/en/language/functions/communication/serial/#:~:text=RX0)%2C%201(TX0)-,Leonardo,-%2C%20Micro%2C%20Yún%20Rev2

You are using Serial in your sketch. On the Leonardo, Serial is only used for communication with the computer over the USB cable, not via pins 0 and 1. So you will need to adjust your code accordingly.

You might find this tutorial to be useful:

https://docs.arduino.cc/built-in-examples/communication/SerialPassthrough/

OOPS, missed that :frowning:

Useful guide to establish Serial Communication between controllers. May helpful.

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