Arduino Nano Ultrasonic Sensor Project

Learn how to measure distance using an ultrasonic sensor (HC-SR04) with Arduino Nano. This complete guide explains wiring, working principle, code, and real-world applications.

Arduino Nano Ultrasonic Sensor Project

The Arduino Nano Ultrasonic Sensor project is a popular and practical electronics project used to measure distance without physical contact. It uses an ultrasonic sensor module such as HC-SR04, which works by emitting sound waves and measuring the time it takes for the echo to return after hitting an object.

This type of sensing is widely used in robotics, automation, and smart systems. From obstacle-avoiding robots to parking sensors and water level monitoring, ultrasonic sensors provide a reliable and cost-effective solution for distance measurement.

In this project, you will learn how to interface an ultrasonic sensor with the Arduino Nano, calculate distance using time-of-flight principles, and display the results on the Serial Monitor.

Understanding Ultrasonic Sensor

An ultrasonic sensor measures distance by using high-frequency sound waves, typically above 20 kHz, which are not audible to humans. The sensor sends out a pulse of sound waves and listens for the echo reflected back from an object.

The time taken for the echo to return is used to calculate the distance. Since the speed of sound in air is approximately 343 meters per second, the distance can be calculated using a simple formula.

Distance = (Time × Speed of Sound) / 2. The division by 2 accounts for the round trip of the sound wave.

Components Needed

  • Arduino Nano
  • Ultrasonic Sensor (HC-SR04)
  • Breadboard
  • Jumper Wires
  • USB Cable for programming and power

Block Diagram

Illustration

Circuit Setup

Connecting Ultrasonic Sensor to Arduino Nano:

Connect the VCC pin of the ultrasonic sensor to the 5V pin of the Arduino Nano and the GND pin to the GND of the Arduino.

Connect the TRIG (trigger) pin of the ultrasonic sensor to digital pin 9 of the Arduino Nano. This pin is used to send the ultrasonic pulse.

Connect the ECHO pin of the ultrasonic sensor to digital pin 10 of the Arduino Nano. This pin receives the reflected signal.

Ensure all connections are secure and properly placed on the breadboard.

Working Principle

The Arduino sends a short pulse to the TRIG pin, which causes the ultrasonic sensor to emit a burst of ultrasonic waves. These waves travel through the air and reflect off any object in their path.

When the reflected waves return to the sensor, the ECHO pin goes HIGH for a duration equal to the time taken by the sound waves to travel to the object and back.

The Arduino measures this duration using the pulseIn() function and calculates the distance using the speed of sound.

Arduino Code

#define TRIG_PIN 9
#define ECHO_PIN 10

long duration;
float distance;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH);

  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);
}

Software (Arduino IDE)

Open the Arduino IDE and create a new sketch. Copy the above code and paste it into the editor. Connect your Arduino Nano to the computer using a USB cable.

Select the correct board (Arduino Nano) and port from the Tools menu, then click the upload button to transfer the code to the board.

Project Operation

After uploading the code, open the Serial Monitor from the Arduino IDE. The sensor will continuously measure the distance and display the values in centimeters.

Place objects at different distances in front of the sensor to observe how the readings change in real time.

Common Issues and Troubleshooting

  • Incorrect wiring of TRIG and ECHO pins.
  • Loose connections on the breadboard.
  • Sensor not receiving enough power.
  • Incorrect board or port selection in Arduino IDE.
  • Objects too far or too close for accurate detection.

Applications

Ultrasonic sensors are widely used in obstacle detection systems for robots and autonomous vehicles. They help avoid collisions by detecting nearby objects.

They are also used in smart parking systems to measure the distance between vehicles and obstacles, assisting drivers in parking safely.

In industrial applications, ultrasonic sensors are used for liquid level measurement in tanks and containers without direct contact.

Other applications include security systems, automatic doors, and distance-based automation systems.

Advanced Ideas

You can enhance this project by adding an LCD display to show the distance directly without using the Serial Monitor.

Another improvement is integrating a buzzer that alerts when an object comes within a certain distance threshold.

You can also connect the Arduino Nano to IoT platforms to monitor distance data remotely.

Conclusion

The Arduino Nano Ultrasonic Sensor project is an excellent introduction to distance measurement and sensor interfacing. It teaches important concepts such as signal timing, real-time data processing, and hardware integration.

With this knowledge, you can build advanced systems like smart robots, automation devices, and IoT-based monitoring solutions.