How to connect a A02YYUW sensor to an ESP32

Here is code that I used to connect a A02YYUW ultrasonic sensor with UART to my ESP32-WROOM-32E, Im just posting it because I had to spend a long time getting it working and wasn't able to find a good starting point really, there might be ones already out there but I was able to get this put together from research before I was able to find one. Anyway here it is.

#include <HardwareSerial.h>

#define RXD2 16 // Connect the TX wire form the sensor to GPIO 16
#define TXD2 -1 // Replace the -1 with the GPIO pin you are using if you want to use the RX wire but it is not needed

HardwareSerial SerialPort(2); // Use UART2

unsigned long lastProcessTime = 0; // Last time data was processed
unsigned long lastPrintTime = 0;   // Last time data was printed
const long processInterval = 95;   // Interval for processing data (milliseconds)
const long printInterval = 1000;    // Interval for printing data (milliseconds)

float mostRecentDistance = -1; // Variable to store the most recent distance

void setup() {
  Serial.begin(115200); // Debug serial
  SerialPort.begin(9600, SERIAL_8N1, RXD2, TXD2); // Initialize UART2
  Serial.println("UART Reading Test");
}

void loop() {
  unsigned long currentMillis = millis();

  // Check if it's time to process the next data point
  if (currentMillis - lastProcessTime >= processInterval) {
    lastProcessTime = currentMillis;

    // Process data if available
    if (SerialPort.available()) {
      int header = SerialPort.read();

      if (header == 0xff) {
        // Read the next bytes
        if (SerialPort.available() >= 3) {
          int high = SerialPort.read();
          int low = SerialPort.read();
          int checksum = SerialPort.read();

          // Compute distance
          int distance = ((high << 8) + low);

          // Convert distance to cm
          mostRecentDistance = distance / 10.0;

      // Clear any remaining data in the buffer
      SerialPort.flush();
    } 
  }
  // Check if it's time to print the most recent data
  if (currentMillis - lastPrintTime >= printInterval) {
    lastPrintTime = currentMillis;

    if (mostRecentDistance >= 0) {
      Serial.print("Distance (cm): ");
      Serial.println(mostRecentDistance);
    }
  }
}

if anyone knows of any better code that deals with that data in a more efficient way then posting it here would be appreciated. Also according to a write up I found it says that if you leave the RX wire disconnected i the sensor should default to a reading every 300ms, mine defaults to a reading every 100ms, whatever yours is just make sure that the process interval (in line 10 of the code, i have it set to 95ms) is slightly faster then the sensors reading interval otherwise readings will get queued up over time and it will take the serial monitor a while to actually print a change in distance. Hopefully this is helpful to someone because it should would have been helpful to me when I got the sensor a couple days ago.

I've moved your topic as you're not using an Arduino Nano ESP32. The Arduino Nano ESP32 section is only for problems with, you guess, the Arduino Nano ESP32 :wink: