A0221AU ultrasound distance sensor

Hi all,
I bought 2 A0221AU UART ultrasonic distance sensors.
I tried both with the official arduino code on an Arduino Uno R3, checked the code several times, ... but the sensors (both were tested repeatedly) does not measure the correct distance.
Most of the time, the same distance is given (value around 1467mm), and at random moments other distances are given but never the correct distance.

I ordered both via Amazon, in reality I ordered the A02yyuw but they delivered the A0221AU instead.

Did anyone over here have the same problem and has a solution?
Thanks
G.

Please show the code and your connection diagram.

Read the forum guidelines to see how to properly ask a question and some good information on making a good post. You will get faster and better help if you post all your code as requested by the forum guidelines.

Hello,

Below the code I used for testing both sensors.

#include <SoftwareSerial.h>
int distance = 0;
int pinRX = 10;
int pinTX = 11;
unsigned char data_buffer[4] = {0};
unsigned char CS;

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

void setup() {
// put your setup code here, to run once:
Serial.begin (9600);

// Set up SoftwareSerial port
mySerial.begin(9600);
}

void loop() {

// Check if data available on mySerial port
if (mySerial.available() > 0) {
delay (4);

if (mySerial.read() == 0xff) { // detect 0xff (= delimiter) before reading the 4 data bytes
  data_buffer[0] = 0xff;
  for (int i = 1; i < 4; i++) {  // Read 4 data bytes
    data_buffer[i] = mySerial.read();
  }

  // compute CheckSum
  CS = data_buffer[0] + data_buffer[1] + data_buffer[2];
  if (data_buffer[3] == CS) {
    distance = (data_buffer[1] << 8) + data_buffer[2];
  }
}

}

if (distance >= 4000 || distance <= 30) {
Serial.println("OutOfRange");
}
else {
// Serial.print (Dip1); Serial.print (Dip2);
Serial.print (distance);
Serial.println (" mm");
}
delay(2000);
}

Please edit the code using a code tags. If you don't know how to insert the code, read the link that I posted in #2

here I have an example for the A02YYUW:

A02YYUW Ultrasonic Sensor readout with Arduino (Serial)

it seems it has the same protocol.

Hello noiasca,
Thanks for the link.
I have used that example as well, but the A0221AU sensors I have OR do not work with this code OR do not seem to work properly at all.
They both give a distance of about 1460mm - whatever I do with the sensor itself - and then for no reason start to give more realistic measurements for a while, but never a very precise measurement.
I wonder if the A0221AU sensor and the A02YYUW are exactly the same, or if there are some differences, but I cannot find any information on this question.
thanks for helping and regards,
G.

// LCD I²C library
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // creation of lcd instance with address, #characters, #lines

#include <SoftwareSerial.h>
int distance = 0;
int pinRX = 10;
int pinTX = 11;
unsigned char data_buffer[4] = {0};
unsigned char CS;

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

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);

  // Set up SoftwareSerial port
  mySerial.begin(9600);
}

void loop() {
  
  // Check if data available on mySerial port
  if (mySerial.available() > 0) {
    delay (4);
    
    if (mySerial.read() == 0xff) { // detect 0xff (= delimiter) before reading the 4 data bytes
      data_buffer[0] = 0xff;
      for (int i = 1; i < 4; i++) {  // Read 4 data bytes
        data_buffer[i] = mySerial.read();
      }

      // compute CheckSum
      CS = data_buffer[0] + data_buffer[1] + data_buffer[2];
      if (data_buffer[3] == CS) {
        distance = (data_buffer[1] << 8) + data_buffer[2];
      }
    }
  }

  if (distance >= 4000 || distance <= 30) {
    Serial.println("OutOfRange");
    }
  else {
    // Serial.print (Dip1); Serial.print (Dip2); 
    Serial.print (distance);
    Serial.println ("  mm");
  }
  delay(2000);
}

Hi b707,
I added the code in 'code tags' in #7.
Sorry for my mistake, it's the first time that I'm using the forum to get some help.
So thanks for your patience and the guidance !
regards
G.

Just letting you re-re-know that the link in Post #2 points you to one place that lets you re-know the procedure to post code in a topic that you agreed to when you signed up for this account. You got it right on the fourth try. As you learn Arduino, programming and electronics (and a bicycle or a swing set), instructions are made to be followed the first time... or things don't happen... or bad things do happen. Be well.

Erratic behavior could be due to a wiring problem, poorly soldered joints, power problem etc. Please post a wiring diagram, a photo of your setup and links to the product pages for the parts.