Measuring Distance Using Ultrasonic Sensor

INTRODUCTION

In this project I’m using an ultrasonic sensor that can precisely measure the distance to show how a beep sound come out when a car is reversed.  You will noticed that when the car is too near to the barrier behind the car, it will give a continuous beep sound. Let’s figure out how it works!  

HARDWARE PREPARATION

This tutorial use :

  1. Maker UNO
  2. Low Cost Ultrasonic Ranging Module
  3. Male to Male Jumper Wire
  4. Micro B USB

You can make the hardware connection based on the diagram and table below.

CONNECTION
Ultrasonic Sensor : VCCMaker UNO: 5V
Ultrasonic Sensor : GNDMaker UNO: GND
Ultrasonic Sensor : TrigMaker UNO: pin 11
Ultrasonic Sensor : EchoMaker UNO : pin 12
Figure 1 : Fritzing connection for ultrasonic with Maker UNO.
Figure 2 : Ultrasonic Sensor

SAMPLE CODE

This is the sample code used for this tutorial. Try it!

/*
This example code is for Measuring Distance Using Ultrasonic Sensor.
Product page:
Maker UNO : https://www.cytron.io/p-maker-uno
Ultrasonic Sensor : https://www.cytron.io/p-sn-hc-sr04
Created by:
30/08/18 Suad Anwar, Cytron Technologies
*/
// defines pins numbers
const int buzzer = 8;
const int trigPin = 11;
const int echoPin = 12;
// defines variables
long duration;
int distance;
void setup() {
pinMode(buzzer, OUTPUT); // Sets the buzzer as an Output
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
digitalWrite(trigPin, LOW); // Clears the trigPin
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance = duration * 0.034 / 2; // Calculating the distance
Serial.print("Distance: "); // Prints the distance on the Serial Monitor
Serial.println(distance);
if(distance >7){
noTone;
Serial.print("Distance: "); // Prints the distance on the Serial Monitor
Serial.println(distance);
}
if(distance <=7 && distance >5){ // If the distance less and equal to 7 and the distance greater than 5
tone(buzzer,1000,200);
delay(700);
Serial.print("Distance: ");
Serial.println(distance);
}
if(distance <=5 && distance >3){ // If the distance less and equal to 5 and the distance greater than 3
tone(buzzer,1000,100);
delay(400);
Serial.print("Distance: ");
Serial.println(distance);
}
if(distance <=3){ // If the distance less and equal to 3
tone(buzzer,1000,100);
Serial.print("Distance: ");
Serial.println(distance);
}
}
view raw ultrasonic.ino hosted with ❤ by GitHub

Reference

  1. How to Mechatronics

Thank you

Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiry, please post at Cytron Technical Forum.

2 thoughts on “Measuring Distance Using Ultrasonic Sensor”

  1. Muhammad Najmuddin

    Hye, i want to want to make motion detection sensor using micro servo, hc sr04 and maker uno. How i want to make sound whenever there is a object detected?

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Why Raspberry Pi is the future of computing devices?
Face Recognition Door Lock System Using OpenCV on Raspberry Pi
Send Data to Firebase Using Arduino Uno WiFi Rev2
Simple Alarm System Using Maker Nano
Program Cucumber ESP32-S2 Using CircuitPython
Scroll to Top