Serial communication between two RP2040 (Arduino Framework)

These are simple examples to implement Serial Communication, between Raspberry Pi Pico and WeAct RP2040 with 128x128 SH1107 SPI OLED.


	Connection between two RP2040s

	+-----------------------------------+
	|				    |
	|    +----------------------------------+
	|    |			            |   |
	|    | +------------------------------------+
	|    | |			    |	|   |
	|    | |         +---------------+  |	|   |		+---------------+
	|    +-|-TX  GP0 | RPi Pico	 |  |	|   +---TX  GP0 | WeAct RP2040	|
	|      +-RX  GP1 |		 |  |	+-------RX  GP1	|		|
	+------------GND |		 |  +---------------GND	|		|
			 |				        |		|
	
        Connection:				
	Raspberry Pi	WeAct RP2040
	Pico
	============================
	TX(GP0)		RX(GP1)
	RX(GP1)		TX(GP0)
	GND		GND
Example Code:

SerialPassthrough_9600N81.ino, run on Raspberry Pi Pico.
/*
  Serial (UART) exercise run on Raspberry Pi Pico/RP2040.

  Actually, it's from Arduino Examples > Communiction > SerialPassthrough
  ref:
  https://www.arduino.cc/en/Tutorial/BuiltInExamples/SerialPassthrough
*/

void setup() {
  delay(2000);
  Serial.begin(9600);
  delay(200);
  Serial1.begin(9600, SERIAL_8N1);  //actually, it's default
  Serial.println("\n--- Serial exercise run on Raspberry Pi Pico/RP2040 ---");
  Serial.println("SERIAL1_TX: " + String(SERIAL1_TX));
  Serial.println("SERIAL1_RX: " + String(SERIAL1_RX));
  Serial.println("-------------------------------------------------------");
}

void loop() {
  if (Serial.available()) {      // If anything comes in Serial (USB),
    Serial1.write(Serial.read());   // read it and send it out Serial1 (pins 0 & 1)
  }

  if (Serial1.available()) {     // If anything comes in Serial1 (pins 0 & 1)
    Serial.write(Serial1.read());   // read it and send it out Serial (USB)
  }
}



Serial_9600N81_SH1107.ino, run on WeAct RP2040 with SH1107 OLED.
For display part, refer to the post"Display on 128x128 SH1107 SPI OLED with RP2040 (Arduino Framework) using U8g2 lib".
/*
  Serial (UART) exercise run on Raspberry Pi Pico/RP2040.
  with 128x128 SH1107 SPI OLED, to display RX from Serial1.

  ref:
  Examples > U8g2 > page_buffer > Serial
*/

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>

U8G2_SH1107_PIMORONI_128X128_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 22, /* dc=*/ 21, /* reset=*/ 20);
//U8G2_SH1107_SEEED_128X128_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 22, /* dc=*/ 21, /* reset=*/ 20);

#define U8LOG_WIDTH 13
#define U8LOG_HEIGHT 7
uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT];
U8G2LOG u8g2log;

void setup() {
  delay(2000);
  Serial.begin(9600);

  u8g2.begin();  
  u8g2.setFont(u8g2_font_10x20_tr);  // set the font for the terminal window
  u8g2log.begin(u8g2, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer);
  u8g2log.setLineHeightOffset(0); // set extra space between lines in pixel, this can be negative
  u8g2log.setRedrawMode(1);   // 0: Update screen with newline, 1: Update screen for every char  
  
  delay(200);
  Serial1.begin(9600, SERIAL_8N1);  //actually, it's default
  Serial.println("\n--- Serial exercise run on Raspberry Pi Pico/RP2040 ---");
  Serial.println("    with 128x128 SH1107 SPI OLED");
  Serial.println("SERIAL1_TX: " + String(SERIAL1_TX));
  Serial.println("SERIAL1_RX: " + String(SERIAL1_RX));
  Serial.println("-------------------------------------------------------");
}

void loop() {
  if (Serial.available()) {      // If anything comes in Serial (USB),
    Serial1.write(Serial.read());   // read it and send it out Serial1 (pins 0 & 1)
  }

  if (Serial1.available()) {     // If anything comes in Serial1 (pins 0 & 1)
    char serial1_In = Serial1.read();
    u8g2log.print(serial1_In); 
    Serial.write(serial1_In);   // read it and send it out Serial (USB)
  }
}

Next:
Bluetooth serial communication between RP2040 using HC-04 Bluetooth Module, in BLE/SPP mode.


Comments

Popular posts from this blog

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.

CameraWebServe: ESP32-S3 (arduino-esp32) + OV5640 camera module