Distance Measurement using Ultrasonic Sensor and Arduino

Distance Measurement using Ultrasonic Sensor and Arduino © GPL3+

I got my first Arduino recently, here I am gonna show you my first project which is Distance Measurement Using Ultrasonic sensors.

  • 11,712 views
  • 4 comments
  • 27 respects

Components and supplies

Apps and online services

About this project

Hi everyone,

I got my first Arduino recently, here I am gonna show you my first project which is Distance Measurement using Ultrasonic sensors and Arduino, you can use this project in many applications like:

  • Liquid Level
  • Object Detection
  • Distance Measurement

I think it's an awesome and easy project!

Step 1: Placing electronics

Things you need to build this project:

  • Arduino Uno R3 (maga)
  • Ultrasonic sensor (model HC-SR04)
  • Jumper wires (4pc)
  • Breadboard

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 all of the pins are connected as shown.

Step 2: Connecting Arduino to sensor

The circuit connections

  • 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 & computer (Arduino Environment).

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

const int trigPin = 2;
const int echoPin = 4;
void setup() { 
Serial.begin(9600);} 
void loop()
{
long duration, inches, cm;
pinMode(trigPin, OUTPUT); 
digitalWrite(trigPin, LOW); 
delayMicroseconds(2); 
digitalWrite(trigPin, HIGH); 
delayMicroseconds(10); 
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT); 
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration); 
cm = microsecondsToCentimeters(duration);
Serial.print(inches); 
Serial.print("in, "); 
Serial.print(cm);
Serial.print("cm"); 
Serial.println(); 
delay(100);
}
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 show you.

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

By: Zakariye Abdirahman

Student: Electronic Engineering

Comments

Similar projects you might like

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 4,661 views
  • 7 comments
  • 19 respects

IOT - Smart Jar Using ESP8266, Arduino and Ultrasonic Sensor

Project tutorial by Team Smazee

  • 6,218 views
  • 4 comments
  • 12 respects

LCD display distance of an ultrasonic sensor

Project tutorial by adam johns

  • 6,800 views
  • 0 comments
  • 8 respects

Door Alarm With Ultrasonic Sensor

Project showcase by MattG

  • 4,904 views
  • 1 comment
  • 9 respects

Ultrasonic Sensor with LCD Display Using 433MHz

Project tutorial by mcharrison500

  • 2,827 views
  • 0 comments
  • 15 respects
Add projectSign up / Login