Circuit Creation

Ultrasonic Distance Sensor Project using Arduino

The ultrasonic distance sensor project teaches you how to measure distances using ultrasonic sound waves. This is an essential skill for robotics, obstacle avoidance, and proximity-based automation systems.

Intermediate

Components Required

Circuit Connections

Follow the steps below to connect the ultrasonic sensor with the Arduino board. The HC-SR04 has 4 pins: VCC, GND, TRIG, and ECHO.

  1. Connect the VCC pin of the sensor to the 5V pin on Arduino.
  2. Connect the GND pin of the sensor to the GND pin on Arduino.
  3. Connect the TRIG pin to digital pin 9 on Arduino.
  4. Connect the ECHO pin to digital pin 10 on Arduino.
  5. Double-check all connections before powering up the Arduino.
Arduino Ultrasonic Sensor circuit connection

Here is the circuit diagram for reference. The TRIG pin sends the ultrasonic pulse, and the ECHO pin receives the reflected signal.

Arduino Code

                
                // Ultrasonic Distance Sensor Project
                // Circuit Creation

                const int trigPin = 9;
                const int echoPin = 10;

                void setup() {
                  Serial.begin(9600);
                  pinMode(trigPin, OUTPUT);
                  pinMode(echoPin, INPUT);
                }

                void loop() {
                  // Clear the trigPin
                  digitalWrite(trigPin, LOW);
                  delayMicroseconds(2);

                  // Set the trigPin on HIGH state for 10 microseconds
                  digitalWrite(trigPin, HIGH);
                  delayMicroseconds(10);
                  digitalWrite(trigPin, LOW);

                  // Read the echoPin, pulseIn() returns the duration (time for sound to travel)
                  long duration = pulseIn(echoPin, HIGH);

                  // Calculating the distance
                  long distance = duration * 0.034 / 2;

                  // Prints the distance in inches or centimeters
                  Serial.print("Distance: ");
                  Serial.print(distance);
                  Serial.println(" cm");

                  delay(500);
                }
                
            

Upload the Code

  1. Connect the Arduino board to your computer using the USB cable.
  2. Open the Arduino IDE and paste the code into a new sketch.
  3. Select the correct board and port from the Tools menu.
  4. Click the Upload button.
  5. Open the Serial Monitor (Ctrl+Shift+M) to view distance readings.
  6. Move objects closer and farther from the sensor to see the distance change.

Code Explanation

Let us understand how the Arduino code works step by step. This will help you modify the project and build more advanced applications.

Defining Pins

const int trigPin = 9; and const int echoPin = 10; define which pins the sensor is connected to.

setup() Function

The setup() function initializes Serial communication at 9600 baud, sets trigPin as OUTPUT, and echoPin as INPUT.

Triggering the Sensor

First, we set trigPin to LOW for 2 microseconds to ensure a clean pulse. Then, we send a 10-microsecond HIGH pulse to trigger the sensor's measurement.

Reading the Echo

pulseIn(echoPin, HIGH) measures how long the ECHO pin stays HIGH. This duration is proportional to the distance of the object.

Calculating Distance

The formula distance = duration * 0.034 / 2 converts the time to distance in centimeters. The factor 0.034 is the speed of sound (343 m/s). We divide by 2 because the sound travels to the object and back.

Serial Output

The distance values are printed to the Serial Monitor every 500 milliseconds using the delay(500) function.

Common Mistakes & Troubleshooting

What You Learned

Project Ideas