Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
RZtronics
Published © CC BY

Ultrasonic Range Detector Using Arduino and SR-04F

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

BeginnerProtip1 hour58,272
Ultrasonic Range Detector Using Arduino and SR-04F

Things used in this project

Story

Read more

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

Code

Code For Arduino

C/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);
}

Credits

RZtronics

RZtronics

1 project • 18 followers
I'm an Electronics Enthusiast,Loves Robotics Arduino and Raspberry Pi!! I make Different Projects every Month and make Tutorials
Thanks to RZtronics.

Comments