Question using Serial and Serial Monitor

Here's my code. This is installed on Arduino Uno 1.

String command = "";

void setup() {

  Serial.begin(9600);
  while (!Serial) { ; }

  Serial.println("Starting....");

}

void loop() {

  if (Serial.available()) {
    char c = Serial.read();
    if ( c == '\n' ) { command = ""; }
    else
    { command += c; }
  }


  if ( command == "ABC" ) {
    Serial.println("You typed: " + command);
    delay(500);
  }

}

When I run this and run the serial monitor, it works fine. I type 'ABC', hit send, and it says, 'You typed: ABC"

The problem comes when I try to hook this arduino up to another one. I plug in arduino Uno 2 and connect it to Arduino Uno 1 (with the above code)
RX to RX, and TX to TX.

When I use the Serial Monitor now ( which is connected to Arduino 2 ), the first line works, it says, 'Starting...' so I know I'm getting something from Arduino 1.

The problem is when I type something in the Serial Monitor and hit send I get no response. It seems that Serial.Available() is coming up 0.

How can I fix this?

Start with RX-TX and TX-RX

1 Like

It is working exactly the way it should. Rx is receiver, think of your TV You can watch it but it does not send your picture anywhere because it is a receiver. Not think of a wireless microphone you can talk into it and it will be received on one of the appropriately tuned receiver but it will not speak at you. You need Tx -> Rx and Rx -> Tx. Be sure both are at the same baud! Happy computing. You might spend a little time researching RS232 signals.

The above connection is not allowed practically. If you do so, none of the UNOs would work and in my case they don't work at all even the sketches are loaded into them.

I think I'm missing something. Here's a question - assume using a single arduino:

Does Serial.println("...") write to the TX pin? Does Serial.read() read from the RX, even with the serial monitor open?

Let me explain to you (with the help of Fig-1) what happens when you execute the following code:

char y = Serial.read();

1. When we enter ABC in the InputBox of the Serial Monitor, the ASCII codes pf these characters (0x41, 0x42, 0x43) arrive to UNO (one-after-another) via RXD-pin and are saved into the Serial Buffer on interrupt Basis.

2. The execution of char y = Serial.read() command brings out 0x41 from TOP position (Location-0) of the Buffer into variable y. The data byte x41 takes the position of Location-0 and 0x42 takes the position of Location-1. The Serial Buffer is a first-in first-out (FIFO) type buffer.

3. Concepts similar to the above may also be applied to understand the functioning of Serial.write(arg) command.


Figure-1:

4. The following diagram of Fig-2 may provide you additional information.


Figure-2:

Yes, and yes.

But if you are connected to the Serial monitor, then how are you connected to the other Arduino?

No!
The post #6 indicates that Serial.read() and Serial.write() deal with the Serial Buffers and not with the physical pins!

Whatever... where do the buffers get the data from?

BTW.. if you think that post #6 is something that a beginner will understand you are sadly mistaken.

We have to try to bring them into the root by providing conceptually correct information.

Must say I'm confused about the connections too. Are they both on usbs in different instances of the monitor on different com ports or what. Perhaps @studiogamma could drop a diagram in here to clarify.

But anyway, I would think that if you want serial comms between 2 Arduinos then software serial would be the thing to use, and leave the hardware serial on the usb intact for program loading and the monitor, with no chance of this confusion.

That's irrelevant on an Uno where Serial is always true.

What does that mean?

1 Like

Let me paraphrase post #10 as follows:

With popular description being aided by diagrams, we may try to educate the beginners on the working principles of Serial.read()/write() functions.

Uno 2 is connected to the computer via USB ( so it uses the serial monitor ) . It’s connected to Uno 1 via RX to RX and TX to TX and Gnd to gnd. Uno 1 is not connected to a computer. The shown code is on the Uno 1.

It does display ‘Starting…’ , which goes from the 1 to the 2 and displays on the 2’s serial monitor. It just seems to hang up on Serial.available() on Uno 1when I try to send data back (serial monitor to Usb2 to Usb1)

RX_a to TX_b
TX_a to RX_b

But if one of the Unos is on its USB to the monitor, shouldn't those Uno-Uno connections be software serial Rx-Tx and Tx-Rx??

I'm not arguing against that.
But it makes/made no sense to have tx to tx and rx to rx.

I know you weren't and I agree R-R and T-T makes no sense, regardless of it being hardware or software serial.

But while they're at it, it would make sense to me to move the Uno-Uno comms off the hardware port.

I agree.

@studiogamma you should look at the software serial library for the Uno-Uno comms. It installs in the IDE as part of the Arduino install if I recall, and there is an example or two.

Then on each Uno you stick this in your code, using whatever pins you like other than 0 or 1:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX  (call it what you like, mySerial is an example)

.... and join the Unos together on those pins, crossed over R-T and T-R.

Then, when you want to communicate between them you use mySerial. rather than Serial. which is still then used for Uno-serial monitor comms.