Distance Measurement Using Ultrasonic Sensor and Arduino




Introduction: Distance Measurement Using Ultrasonic Sensor and Arduino

About: student of Electronic Engineer

HI every one

I got my first Aduino recently , here I am gonne show you my first project which is Distance measurement using Ultrasonic sensor and Arduino , you can use this project many application like

1-Liquid Level

2-Object Detection

3-Distance Measurement

i think its Awesome and easy project

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Placing Electronics

You need to build this project
1- Arduino uno R3(maga )

2-Ultrasonic sensor ( model HC-SR04)

3-jumber wires (4pc )


This step will show you where and how to place the electronics on the breadboard and then connect Arduino

First, place the distance sensor on one end of the breadboard so that none of the pins are connected as shown

Step 2: Connecting Arduino to Sensor

The circuit: *

VCC connection of the sensor attached to +5V

GND connection of the sensor attached to ground

TRIG connection of the sensor attached to digital pin 2

ECHO connection of the sensor attached to digital pin 4

Step 3: The Code

This step is the code that you need to load onto your arduinoMaterials:Arduino +Breadboard + ElectronicsComputerUSB cable to connect arduino & computerArduino Environment

Copy and paste this code into the processing environment.This is the code

const int trigPin = 2;

const int echoPin = 4;

void setup() {


void loop()


long duration, inches, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);


digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

inches = microsecondsToInches(duration);

cm = microsecondsToCentimeters(duration);


Serial.print("in, ");






long microsecondsToInches(long microseconds)

{return microseconds / 74 / 2;


long microsecondsToCentimeters(long microseconds)

{return microseconds / 29 / 2;}

Original code for Ping example was created by David A. Mellis Adapted for HC-SR04 by Tautvidas Sipavicius This example code is in the public domain.

Step 4: Result Using Serial Monitor

After compiling, uploading and running it, in the Serial Monitor (Tools -> Serial Monitor or Ctrl + Shift + M) the sensor was sending correct data!

serial monitor will shown u

I hope you enjoyed this project and if there is something wrong or not clear please let me know.
Thanks for reading.


Zakariye Abdirahman

Student :Electronic Engineering

Be the First to Share


    • Trash to Treasure Contest

      Trash to Treasure Contest
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    11 Discussions


    Question 1 year ago on Step 3

    Its giving only 0cm and not anything can anyone give me a soln

    Dhyey Patel 7120
    Dhyey Patel 7120

    Answer 10 months ago

    Try to connect the sensor properly on the breadboard.... It may be loose.


    Reply 4 months ago

    I have the same problem..

    Vijaya vijji
    Vijaya vijji

    Question 1 year ago on Step 3

    The code is having some errors how can i modify it could you help me

    Dhyey Patel 7120
    Dhyey Patel 7120

    Answer 10 months ago

    No the code is proper.
    There is an error write pulseIn (capital I) and not pulseln(small L)


    1 year ago on Step 4

    how long can it measure can anyone help me out with that?