+2 votes
6.4k views
asked 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
answered 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

0 votes
0 answers 4.4k views
0 votes
2 answers 6.8k views
asked Jul 31, 2017 in Arduino by nwbi
0 votes
1 answer 6.5k views
asked Aug 5, 2016 in General by Elliot
–1 vote
0 answers 4.7k views
asked Apr 6, 2017 in General by larrystanley (120 points)
–1 vote
0 answers 6.1k views
asked Apr 6, 2017 in Modules by larrystanley (120 points)
...