0

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!

2 Answers 2

2

I found the solution after reading https://github.com/arduino/ArduinoCore-mbed/issues/210#issuecomment-849502556.

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 {}
1

I'm not using the "Mbed" board, I'm using the "Raspberry Pi Boards(1.9.7)" --> "Raspberry Pi Pico" board, and found that:

Serial1 uses hardware UART0

Serial2 uses hardware UART1

Serial1.begin(SOME_BAUDRATE); // defaults to TX on GP0 and RX on GP1

Serial2.begin(SOME_BAUDRATE); // defaults to TX on GP8 and RX on GP9

To change the default UART pins to other option, as long as it comply with the Raspberry Pi Pico pinout (look for UARTx_TX / UARTx_RX next to the pins), see the following example:

#include <hardware/pio.h>

gpio_set_function(12, GPIO_FUNC_UART); // change the TX default for Serial1 (UART0)
                                       // to be GP12

gpio_set_function(13, GPIO_FUNC_UART); // change the RX default for Serial1 (UART0)
                                       // to be GP13
1
  • Completely different situation since you're using a different board.
    – LarryBud
    Dec 21, 2023 at 19:41

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.