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.
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?
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.
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.
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
a BME280 sensor to read air temperature, pressure and humitidy
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);
}