Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial Communication between two ESP32

Tags:

esp32

I have found examples of basic arduino to arduino serial communication but have been unable to get those working on ESP32 boards. I am trying to make the same thing work between two ESP32's The two are connected:

esp1   esp2
gnd to gnd
tx2 to rx2
rx2 to tx2

Simple sketches:

//transmit sketch
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("test...");
  delay(1000);
}

//receive sketch
void setup() {
  Serial.begin(9600);
}

void loop() {
  String received = "";
  while (Serial.available())
  {
    received = Serial.read();
    Serial.println(received);
  }
}

What else is required to make this work?

like image 653
Roger Avatar asked Mar 12 '26 17:03

Roger


1 Answers

I think your code comes from a simpler world in which pins were always fixed and one UART was all you had available. With the ESP32 you should probably look for a solution more along these lines:

#include <HardwareSerial.h>

HardwareSerial Serial2(2); // use uart2

Serial2.begin(19200, SERIAL_8N1, 16, 17); // pins 16 rx2, 17 tx2, 19200 bps, 8 bits no parity 1 stop bit

I hope this helps. If you still have problems after this, they're likely to be either a) the board you're using doesn't use 16 & 17 for rx2 & tx2, or b) you need 10k pull-up (not series) resistors on both lines to stop them "floating" - however some boards take care of the pull-ups for you.