Ultrasonic Range Detector Using Arduino and SR-04F

Want to Make a Tool that Measures Distance without using ruler,Then this Mini Project will be the best solution for it.Here we are Gonna make a Arduino Distance Measuring Device with a Ultrasonic Module similarly to a Bat.So lets Get Started!!

Things you need:

Ultrasonic / Sonar Module (HC-SR04F) Working

HC-SR04F Working
HC-SR04F Working

This Module consists of Two drums,one of which is a emitter that emits Ultrasound and other is receiver  which receives the Reflected Ultrasound from the object.The emitter drum emits Ultrasound when we trigger the Module using the trigPin by sending a 10 Microseconds high Pulse.

As soon as the Ultrasound is emitted through the emitter the Module makes the echoPin high.

Emitted Ultrasound travels forward till it gets reflected by object and then travels Backward.The reflected ultrasound is detected by the receiver.

When the reflected Ultrasound  is received by the receiver,echoPin is made Low.

Now we have the Time take by the Ultrasound to reach the object and again reach the source which is also equal to the duration for which the echoPin was High.This time is Stored in the Microcontroller.Therefore Travel time of Ultrasound between just source to object is half the time take to travel source-object-sourse

we know that,

Distance=Speed x Total Time

But time take to travel distance is half of total time

Arduino Projects
Arduino Projects

Distance=(Time/2) x Speed

Distance=(Time/2) x 29.1       where 29.1 is the speed of ultrasound in Centimeter/Microseconds

Arduino Ultrasonic distance measure Code

This is a very simple code,First we declare pin 13 as trigPin and pin 12 as echoPin and then we set trigPin as Output so that we can send signal to Module from Arduino and echoPin as Input to receive signal from Module.Further we declare two variables ‘duration’ and ‘distance’ to store time taken to receive.

To get the distance of an object from the module we first send a 10 Microsecond High pulse to the Module through the trigPin this triggers the module to get the distance.

Arduino Ultrasonic
Arduino Ultrasonic

Now the Module sends a High signal through the echoPin for specific Time Duration.We store this time duration in a Variable.This time is later used to calculate the distance through a formulae(Explained in working of Module)


#define trigPin 13
#define echoPin 12

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

void loop() 
  long duration, distance;
  digitalWrite(trigPin, LOW);        
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0)
    Serial.println("Out of range");
    Serial.println(" cm");


Wiring up the Circuit

You need to connect the Ultrasonic Module to Arduino for that you need 4 Jumper wires and connect them as follows

Module  Arduino





Ultrasonic SR04F
Ultrasonic SR04F

Uploading and Getting the Distance

After connecting the module to Arduino we can now Upload the code to the Arduino.

In Arduino IDE go to Tools>>Serial Monitor and Press any Key,You will get the distance between the Module and Object.Vary the distance and check for values

Arduino Serial Monitor
Arduino Serial Monitor


Arduino Serial Monitor
Arduino Serial Monitor


Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons