Uno to ESP32 sketch example for A02YYUW sensor

I'm trying to convert the following sketch which is for an Arduino Uno board to an ESP32 board and pins.

Are there pins on the ESP32 that the SoftwareSerial library would work with, or is there another approach that I need to take to read from A02YYUW sensor on an ESP32 board? Any guidance would be appreciated!

Thanks in advance,

Jordan

/*
  *@File  : DFRobot_Distance_A02.ino 
  *@Brief : This example use A02YYUW ultrasonic sensor to measure distance
  *         With initialization completed, We can get distance value 
  *@Copyright [DFRobot](https://www.dfrobot.com),2016         
  *           GUN Lesser General Pulic License
  *@version V1.0           
  *@data  2019-8-28
*/

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11,10); // RX, TX
unsigned char data[4]={};
float distance;

void setup()
{
 Serial.begin(57600);
 mySerial.begin(9600); 
}

void loop()
{
    do{
     for(int i=0;i<4;i++)
     {
       data[i]=mySerial.read();
     }
  }while(mySerial.read()==0xff);

  mySerial.flush();

  if(data[0]==0xff)
    {
      int sum;
      sum=(data[0]+data[1]+data[2])&0x00FF;
      if(sum==data[3])
      {
        distance=(data[1]<<8)+data[2];
        if(distance>30)
          {
           Serial.print("distance=");
           Serial.print(distance/10);
           Serial.println("cm");
          }else 
             {
               Serial.println("Below the lower limit");
             }
      }else Serial.println("ERROR");
     }
     delay(100);
}

Watch out for pin numbers when changing controller. Some use names like GPIOnn etc. Then You need to know if that GPIO pin provides the functionality You want. Remember to set up the IDE for the new controller.

see post ESP32 to ESP-CAM for Serial1 code for an ESP32
you should be able to connect it to a UNO using SoftwareSerial on pins 2 and 3
make sure you reduce the baudrate as SoftwareSerial will not work over 38400baud on a UNO

remember the UNO uses 5V logic and the ESP32 3.3V logic - you require a potential divider on the UNO Tx to ESP32 Rx or you can damage the ESP

any particular reason to use a UNO and ESP32 - could you not just use the ESP32?

does this help?

https://werner.rothschopf.net/microcontroller/202201_a02yyuw_ultrasonic_sensor_en.htm

I'm only trying to get the sketch above working on an esp32-wrover board.

Still no luck trying what was previously posted here.

Thanks again in advance.

Jordan

software serial does not work well with a ESP32 which has 3 hardware serial ports. Why not use one of the ESP32's hardware serial ports?

I only see Pins 1 & 3 (TX & RX) which are tied to the main Serial (USB) port, I don't see other serial pins on the board.

this

https://www.google.com/search?client=firefox-b-d&q=esp32+serials

brings me to

this Serial1 hardware pins can be programmed - see post communication-between-esp32-and-esp32-cam for an example of using Serial1 on pins 14 and 15

I'm using the esp32-wrover variant and I only see one set of Serial pins. Here is more info. on the board.

Jordan

if you look at Table 6: Description of Peripherals and Sensors the section on UARTs shows three hardware serial ports U0, U1 nad U2

see esp32-hardware-serial2-example which states

/* 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.
*/

Which developer board is being used or are you trying to use a bare ESP32 chip?


Does your board look like that?

or does it look like this,


.

It's this one;

If we take a look at this document:

and this doc

There are 3 options on the ES32 - but the wrover has limited exposed options -

UART0
UART1
UART2

UART0 - same as USB port - I need this one for Serial communications
UART1 - GPIO9, GPIO10 - see below code
UART2 - pins are unavailable - are marked as GND

Looking at this for a reference:

#include <HardwareSerial.h>

unsigned char data[4]={};
float distance;

HardwareSerial SerialPort(1);  //if using UART1

