コンテンツにスキップ

Networking Sample by Serial Communication#

  • Author: Yosuke Tsuchiya (Fab Lab Kamakura)
  • Date created: 4/1/2026

This is some simple example for networking and communciation in each devices.

Serial Multi-drop#

The easiest implementation of networking is serial multi-drop. Here is the diagram.

Left side is the master devices. All TX line of slave devices connect to Master’s RX line, and all RX line of slave devices connect to Master’s TX line. And, don’t forget to connect each GND lines.

Here is the schema using XIAO RP2040, XIAO ESP32S3 and Pico2.

Here is the code for master device. The important point is to use “Serial1” for UART communication with TX(6) and RX(7) pins.

void setup() {
  // put your setup code here, to run once:
  Serial1.setTX(0);
  Serial1.setRX(1);
  Serial1.begin(115200);
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  //Serial.println(Serial1.available());
  //if(Serial1.available() == true){
  char buf = '1';
  Serial1.write(buf);
  Serial.println(buf);
  delay(10000);
  buf = '2';
  Serial1.write(buf);
  Serial.println(buf);
  delay(10000);
  //}
}

Here is the code for slave device. The code will blink LED when receiving their own address via serial communication. Here the most important point is to set the same serial baudrate with master device. Here, I set 115200bps in each devices.

char addr = '2';

void setup() {
  // put your setup code here, to run once:
  //Serial1.begin(115200,SERIAL_8N1,D7,D6);
  Serial1.begin(115200);
  Serial.begin(115200);
  pinMode(2,OUTPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  //Serial.println(Serial1.available());
  if(Serial1.available() == true){
    char buf = Serial1.read();
    Serial.println(buf);
    if (buf == addr){
      digitalWrite(2,HIGH);
      delay(10000);
      digitalWrite(2,LOW);
    }

  }

}

Here is the result of above code. You can see the LED blink alternately in 10 second intervals.

I2C#

In case you want to connect a lot of devices (including sensors and acctuators), you can use I2C networking.

Here is the case for connecting two XIAO (RP2040 and ESP32S3) and Raspberry Pi Pico2

Schemas#

  • Connect each SDA and SCL lines.
  • Add Pull-up Register on both SDA/SCL lines.
  • Each devices GND should be connected each other
  • If you use USB connection in master, you can use VBUS line as power supply for slave devices.

In this schema, we define slave 1 address as “8”, and slave 2 address as “9”.

Here is the real wiring sample.

Here is the Arduino code for master device.

#include "Wire.h"

//define the address
#define SLAVE_ADDR 8
#define SLAVE_ADDR2 9

#define ANSWERSIZE 5

void setup() {
// put your setup code here, to run once:
  Wire.begin();
  Serial.begin(9600);

  Serial.println("I2C Master");

}

void loop() {
// put your main code here, to run repeatedly:
  delay(50);

  Serial.println("Write data to Slave");
  /*
  Wire.beginTransmission(SLAVE_ADDR);
  Wire.write(0);
  Wire.endTransmission();
  Serial.println("Receive Data");
  Wire.requestFrom(SLAVE_ADDR, ANSWERSIZE);
  */
  String resp = " ";
  transData(SLAVE_ADDR, resp);
  Serial.println(resp);

  transData(SLAVE_ADDR2, resp);
  Serial.println(resp);
}

void transData(int s_addr, String response){
  Wire.beginTransmission(s_addr);
  Wire.write(0);
  Wire.endTransmission();

  Serial.print("Receive Data from");
  Serial.println(s_addr);
  Wire.requestFrom(s_addr, ANSWERSIZE);

  // String response = " ";
  response = " ";
  while(Wire.available()){
    char b = Wire.read();
    Serial.print(b);
    response += b;
  }
}

Here is the code for slave 1 device.

#include "Wire.h"

//define the address
#define SLAVE_ADDR 8

#define ANSWERSIZE 5

String answer = "Hello";

void setup() {
  // put your setup code here, to run once:
  Wire.begin(SLAVE_ADDR);

  Wire.onRequest(requestEvent);

  Wire.onReceive(receiveEvent);

  Serial.begin(9600);
  Serial.println("I2C Slave Demonstration");

}

void receiveEvent(int howbyte){
  Serial.println(Wire.available());
  while(0 < Wire.available()){
    byte x = Wire.read();
  }

  Serial.println("Receive Event");
}

void requestEvent(){
  byte response[ANSWERSIZE];

  for(byte i=0;i<ANSWERSIZE; i++){
    response[i] = (byte)answer.charAt(i);
  }
    Wire.write(response, sizeof(response));
    Serial.println("Request Event");

}

void loop() {
  // put your main code here, to run repeatedly:
  delay(50);
  //Serial.println(Wire.available());

}

Here is the code for slave 2. The difference with slave 1 is the definition of SLAVE_ADDRESS.

#include "Wire.h"

//define the address
#define SLAVE_ADDR 9

#define ANSWERSIZE 5

String answer = "Hello";

void setup() {
  // put your setup code here, to run once:
  Wire.begin(SLAVE_ADDR);

  Wire.onRequest(requestEvent);

  Wire.onReceive(receiveEvent);

  Serial.begin(9600);
  Serial.println("I2C Slave Demonstration");

}

void receiveEvent(int howbyte){
  Serial.println(Wire.available());
  while(0 < Wire.available()){
    byte x = Wire.read();
  }

  Serial.println("Receive Event");
}

void requestEvent(){
  byte response[ANSWERSIZE];

  for(byte i=0;i<ANSWERSIZE; i++){
    response[i] = (byte)answer.charAt(i);
  }
    Wire.write(response, sizeof(response));
    Serial.println("Request Event");

}

void loop() {
  // put your main code here, to run repeatedly:
  delay(50);
  //Serial.println(Wire.available());

}
Here is the result of the I2C communication. You can see both slave devices replied with “hello”.