DYP-A02YY Waterproof sensor

I recently bought a A02YY ultrasonic sensor from Ali express. I watched videos on youtube and saw people using the same senor with the same codes but when I try it I get no output. I realized there's is A02YYUW, is it different from the DYP-A02YY. I really need help please. This is the code used:
code:

#include <SoftwareSerial.h>

// Define connections to sensor
const int pinRX = 10;
const int pinTX = 11;

// Array to store incoming serial data
unsigned char data_buffer[4] = {0};

// Integer to store distance
int distance = 0;

// Variable to hold checksum
unsigned char CS;

// Object to represent software serial port
SoftwareSerial mySerial(pinRX, pinTX);

void setup() {
  // Set up serial monitor
  Serial.begin(115200);
  //Serial.begin(9600);
  // Set up software serial port
  mySerial.begin(9600);
Serial.println("Setup complete.");
}

void loop() {

  // Run if data available
  if (mySerial.available() > 0) {
   Serial.println("Data available.");

    delay(4);

    // Check for packet header character 0xff
    if (mySerial.read() == 0xff) {
      // Insert header into array
      Serial.println("Data available2.");
      data_buffer[0] = 0xff;
      // Read remaining 3 characters of data and insert into array
      for (int i = 1; i < 4; i++) {
        data_buffer[i] = mySerial.read();
      }

      //Compute checksum
      CS = data_buffer[0] + data_buffer[1] + data_buffer[2];
      // If checksum is valid compose distance from data
      if (data_buffer[3] == CS) {
        Serial.println("Data available3.");
        distance = (data_buffer[1] << 8) + data_buffer[2];
        // Print to serial monitor
        Serial.print("distance: ");
        Serial.print(distance);
        Serial.println(" mm");
      }
    }
  }
}

I moved your topic to an appropriate forum category @kripson.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Post the data heets of sensor uesd.

initially rather than attempting to decode the value simply print out the HEX value of each byte received - this should enable you to check the protocol is as expected
you can try reading bytes - when you received 0xFF you start decoding?

The site doesn't give a data sheet but this all it says:

DETAILS

Description:

This ultrasonic ranging sensor is a non-contact distance measurement module with stable performance and high ranging accuracy featuring much smaller blind zone, wider sensing angle and a certain penetration power compared with other similar sensors. It can detect the distance between the ultrasonic wave and the target. Widely used in robot obstacle avoidance, parking management system, automatic control and so on.

Specification:

  • Average Current: <8mA

  • Operating Voltage: 3.3~5V

  • Response Time:100ms

  • Detecting Range(Flat object): 3-450cm

  • Output: URAT

  • Operating Temperature: -15~60℃

  • Reference Angle: 60º

  • Waterproof Grade: IP67

  • Measurement accuracy: ±(1 + S*0.3%)

I am kind of new to this type of sensor . I started with the HC-SRO4 before moving to this recently. I don't really understand how this one works. But I saw people using it on youtube so I thought I'd just try it since it's waterproof.

Hello kripson

How does the sensor talk to the Arduino?

Is any spec of the communication protocol available?

when I run this code I get:
ERROR

FFFFFFFF

ERROR

FFFFFFFF

ERROR

FFFFFFFF

