+2 votes
9.4k views
in Arduino by
Sorry for my stupid question, but I cannot understand what is going on. Can anyone help me understand please?

This is my code written for Mega

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  if (Serial.available()) {    Serial.write("From Serial");  }
  if (Serial1.available()) {    Serial1.write("From Serial-1");  }
}

I am only seeing the Serial output and nothing on Serial1 output.

What am I doing wrong here? Sorry I'm all new to this.

1 Answer

+1 vote
by
Dear Ardinon, dont worry about it. Infact it is what expected and almost everyone will be confused with these initially.

This is because that on an Arduino board the primary Serial is used for USB communication with the computer, and is the only thing you can see as output on serial monitor. Note it says Serial monitor and NOT Serial1 monitor :)

For seeing the output of Serial1, you can just write the output to serial whenever a data happens on Serial1. THats it. Dont be confused. Same thing is what happening with the Software serial if you happen to use an Arduino UNO board.

Hope it helped. Code pasted below for you to check.

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

Related questions

+1 vote
2 answers 7.6k views
0 votes
2 answers 9.7k views
asked Aug 1, 2017 in Arduino by nwbi
0 votes
1 answer 8.4k views
asked Aug 5, 2016 in General by Elliot
0 votes
1 answer 637 views
asked Sep 19, 2021 in Arduino by Samurai
0 votes
1 answer 847 views
0 votes
1 answer 3.2k views
...