Ultrasonic Range Detector Using Arduino and SR-04F

Ultrasonic Range Detector Using Arduino and SR-04F © CC BY

Make a Ultrasonic Range Detector Using Arduino and SR-04F to measure any distance without using rulers with this simple tutorial.

  • 20,516 views
  • 10 comments
  • 38 respects

Components and supplies

About this project

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 similar to a bat. So lets get started!

For more tutorials visit this link.

How Does a Ultrasonic Module Work?

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-source we know that:

Distance=Speed x Total Time

But time take to travel distance is half of total time:

Distance=(Time/2) x Speed
Distance=(Time/2) x 29.1       

Where 29.1 is the speed of ultrasound in centimeter/microseconds.

Code

Code For ArduinoC/C++
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.
Uploading and Getting the Distance

Uploading Code-
After connecting the module to Arduino we can now Upload the code to the Arduino.
After uploading code,in Arduino IDE go to Tools>>Serial Monitor and Press any Key,You will get the distance between the Module and Object in the SERIAL MONITOR.Vary the distance and check for values
#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);        
  delayMicroseconds(2);              
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);           
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0)
  {
    Serial.println("Out of range");
  }
  else 
  {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

Schematics

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

VCC>>5v

GND>>Ground

trigpin>>13

echopin>>12
Sr04

Comments

Similar projects you might like

Ultrasonic Range Detector With Arduino

Project tutorial by Salman Faris

  • 27,202 views
  • 7 comments
  • 25 respects

Ultrasonic Range Finder

Project tutorial by Saaket Poray

  • 9,417 views
  • 8 comments
  • 10 respects

Android Motion Detector Camera with Arduino/MCU

Project tutorial by Walid Mafuj

  • 5,986 views
  • 7 comments
  • 22 respects

Basic Ultrasonic Tutorial

by Damien Murtagh

  • 6,837 views
  • 4 comments
  • 33 respects

Arduino EMF Detector A.K.A Ghost Detector

Project showcase by Patel Darshil

  • 7,069 views
  • 9 comments
  • 18 respects
Add projectSign up / Login