Hello All,
I am trying to send a string or a letter from one arduino to another but the program does not work at all.
Two arduino are set baurate: 9600. One on COM4 and another on COM6, but when I send, I don't get anything from both in serial monitor.
One Tx connects Two Rx and One Rx connects to Two Tx.
Any help will be greatly appreciated cause I am new with this.
Here is my code for the sender:
String msg = "";
void setup() {
Serial.begin(9600);
}
void readSerialPort() {
msg = "";
if (Serial.available()) {
delay(30);
while (Serial.available() > 0) {
msg += (char)Serial.read();
}
Serial.flush();
}
}
void loop() {
readSerialPort();
if (msg != ""){
char Mymessage = msg.charAt(0);
Serial.write(Mymessage); //Write the serial data
}
delay(1000);
}
And here is the code for receiver:
char Mymessage[2]; //Initialized variable to store recieved data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
Serial.readBytes(Mymessage,2); //Read the serial data and store in var
Serial.println(Mymessage); //Print data on Serial Monitor
delay(1000);
}
Welcome to the forum.
I see this is your first forum post and it seems to me that you are also new to "arduino".
The Arduino RX and TX are the communication "hardware" between the Arduino and the PC.
Some arduinos have more than one TX and RX, such as the arduino mega, but the arduino UNO only has one.
If you use these same RX and TX for communication between the arduinos UNO, there will be a "conflict" of transmitting and receiving data between the arduinos and the PC.
Using arduino UNO, for projects like yours, it is recommended to use the Softserial.h library.
You will not be able to send from the PC to an arduino when the Rx line of that arduino is connected to the Tx line of another arduino. The Tx from the USB-to-serial chip is fed to the Rx of the atmega328 through a resistor, the Tx from the other arduino will overpower the signal from the USB-to-serial chip and it will never reach the atmega328.
You need an Example/Tutorial. Follow these steps, see the output, and then write your own sketch to reproduce the results.
1. Connect UNO-1 and UNO-2 as per diagram of Fig-1 using Software UART (SUART) Port. The Hardware UART Ports of both UNOs are engaged with PC/SM for sketch uploading and debugging.
Figure-1:
2. Upload the following sketch (not tested) in UNO-1 (Arduino-1).