2

How can I declare multiple serial ports for Arduino Mega as it has been in the example below?

#include <SoftwareSerial.h>
SoftwareSerial mySerial(18,19); // RX, TX  //how this syntax can be modified for multiple Rx module?

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

void loop(){
   Serial.write(45);    // send a byte with the value 45

   int bytesSent = Serial.write(“helloâ€); //send the string “hello†and return the length of the string.
}
3
  • 4
    The Mega has four UARTs. Why are you using SoftwareSerial? Commented Apr 27, 2017 at 11:50
  • @IgnacioVazquez-Abrams..... But how i can access four Softwareserial for Arduino mega?like syntax for single serial port is as, SoftwareSerial mySerial(18,19); // RX, TX Commented Apr 28, 2017 at 10:49
  • @Code Gorilla But how i can access four Softwareserial for Arduino mega?like syntax for single serial port is as, SoftwareSerial mySerial(18,19); // RX, TX – Hafeez Ahmad Fii Zue 2 mins ago edit Commented Apr 28, 2017 at 10:55

2 Answers 2

5

If you mean the built in serial ports then just user the other objects that are pre-declared as per:

https://www.arduino.cc/en/Serial/Begin

0
5

From the official docs on serial:

The Arduino Mega has three additional serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).

Which means, you use Serial1 for serial on pins 19 and 18 instead of Serial:

void setup(){
   Serial1.begin(9600);
}

void loop(){
   Serial1.write(45);    // send a byte with the value 45

   int bytesSent = Serial1.write("hello"); //send the string "hello" and return the length of the string.
}
5
  • what will be the syntax for serial two and three if it is "SoftwareSerial mySerial(18,19)" for serial 1 Commented Apr 28, 2017 at 15:21
  • Isn't is obvious? Did you read the quoted text?
    – gre_gor
    Commented Apr 28, 2017 at 15:22
  • how can we access more than two Softwareserial? Commented Apr 28, 2017 at 15:23
  • 2
    Why do you need software serials if you have 4 hardware serials?
    – gre_gor
    Commented Apr 28, 2017 at 15:24
  • 1
    can i share you the complete code so that you can amend it easily? i need yours email id? Commented Apr 28, 2017 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.