/*
*@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](http://www.dfrobot.com),2016
* GUN Lesser General Pulic License
*@version V1.0
*@data 2019-8-28
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(A0,A2); // 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");
   Serial.print( data[0] ,HEX ); Serial.print( data[1] ,HEX ); Serial.print( data[2] ,HEX ); Serial.println( data[3] ,HEX ) ;
   delay(1000);
}
}
delay(100);
}

not used the A02YY ultrasonic sensor - I have used the SR04M

I recently updated my river monitoring system which used The Things Uno to use a TTGO ESP32 LoRa module (an ESP32 with onboard WiFi, Bluetooth Classic and BLE, a LoRa module and a OLED)

it also uses

  1. a BME280 sensor to read air temperature, pressure and humitidy
  2. a SR04M ultrasonic transducer to measure water height
  3. a DS18B20 DallasTemperature sensor ro read water temperature

it uploads data over LoRaWAN to the myDevices/cayenne desktop every 15 minutes, e.g.

the river is 15miles from the sea but is still tidal as the water level display shows (approx 10cm rise and fall)
the SR04M has been out in all weathers for over a year

think we could do with checking you are receiving data correctly
try running this code which will print the hexadecimal values of the data received

#include <SoftwareSerial.h>

// Define connections to sensor
const int pinRX = 10;
const int pinTX = 11;

// Array to store incoming serial data
unsigned char data_buffer[4] = {0};

// Integer to store distance
int distance = 0;

// Variable to hold checksum
unsigned char CS;

// Object to represent software serial port
SoftwareSerial mySerial(pinRX, pinTX);

void setup() {
  // Set up serial monitor
  Serial.begin(115200);
  //Serial.begin(9600);
  // Set up software serial port
  mySerial.begin(9600);
Serial.println("Setup complete.");
}

void loop() {

  // Run if data available
  if (mySerial.available() > 0) Serial.println((int) mySerial.read(),HEX);
  }

1 Like

try this version of your code - it waits for 0xFF before attempting to decode the value

/*
*@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](http://www.dfrobot.com),2016
* GUN Lesser General Pulic License
*@version V1.0
*@data 2019-8-28
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(A0, A2);  // RX, TX
unsigned char data[4] = {};
float distance;
void setup() {
  Serial.begin(57600);
  mySerial.begin(9600);
}
void loop() {
  static int index = 0;
  // wait for 0xFF
  if (Serial.available())
    if ((data[index] = (int)mySerial.read()) != 0xFF) return;
  Serial.println("0xFF received");
  // read next 3 bytes
  for (index = 1; index < 4; index++)
    Serial.println(data[index] = (int)mySerial.read(), HEX);
  index = 0;
  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");
    Serial.print(data[0], HEX);
    Serial.print(data[1], HEX);
    Serial.print(data[2], HEX);
    Serial.println(data[3], HEX);
    delay(1000);
  }
}

I only get this in the serial monitor:

�Setup complete.

Setup complete.

The second code gives this output:
FF

ERROR

0FFFFFF

0xFF received

FF

FF

FF

ERROR

0FFFFFF

0xFF received

FF

FF

FF

ERROR

0FFFFFF

there was an error in my code - I was reading the character from Serial no mySerial, e.g. should be

void loop() {

  // Run if data available
  if (mySerial.available() > 0) Serial.println((int) mySerial.read(),HEX);
  }

try this code (again I was checking Serial not mySerial)

/*
*@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](http://www.dfrobot.com),2016
* GUN Lesser General Pulic License
*@version V1.0
*@data 2019-8-28
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(A0, A2);  // RX, TX
unsigned char data[4] = {};
float distance;
void setup() {
  Serial.begin(57600);
  mySerial.begin(9600);
}
void loop() {
  static int index = 0;
  // wait for 0xFF
  if (mySerial.available())      // << fixed this
    if ((data[index] = (int)mySerial.read()) != 0xFF) return;
  Serial.println("0xFF received");
  // read next 3 bytes
  for (index = 1; index < 4; index++)
    Serial.println(data[index] = (int)mySerial.read(), HEX);
  index = 0;
  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");
    Serial.print(data[0], HEX);
    Serial.print(data[1], HEX);
    Serial.print(data[2], HEX);
    Serial.println(data[3], HEX);
    delay(1000);
  }
}

this is what happens when working remotly without the hardware!!

I'm still getting the same error:
FF

ERROR

FFFFFFFF

0xFF received

FF

FF

FF

ERROR

FFFFFFFF

0xFF received

FF

FF

FF

ERROR

FFFFFFFF

Hi, @kripson

Can you please post a circuit diagram of your project?

What model Arduino are you using?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

appears you are using a UNO
looking at your code you use

SoftwareSerial mySerial(A0, A2);  // RX, TX

yet the photo appears to show serial connected to pins 12 and 13

recommend you use

const byte rxPin = 2;
const byte txPin = 3;

// Set up a new SoftwareSerial object
SoftwareSerial mySerial (rxPin, txPin);

as I know it works

I did keep changing the pins. I started from the digital pins to analog and programmed it accordingly.

HI,
You are connecting Tx of the sensor to Rx pin of the UNO?
You are connecting Rx of the sensor to Tx pin of the UNO?

Tom.. :smiley: :+1: :coffee: :australia: