Arduino Zero Serial2

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Kimlorentz
 
Posts: 191
Joined: Mon Jun 23, 2014 7:11 am

Arduino Zero Serial2

Post by Kimlorentz »

Many people have seen the new Arduino Zero/M0 Pro and this is a good low power chip.
But this only have 1 serial port to connect your serial data devices, Serial1.
But I found a way around it, I got Serial2 to work, Serial3 and Serial4 is not possible right now but I will find out if its possible.

Here is a simple code for sending Serial2 data to serial port and make it possible to send data to serial2 from serial port.

I used an Adafruit Ultimate GPS for this testing.

Code: Select all

// Serial2 pin and pad definitions (in Arduino files Variant.h & Variant.cpp)
#define PIN_SERIAL2_RX       (34ul)               // Pin description number for PIO_SERCOM on D12
#define PIN_SERIAL2_TX       (36ul)               // Pin description number for PIO_SERCOM on D10
#define PAD_SERIAL2_TX       (UART_TX_PAD_2)      // SERCOM pad 2
#define PAD_SERIAL2_RX       (SERCOM_RX_PAD_3)    // SERCOM pad 3

// Instantiate the Serial2 class
Uart Serial2(&sercom1, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);

void setup()
{
  Serial.begin(115200);
  Serial2.begin(9600);          // Begin Serial2
}

void loop()
{ 
  if (Serial.available())
  {
    byte byteRead2 = Serial.read();
    Serial2.write(byteRead2);
  }
  if (Serial2.available())        // Check if incoming data is available
  {
    byte byteRead = Serial2.read();    // Read the most recent byte 
    Serial.write(byteRead);      // Echo the byte back out on the serial port
  }
}

void SERCOM1_Handler()    // Interrupt handler for SERCOM1
{
  Serial2.IrqHandler();
}
Here is a code to read data from Serial2 and save it to a string and print the string to Serial.
It also sets the GPS to 1Hz update and RMC +GGA data.

Code: Select all

// Serial2 pin and pad definitions (in Arduino files Variant.h & Variant.cpp)
#define PIN_SERIAL2_RX       (34ul)               // Pin description number for PIO_SERCOM on D12
#define PIN_SERIAL2_TX       (36ul)               // Pin description number for PIO_SERCOM on D10
#define PAD_SERIAL2_TX       (UART_TX_PAD_2)      // SERCOM pad 2
#define PAD_SERIAL2_RX       (SERCOM_RX_PAD_3)    // SERCOM pad 3

// Instantiate the Serial2 class
Uart Serial2(&sercom1, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  
  // initialize serial:
  Serial.begin(115200);
  Serial2.begin(9600);
  delay(1000);
  Serial2.println("$PMTK220,1000*1F");  // 1 Hz
  delay(100);
  Serial2.println("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"); // RMCGGA
  delay(100);
  Serial2.println("$PMTK286,1*23");
  delay(100);
  Serial2.println("$PGCMD,33,0*6D");
  delay(100);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {

   serialEvent();

   // print the string when a newline arrives:
   if (stringComplete) {
     Serial.print(inputString);
     // clear the string:
     inputString = "";
     stringComplete = false;
      }
}

void SERCOM1_Handler()    // Interrupt handler for SERCOM1
{
  Serial2.IrqHandler();
}

void serialEvent() {
  while (Serial2.available()) {
    //SerialUSB.println("Serial1 available");
    // get the new byte:
    char inChar = (char)Serial2.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
      //SerialUSB.println("String is complete");
    }
  }
}
Whit the use of Serial2 you can now connect somthing like Adafruit FONA on Serial1 and GPS on Serial2. If you use FONA 808 you dont need 2 serial ports, but if you only got the 800L you need 2 serial ports, one for FONA and one for GPS.

User avatar
Kimlorentz
 
Posts: 191
Joined: Mon Jun 23, 2014 7:11 am

Re: Arduino Zero Serial2

Post by Kimlorentz »

On the link below you can se where I got my info.
Link: http://forum.arduino.cc/index.php?topic=341054.0

He also made 2 complete files that makes it possible to use Serial 2 and Serial 3.
variant.cpp
varianlt.cpp
(19.13 KiB) Downloaded 439 times
variant.h
variant.h
(7.09 KiB) Downloaded 437 times
You find the files at C:/user/"yourname"/AppData/Local/Arduino15/packages/arduino/hardware/samd/1.6.2/variants/arduino_zero


Here are Demo for using Serial 2
digital pins 12 (Rx) and 10 (Tx) - Connect pin 12 to GPS TX and pin 10 to GPS RX

Code: Select all

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

void loop()
{ 
  if (Serial.available())
  {
    byte byteRead = Serial.read();
    Serial2.write(byteRead);
  }
  if (Serial2.available())
  {
    byte byteRead = Serial2.read();
    Serial.write(byteRead);
  }
}

Here are Demo for using Serial 3
digital pins 5 (Rx) and 2 (Tx) on the Zero - Connect pin 5 to GPS TX and pin 2 to GPS RX
digital pins 5 (Rx) and 4 (Tx) on the M0 Pro - Connect pin 5 to GPS TX and pin 4 to GPS RX

Code: Select all

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

void loop()
{ 
  if (Serial.available())
  {
    byte byteRead = Serial.read();
    Serial3.write(byteRead);
  }
  if (Serial3.available())
  {
    byte byteRead = Serial3.read();
    Serial.write(byteRead);
  }
}

User avatar
adafruit2
 
Posts: 22148
Joined: Fri Mar 11, 2005 7:36 pm

Re: Arduino Zero Serial2

Post by adafruit2 »

hey thanks! yes its neat you can have multiple SPI & Serial ports :D

User avatar
olavopm
 
Posts: 5
Joined: Mon Jan 16, 2017 5:44 pm

Re: Arduino Zero Serial2

Post by olavopm »

I´ve added these variands to the ADAFRUIT (not arduino) package in my APP DATA, but I cannot declare Serial2

This is the error I get.

How can I declare Serial2 in a adafruit M0 proto feather?

Thank you very much!

Code: Select all


In file included from C:\Users\Monken\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.0.13\cores\arduino/Arduino.h:126:0,

                 from C:\Users\Monken\AppData\Local\Temp\arduino_build_952356\sketch\sketch_feb19b.ino.cpp:1:

C:\Users\Monken\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.0.13\cores\arduino/USB/USBAPI.h:174:16: error: conflicting declaration 'Serial_ Serial'

 extern Serial_ Serial;

                ^

In file included from C:\Users\Monken\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.0.13\cores\arduino/delay.h:27:0,

                 from C:\Users\Monken\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.0.13\cores\arduino/Arduino.h:77,

                 from C:\Users\Monken\AppData\Local\Temp\arduino_build_952356\sketch\sketch_feb19b.ino.cpp:1:

C:\Users\Monken\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.0.13\variants\arduino_zero/variant.h:186:13: error: 'Serial' has a previous declaration as 'Uart Serial'

 extern Uart Serial;

             ^

exit status 1
Error compiling for board Adafruit Feather M0 (Native USB Port).

User avatar
Kimlorentz
 
Posts: 191
Joined: Mon Jun 23, 2014 7:11 am

Re: Arduino Zero Serial2

Post by Kimlorentz »

I have not tested that, sorry.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”