I would like to use the Serialports 1 and 2 and cannot find a example-sketch.
It would be great, if anybody could give me a short, but complete sketch, that uses the Serial1 and the Serial2.
Hardware or software? What board?
This seems to work using the Philhower core, but not the Arduino MBed core (which complains about not having _UART2
)
void setup() {
Serial.begin(9600); // USB Serial
Serial1.begin(300); // UART0
Serial2.begin(300); // UART1
while (!Serial)
;
}
void loop() {
Serial.println("Serial xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
Serial2.println("Serial 2 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@xx");
delay(200);
Serial1.println("Serial 1 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@xx");
delay(600);
}
1 Like
try this for Serial1
// Raspberry Pi Pico RP2040 (and W) Serial1 test - for loopback test connect pins Rx GP1 and Tx GP0
// Note (also on RPi Pico W)
// Serial is the USB serial for program loading and serial monitor
// there are two hardware serial ports UART0 and UART1
// Serial1 is mapped to UART0 on Rx pin GP1 Tx pin GP0
// Serial2 is mapped to UART1 on Rx pin GP5 Tx pin GP4
// for RS232 shield connect - note RP2040 uses 3.3V logic
// RP2040 UART0 RX GP1 to TTL/RS232 Rx
// RP2040 UART0 TX GP0 to TTL/RS232 Tx - GP0 is Tx checked with oscilloscope
// connect GND pins together and VCC to 3.3V on RP2040
// if connecting VCC to 5V use a potential divider on TTL/RS232 Tx to RP2040 RX
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)
#include <Arduino.h>
void setup() {
// initialize both serial ports:
delay(5000);
Serial.begin(115200);
Serial1.begin(9600); //115200);
Serial.println();
Serial.println("\n\nRaspberry Pi Pico RP2040 Serial1 test Rx pin GP1 Tx pin GP0");
Serial.write(" for loopback test connect pin 0 to pin 1\n");
Serial.printf("RS232: RP2040 UART0 GP1 RX to TTL/RS232 Rx and RP0 TX to TTL/RS232 Tx\n");
Serial.printf("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
//Serial.write('>');
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
//Serial.write(inByte);
Serial1.write(inByte);
}
}
serial monitor output communicating with an ESP32
Raspberry Pi Pico RP2040 Serial1 test Rx pin GP1 Tx pin GP0
for loopback test connect pin 0 to pin 1
hello from ESP32
ESP32 test 1234567890
ESP32 serial1 test Rx pin 16 Tx pin 17
for loopback test connect pin 16 to pin 17
hello from RPi Pico
RPi pico test 1234567
for Serial2 try
// Raspberry Pi Pico RP2040 (and W) Serial2 test - for loopback test connect pins Tx GP4 and Rx GP5
#include <Arduino.h>
// Note (also on RPi Pico W)
// Serial is the USB serial for program loading and serial mointor
// there are two hardware serial ports UART0 and UART1
// Serial1 is mapped to UART0 on Rx pin GP1 Tx pin GP0
// Serial2 is mapped to UART1 on Rx pin GP5 Tx pin GP4
#define TX2 4
#define RX2 5
void setup() {
// initialize both serial ports:
Serial.begin(115200);
delay(2000);
Serial.println("\n\nRaspberry Pi Pico RP2040 serial2 test Rx pin GP5 Tx pin GP4 ");
Serial.write(" for loopback test connect pin GP4 to pin GP5\n");
Serial2.setTX(TX2);
Serial2.setRX(RX2);
Serial2.begin(115200);
Serial.print(" Serial2 RX2 is on pin GP " + String(RX2));
Serial.println(" TX2 pin: " + String(TX2));
}
void loop() {
// read from port 1, send to port 0:
if (Serial2.available()) {
int inByte = Serial2.read();
Serial.write('>');
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
//Serial.write(inByte);
Serial2.write(inByte);
}
}