ESP32 A02YYUW Waterproof Ultrasonic Distance Sensor
The A02YYUW is a waterproof ultrasonic distance sensor ideal for outdoor and industrial applications. With a measuring range of up to 4.5 meters and a durable build, it is perfect for detecting objects in harsh conditions. It operates using a UART interface and is compatible with microcontrollers like Arduino and ESP32.
Jump to Code Examples
Quick Links
A02YYUW Price
About A02YYUW Waterproof Ultrasonic Distance Sensor
The A02YYUW is a waterproof ultrasonic distance sensor capable of measuring distances from 3 cm to 450 cm. Its robust design, featuring an IP67-rated waterproof and dustproof enclosure, makes it suitable for harsh and moist environments. The sensor operates using a UART interface, providing reliable distance measurements for applications such as liquid level detection, obstacle avoidance, and proximity sensing.A02YYUW Sensor Technical Specifications
Below you can see the A02YYUW Waterproof Ultrasonic Distance Sensor Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: UART
- Measurement Range: 3 cm to 450 cm
- Resolution: 1 mm
- Accuracy: ±1 cm
- Operating Voltage: 3.3V to 5V DC
- Average Current: <8 mA
- Response Time: 100 ms
- Interface: UART
- Operating Temperature: -15°C to 60°C
- Waterproof Grade: IP67
A02YYUW Sensor Pinout
Below you can see the pinout for the A02YYUW Waterproof Ultrasonic Distance Sensor. The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
The A02YYUW pinout is as follows:
- VCC: Connect to a 3.3V to 5V power supply.
- GND: Ground connection.
- RX: UART receive pin for communication.
- TX: UART transmit pin for communication.
A02YYUW Wiring with ESP32
Below you can see the wiring for the A02YYUW Waterproof Ultrasonic Distance Sensor with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
VCC
to a 3.3V or 5V power source, GND
to ground, TX
to the microcontroller's RX pin, and RX
to the microcontroller's TX pin. Ensure that the UART communication parameters are set to 9600 baud rate, 8 data bits, 1 stop bit, and no parity.Code Examples
Below you can find code examples of A02YYUW Waterproof Ultrasonic Distance Sensor with ESP32 in several frameworks:
ESP32 A02YYUW Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the A02YYUW Waterproof Ultrasonic Distance Sensor:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5, 18); // RX, TX
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
Serial.println("A02YYUW Distance Sensor Example");
}
void loop() {
if (mySerial.available() >= 4) {
uint8_t data[4];
for (int i = 0; i < 4; i++) {
data[i] = mySerial.read();
}
if (data[0] == 0xFF) {
int sum = (data[0] + data[1] + data[2]) & 0xFF;
if (sum == data[3]) {
int distance = (data[1] << 8) + data[2];
if (distance > 30) {
Serial.print("Distance: ");
Serial.print(distance / 10.0);
Serial.println(" cm");
} else {
Serial.println("Below the lower limit");
}
} else {
Serial.println("Checksum error");
}
}
}
delay(100);
}
This Arduino sketch demonstrates how to use the A02YYUW sensor for distance measurement. It utilizes the SoftwareSerial library to communicate with the sensor via UART. The mySerial
object is initialized on pins 5 (RX) and 18 (TX) with a baud rate of 9600. In the loop()
, the code checks if at least 4 bytes are available in the serial buffer, reads them into an array, and verifies the header and checksum. If valid, it calculates the distance in millimeters, converts it to centimeters, and prints the result to the Serial Monitor.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 A02YYUW ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the A02YYUW Waterproof Ultrasonic Distance Sensor, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#include <stdio.h>
#include "driver/uart.h"
#include "esp_log.h"
#define UART_NUM UART_NUM_1
#define TXD_PIN (GPIO_NUM_17)
#define RXD_PIN (GPIO_NUM_16)
void init(void) {
const uart_config_t uart_config = {
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(UART_NUM, &uart_config);
uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(UART_NUM, 1024 * 2, 0, 0, NULL, 0);
}
void app_main(void) {
init();
uint8_t data[4];
while (1) {
int len = uart_read_bytes(UART_NUM, data, 4, 20 / portTICK_RATE_MS);
if (len == 4 && data[0] == 0xFF) {
int sum = (data[0] + data[1] + data[2]) & 0xFF;
if (sum == data[3]) {
int distance = (data[1] << 8) | data[2];
if (distance > 30) {
printf("Distance: %.2f cm\n", distance / 10.0);
} else {
printf("Below the lower limit\n");
}
} else {
printf("Checksum error\n");
}
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
This ESP-IDF code configures the UART interface to communicate with the A02YYUW sensor. The UART is initialized with parameters such as a 9600 baud rate, 8 data bits, 1 stop bit, and no parity. The uart_read_bytes()
function reads 4 bytes of data from the sensor, checks if the header byte is 0xFF
, and verifies the checksum. If the data is valid, the code calculates the distance in millimeters, converts it to centimeters, and prints the result. A delay of 100 ms is added between readings to ensure the sensor operates reliably.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 A02YYUW ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the A02YYUW Waterproof Ultrasonic Distance Sensor
uart:
tx_pin: GPIO5
rx_pin: GPIO18
baud_rate: 9600
sensor:
- platform: a02yyuw
name: "A02YYUW Distance"
update_interval: 100ms
timeout: 2s
The ESPHome configuration uses the uart
platform to interface with the A02YYUW sensor. The tx_pin
and rx_pin
specify the GPIO pins used for UART communication. The baud_rate
is set to 9600 to match the sensor's communication protocol. The sensor
component configures the A02YYUW as the distance sensor, with a user-friendly name 'A02YYUW Distance.' The update_interval
specifies that measurements are taken every 100 ms, and the timeout
ensures reliable communication by preventing prolonged wait times for data.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 A02YYUW PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
ESP32 A02YYUW PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the A02YYUW Waterproof Ultrasonic Distance Sensor:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5, 18); // RX, TX
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
Serial.println("A02YYUW Distance Sensor Example");
}
void loop() {
if (mySerial.available() >= 4) {
uint8_t data[4];
for (int i = 0; i < 4; i++) {
data[i] = mySerial.read();
}
if (data[0] == 0xFF) {
int sum = (data[0] + data[1] + data[2]) & 0xFF;
if (sum == data[3]) {
int distance = (data[1] << 8) | data[2];
if (distance > 30) {
Serial.print("Distance: ");
Serial.print(distance / 10.0);
Serial.println(" cm");
} else {
Serial.println("Below the lower limit");
}
} else {
Serial.println("Checksum error");
}
}
}
delay(100);
}
The PlatformIO code communicates with the A02YYUW sensor via UART using the SoftwareSerial library. The RX and TX pins are defined on pins 5 and 18, respectively, and configured to operate at a 9600 baud rate. The code reads 4 bytes of data from the sensor, validates the header and checksum, and calculates the distance in millimeters. The distance is converted to centimeters and printed to the Serial Monitor every 100 ms.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 A02YYUW MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the A02YYUW Waterproof Ultrasonic Distance Sensor with your ESP32.
from machine import UART
from time import sleep
# Configure UART
uart = UART(2, baudrate=9600, tx=5, rx=18)
def read_distance():
if uart.any() >= 4:
data = uart.read(4)
if data[0] == 0xFF:
checksum = (data[0] + data[1] + data[2]) & 0xFF
if checksum == data[3]:
distance = (data[1] << 8) | data[2]
if distance > 30:
return distance / 10.0
else:
return "Below the lower limit"
else:
return "Checksum error"
return None
print("A02YYUW Distance Sensor Example")
while True:
result = read_distance()
if result:
print(f"Distance: {result} cm")
sleep(0.1)
This MicroPython script uses UART to communicate with the A02YYUW sensor. The UART is configured on TX pin GPIO17 and RX pin GPIO16 at a baud rate of 9600. The read_distance()
function reads 4 bytes of data from the UART buffer, validates the header and checksum, and calculates the distance in millimeters. The result is converted to centimeters and returned. The main loop continuously calls this function every 100 ms to print the distance to the console. If the sensor detects an issue, such as a checksum error or a distance below the minimum threshold, appropriate messages are displayed.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of A02YYUW Waterproof Ultrasonic Distance Sensor, its pinout, connection with ESP32 and A02YYUW Waterproof Ultrasonic Distance Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.