Serial2 on Raspberry Pi Pico

I want to use Serial1 and Serial2 in a sketch in Arduino IDE for a Raspberry pi pico.
My goal is to communicate from the pico with two other Arduino boards.

I use Arduino 1.8.9 and the official board for Raspberry Pi Pico:
"Arduino Mbed OS RP2040 Boards by Arduino version 2.1.0"

Serial2 does not seem to be configured. When I compile

    #include <Arduino.h>
    void setup() {
        Serial2.begin(9600);
    }

    void loop(){}

I get:
"undefined reference to '_UART2_'".

I found some documents (not rpi pico specific) that state you can define a second UART like this:

    #include "wiring_private.h"
    Uart mySerial (&sercom3, 1, 0, SERCOM_RX_PAD_1, UART_TX_PAD_0); // Create the new UART instance assigning it to pin 1 and 0

But that throws
" 'Uart' does not name a type; did you mean 'sqrt'?"

Does anyone have an example of how to use Serial1 and Serial2 (2 serial communications to two other boards) on a Raspberry Pi Pico using Arduino-IDE?

Thanks a lot!

Currently, Arduino-mbed RP2040 core only provides 1 serial port. Check pins_arduino.h#L41-L61

// Serial
#define PIN_SERIAL_TX (0ul)
#define PIN_SERIAL_RX (1ul)
...
#define SERIAL_HOWMANY		1

If you'd like to use 2 Serial ports, you have to either modify the core or use the Earle Philhower's arduino-pico core.

Check pins_arduino.h#L11-L15

#define PIN_SERIAL1_TX (0u)
#define PIN_SERIAL1_RX (1u)

#define PIN_SERIAL2_TX (8u)
#define PIN_SERIAL2_RX (9u)
...
#define SERIAL_HOWMANY (3u)
1 Like

I found the solution after reading Raspberry pi pico unable to use 2 hardware serial ports (Serial1&2) · Issue #210 · arduino/ArduinoCore-mbed · GitHub.

Although the proposed code did not compile as it was shown there.

UART Serial2(8, 9); // did not compile

What worked for me was:

UART Serial2(8, 9, 0, 0);

Full code:

    #include <Arduino.h>
    
    UART Serial2(8, 9, 0, 0);
    
    void setup() {
      Serial2.begin(9600);
      // ...
      Serial2.write("hello world");
    }
    
    void loop {}
2 Likes

I can confirm this solution works. Thanks for posting. I've been able to test that with a passthrough script and a wire connected between GPIO 8 and 9.

 #include <Arduino.h>
    
    UART Serial2(8, 9, 0, 0);
    

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

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

  if (Serial2.available()) {     
    Serial.write(Serial2.read());   
  }
}

P.S: Also, I tried to swap RX and TX using this code in an attempt to solve a PCB design mistake :persevere: ( using UART Serial2(9, 8, 0, 0); ) But the Pico doesn't seem to like that at all, I bricked it and I had to re-upload a uf2 file.
Does anyone know if there's a way to swap Rx and TX for these gpio 8 and 9 ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.