void setup()
{
 Serial.begin(115200);
 SerialPort.begin(9600, SERIAL_8N1, 4, 2); 

}

void loop()
{
  Serial.println("Starting!");

    do{
     for(int i=0;i<4;i++)
     {
       data[i]=SerialPort.read();
     }
  }while(SerialPort.read()==0xff);

  Serial2.flush();

  if(data[0]==0xff)
    {
      int sum;
      sum=(data[0]+data[1]+data[2])&0x00FF;
      if(sum==data[3])
      {
        distance=(data[1]<<8)+data[2];
        if(distance>1)
          {
           Serial.print("distance=");
           Serial.print(distance/10);
           Serial.println("cm");
          }else 
              {
                Serial.println("Below the lower limit");        
              }
      }else Serial.println("ERROR");
     }
     Serial.println("hello");
     delay(500);
}

Still no joy, just loops as ERROR. I'm connected to SD2, SD3 which map to pins 17, 18 according to this doc: Use the GPIO pins of the ESP32 – uPesy

btw - I'm open to using another ESP32 board as long as wifi works. I tried the ESP-WROOM from this vendor on Amazon;

and could not get wifi working.

What ESP wroom do others use where they can confirm wifi works?

Thanks,

Jordan

WiFi should have worked - what test did you do? did it compile? did it load? what did the Serial Monitor display?
probably the first and simplest test is to scan for nearby WiFi networks as suggested in getting-started-with-esp32
I have a number of ESP32 boards but mainly use the NodeMCU ESP32 with onboard WiFi, Bluetooth classic 2.0 and BLE - also hardware serial ports Serial1 and Serial2 work OK

Oh, ESP32 CAM. That's different.

Yes WiFi should work with a ESP32 CAM. Did you properly set up the FTDI interface?

Post an image or 2 of your project.

Thanks everyone for the pointers.

I finally got everything working (including my wifi) on the esp-wroom-32 board with essentially...

#include <HardwareSerial.h>

#define RXD2 16
#define TXD2 17

HardwareSerial SerialPort(2); // use UART2
...
SerialPort.begin(9600, SERIAL_8N1, RXD2, TXD2);
...

Part of the problem was the example code that DFRobot provided on this page

was somehow never able to read successfully so I assumed the Serial port was not up.

I rewrote the serial reader essentially to get it working consistently. Not sure if anyone else has had a similar problem but if you are interested let me know.

Thank you all again!

Regards,

Jordan

Hiya Mate. Attempted to DM you but couldn't find such a function. I was hoping you could provide me some assistance with a similar matter. I'm stumped on how to interface with the A02YYUW sensor (with an ESP32) given i need to use the Software Serial function. I simply cannot get an accurate readout from the sensor. Any advice/ insight?

All im trying to do at the moment is get a simple print-out of the distance reading, as the sample script does.

TIA!

does this code help you? these commands should work, but I don't want to paste all the rest of my business logic....

#include <HardwareSerial.h>

HardwareSerial SerialPort(2); // use UART2

SerialPort.begin(9600, SERIAL_8N1, RXD2, TXD2);

while (SerialPort.available()) {
int header = SerialPort.read();
if (header == 0xff) {
high = SerialPort.read();
low = SerialPort.read();
checksum = SerialPort.read();
SerialPort.flush();
distance = ((high<<8)+low);
}
}

Serial.print("header: ");

Serial.println(header);
Serial.print("high: ");
Serial.println(high);
Serial.print("low: ");
Serial.println(low);
Serial.print("checksum: ");
Serial.println(checksum);
Serial.print("Distance mm: ");
Serial.println(distance);
delay(1000);

1 Like

Jordan,
Thank you for your response! Unfortunately, while it didn't solve my issues, I do believe your code has helped point me in the right direction. In an effort to not de-rail your thread further, I will be creating a new thread with my issues. Figers crossed I can solve them! Thanks again.

Ok. Glad it helped and good luck!