Just wondering what i am do wrong with arduino nano esp32 uart

I connected the TX1 of the Teensy to the RX0 of the nano esp32 and the RX1 of the Teensy to the TX1 on the nano esp32. I uploaded the following code but the NANO ESP32 output nothing. On the teensy code

void setup() {
  Serial.begin(19200);  // Start serial communication at 19200 bps to match the ESP32 setup
  pinMode(A0, INPUT);   // Set A0 as an input for analog readings
}

void loop() {
  int sensorValue = analogRead(A0);  // Read the analog value from pin A0
  Serial.print("Sensor Value: ");    // Prefix for clarity
  Serial.println(sensorValue);       // Send this value over UART, followed by a newline

  delay(1000);  // Send data every second
}

the monitor is


Sensor Value: 329
Sensor Value: 320
Sensor Value: 308
Sensor Value: 297

On the Nano ESP32, the code is

void setup() {
  // Initialize Serial for debugging
  Serial.begin(19200);  // Change this baud rate if different speeds are required for debugging
  Serial.println("ESP32 UART Debugging Initialized");

  // Initialize Serial2 for main communication
  const int RX2PIN = 16;
  const int TX2PIN = 17;
  Serial2.begin(19200, SERIAL_8N1, RX2PIN, TX2PIN);
  Serial.println("ESP32 UART2 Receiver Initialized on custom pins");
}

void loop() {
  // Check for incoming data on Serial2
  if (Serial2.available()) {
    String incoming = "";
    while (Serial2.available()) {
      delay(2);  // A small delay to buffer the data
      char c = Serial2.read();  // Read incoming character
      incoming += c;  // Append character to string
    }
    if (incoming.length() > 0) {
      Serial.print("Received on UART2: ");
      Serial.println(incoming);
    }
  }
}

and it prints nothing on the serial monitor

Did you connect the grounds of the two?

And, why then is the ESP32 code looking at Serial2?
I'm not an ESP32 Nano user, but that doesn't sound right.

Yes, I connected the grounds.

I tried all the Serial0, Serial1 and Serial2

looking at the ESP32 nano pinout I cannot see GPIO16?
try this simple loopback test using GPIO 17 and 18

// ESP32 serial2 hardware loop back test

// see https://circuits4you.com/2018/12/31/esp32-hardware-serial2-example/
/* There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD.
 * 
 * U0UXD is used to communicate with the ESP32 for programming and during reset/boot.
 * U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though
 * U2UXD is unused and can be used for your projects.
*/


#define RXD2 18
#define TXD2 17

void setup() {
  // Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
  Serial.println("ESP32 hardware serial test on Serial2");
  Serial.println("Serial2 Txd is on pin: " + String(TXD2));
  Serial.println("Serial2 Rxd is on pin: " + String(RXD2));
  Serial.println("For loopback test connect Txd(pin " + String(TXD2) +
                 ") to Rxd(pin " + String(RXD2) + ")");
}

void loop() {  //Choose Serial1 or Serial2 as required
  while (Serial2.available()) {
    Serial.print(char(Serial2.read()));
  }
  while (Serial.available()) {
    Serial2.print(char(Serial.read()));
  }
}

connect 17 to 18 - text entered on keyboard should be echoed to display, e.g.

ESP32 hardware serial test on Serial2
Serial2 Txd is on pin: 17
Serial2 Rxd is on pin: 18
For loopback test connect Txd(pin 17) to Rxd(pin 18)
test1 abcdef

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.