Help Center
< All Topics
Print

Interfacing A02YYUW-1AT Ultrasonic Sensor With Arduino Uno

Introduction

The goal of this project is to seamlessly interface the A02YYUW sensor with an Arduino to capture and display sensor data in real-time using the Arduino’s Serial Monitor. By the end of this project, you will have a clear understanding of how to connect the sensor to the Arduino, write the necessary code to read sensor data, and utilize the Serial Monitor to visualize the data. This project is perfect for beginners looking to enhance their skills in sensor integration and data visualization with Arduino.

Materials:

  • Arduino IDE (2.3.2)
  • Arduino (Compatible) UNO R3 with USB cable
  • 4 x MM Jumper Wires
  • A02YYUW Waterproof Ultrasonic Sensor

About The A02YYUW Ultrasonic Sensor

The A02YYUW sensor is an ultrasonic sensor used for measuring distance with high precision. It emits ultrasonic waves and calculates the time it takes for the waves to bounce back from an object, providing accurate distance measurements. This sensor is commonly used in various applications such as obstacle avoidance in robotics and distance measurement in automation systems. 

Note: There are different variants of the A02YYUW sensor, utilising different communication protocols. This variant is the UART Controlled Type. 

Pin Layout

The A02YYUW sensor comes with a JST connector with colour-coded wires. The pin layout for each pin is displayed below:

MB-ELC-SEN-A02YYUW-06

Power Connections:
Wire pin (1) VCC from the JST connector to 5V on the Arduino. Next, connect pin (2) GND from the JST connector to GND on the Arduino. 

Communication:
Connect Pin 3 RX from the JST Connector to pin 3 on the Arduino and connect Pin 4 TX to pin 2 on the Arduino. 

See the wiring diagram below:

A02YYUW_watermark
Click to Zoom

Code

Initialisation:

Define the pins output and inputs, then begin the serial.

#include <SoftwareSerial.h>
#include <Wire.h>

// Connect the sensor’s RX/TX pin to these Arduino pins
#define RXpin 2
#define TXpin 3
SoftwareSerial mySerial(RXpin, TXpin);
unsigned char data[4] = {};
float distance = 0.00;

void setup() {
  pinMode(RXpin, INPUT);
  pinMode(TXpin, OUTPUT);
  Wire.begin();
  Serial.begin(9600);
  mySerial.begin(9600);
}

Main loop:

Reset the RX pin. Then, detect the pulse. The RX pin is then set to high again so that the measurement can be taken. The data is then read in serial format. Lastly, display the distance in centimeters in the Arduino IDE.

void loop() {
  delay(30);

  // Resetting the RX pin
  digitalWrite(TXpin, HIGH);
  delay(30);

  delay(2); // Ensure the pulse is detected
  digitalWrite(TXpin, LOW);
  delay(30);

  // Send positive pulse to the sensor’s RX pin to start measurement
  digitalWrite(TXpin, HIGH);

  // Wait for the measurement to complete
  delay(60); // Adjust delay based on sensor specifications

  // Gathering & calculating the data
  for(int i=0; i<4; i++) { data[i] = mySerial.read(); }
  distance = ((data[1]*256)+data[2] )/ 10;

  // Display the measurement
  Serial.print(“Distance: “);
  Serial.print(distance);
  Serial.println(” cm”);

  // Adjust delay based on measurement frequency
  delay(5000);
}

Console Output:

You should see the distance measured by the sensor displayed into the Arduino IDE Serial Monitor, like the following.

Downloadable Content

Please find this tutorial’s code on our GitHub page.

Credits:

  • Maker Community
  • The Arduino Community
  • The STEM Community
Table of Contents