Vous êtes sur la page 1sur 20

Distance Measurement using Ultrasonic- sensor(HC-SR04)

By : Mohammed Sazzad(15302004)

Abstract
The purpose of project is to design and develop a distance measurement system using ultrasonic
waves(ultrasonic sensor HC-SR04) that interfaced with Arduino and a LCD screen that display
the readings of distance measured by ultrasonic sensor HC-SR04 ,along with a push button to
see the measured readings in different units (i.e.,Cm,inch) .We know that human audible range
is 20hz to 20khz. We can utilize these frequency range waves through ultrasonic sensor HC-
SR04.The advantages of this sensor when interfaced with Arduino which is a control and sensing
system, Arduino is a small microcontroller board with a USB plug to connect to the computer.
The Arduino board senses the environment by receiving input from ultrasonic sensor (HC-
SR04) and can affect its surroundings by controlling LCD display which provides the measured
readings, along with provided codes from the Arduino Basic Library in the Arduino coding
software (Arduino IDE 1.0.6). Have to modify the basic code to get almost accurate distance
measurement, converting the distance in different units. And push buttons to change units
(i.e.,Cm, inch) . In present work, I have measured small distances very precisely and Arduino
Programming functioned successfully for this system. It is used to detecting an obstacle, along
with its exact distance.

1
Table of contents

Abstract 1

Table of Contents 2
List of figures 3
List of Tables 4

Chapter 1 5
1.1 : Introduction 5

Chapter 2 6

2.1 : Project Background 6

2.2 : System Requirements 7

Chapter 3 : Experimantel setup 8

3.1 : Ultrasonic-sensor(HC-SR04) 8

3.2 : 16x2 LCD Display 9

3.3 : Arduino uno based on ATMega-328 Microcontroller 10

3.4 : Potentiometer 11

3.5 : Push Buttons 11

Chapter 4

4.1 : Methodology 12

4.2 : Results 14

2
Chapter 5

5.1 : Future work and Conclusion 16

5.2 : References 16

APPENDIX (c++ programming code for Arduino IDE 1.0.6) 17

3
List of Figures

Fig 2.1: Basic block diagram of distance measurement by ultrasonic-sensor(Hc-SR04)


Fig 3.1 : a) Ultarspnic-sensor distance sensor module working principle,
b) Ultrasonic-sensor (HC-SR04)
Fig3.2 : 16x2 Lcd display
Fig 3.3 : Arduino UNO Based on ATMega-328 Microcontroller
Fig 3.4 : Potentiometer
Fig 3.5 : Push buttons
Fig 4.1 : Circuit diagram of Distance Measurement using Ultrasonic-sensor(HC-SR04)
Fig 4.2 : Schematic diagram Distance Measurement using Ultrasonic-sensor(HC-SR04)
Fig 4.3 : Sample result on display

4
List of Tables

Table 3.1: 16x2 LCD display pin diagrams


Table 4.1: 16x2 Lcd Display interfacing with Arduino pin diagrams

5
Chapter 1

Introduction: Nowadays, we have some difficulties in obtaining the distance, length that we
want to measure. Even though, measuring tape, scale are easy option, but this kind of tool will
have a limitation of manual error, accuracy, precision. Before this, engineers have produced a
range finder module but in the end, they find out the module have many disadvantages like
limitation for distance, different result for different colored obstacles, and need a calibration for
every time before starts using it.

In our daily life we use rulers to measure length but we certainly face problems regarding
measurement errors. Precise and fix measurement of low range distance, is the main objective
for this project. In the project we have followed by the reference[1] and I have developed a
distance measurement device or system, which can measure distance or length using ultrasonic
sensors interfaced with Arduino conveniently and display them on LCD Screen, having the
results in different measuring units on the LCD display by pressing a push button. This distance
measuring device can measure distance in the range of 2cm to 400cm with the accuracy of 3mm .

This device is based upon the reflection of sound waves. Sound waves are defined as
longitudinal pressure waves in the medium in which they are travelling. Subjects whose
dimensions are larger than the wavelength of the impinging sound waves reflect them, the
reflected waves are called the echo. If the speed of sound in the medium is known and the time
taken for the sound waves to travel the distance from the source to the subject and back to the
source is measured, the distance from the source to the subject can be computed accurately.

6
Chapter 2

2.1 Project Background: Bats are wonderful creatures. Blind from the eyes but the vision is
sharper than humans, Ultrasonic ranging is the technique used by bats. Ultrasonic sensor
provides an easy way in distance measurement. The ultrasonic sensor (HC-SR04) is perfect for
distance measurements between moving or stationary objects. Ultrasonic Sensor measure the
distance of the objects in air through non-contact technique. They measure distance without
damage and are easy to use and reliable. These distance measurement sensors connect with all
common types of automation and telemetry equipment. Machinery and processes in a wide range
of industries use distance measurement sensors where size or position feedback is
required.Distance measurement sensors are used to control or indicate the position of objects and
materials. Distance measurement sensors can determine the dimensions of objects such as height,
width and diameter, using one or more sensors. The echo time response of ultrasonic sensor
detector is based on time of travel after trigger pulse to the surrounding objects is non-linear and
depends on the reflectance characteristics of the object surface. Ultra Sonic sensors are widely
used for distance measurement purposes. They offer low cost and a precision of less than 1 cm in
distance measurements of up to 6m [1, 4]. However, the most popular method used in these
measurements is based on the time of flight (ToF) measurement. This ToF is the time elapsed
between the emission and subsequent arrival after reflection of an Ultrasonic pulse train
travelling at the speed of sound. In this project, I have tried to devlop such a measurement
system which uses ultrasonic sensor. A sensor is a electrical transducer that converts one type
of energy to another (physical quantity into electrical signal). Ultrasound sensors are very
versatile in distance measurement. They are also providing the cheapest solutions. Ultrasound
waves are useful for both the air and underwater[3]. Ultrasonic sensors are also quite fast for
most of the common applications. Speed of sound in air is 340 m/s. An ultra-sound is a sound
wave which typically travels at speed of sound. A sonic sensor when triggered generates an
ultra-sound wave which will hit a solid barrier and reflect back to the ultra-sonic sensor. The
time is recorded starting from echoing to reception of the waves. The time taken is for twice
the distance travelled.
Using the formula

𝒅𝒊𝒔𝒕𝒂𝒏𝒄𝒆 = 𝒔𝒑𝒆𝒆𝒅 × 𝒕𝒊𝒎𝒆


And
𝒅𝒊𝒔𝒕𝒂𝒏𝒄𝒆
𝑫𝒊𝒔𝒕𝒂𝒏𝒄𝒆𝒃𝒂𝒓𝒓𝒊𝒆𝒓 =
𝟐

We get the uni-directional distance from sensor to object by using this fomula, a digital ruler
device can be designed with the power of micro controller and sensing capability of ultra-sonic
sensor. The circuit designed can be used to measure distances up to 4 meter with an accuracy of
3mm.

7
The Arduino Uno is a microcontroller board based on the ATmega328 (datasheet). It has 14
digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz
crystal oscillator, a USB connection, a power jack, an
ICSP header, and a reset button. It contains everything needed to support the microcontroller;
simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or
battery to get started[3]. The ultrasonic sensor emits an ultrasound at 40 000 Hz which travels
through the air and if there is an object or obstacle on its path It will bounce back to the module.
Considering the travel time and the speed of the sound the sensor measure the distance around
it[4]. The measured distance is calculated using Arduino controller within a predefined time
interval. The analog output read from the sensor module is transferred to LCD display through
serial port via Arduino and by pressing push button to have different measuring units. This
causes large response times for a single measurement.

2.2 SYSTEM REQUIREMENTS : The system in this project proposes consist of


Arduino Uno Atmega 328 microcontroller, Ultrasonic sensor(HC-SR04), 16x2,LCD Display,
Push buttons, potentiometers,9v battery. The block diagram of proposed system is as follows.

Block diagram :

16x2 Crystal LCD


Ultrasonic Potentiometer display
sensor(HC –SR04)
u

Personal
Arduino Uno computer or
Push buttons
9v battery
Atmega 328
Micro controller

Fig 2.1: Basic block diagram of distance measurement by ultrasonic-sensor(Hc-SR04)

8
Chapter 3

Experimental setup:
3.1 ULTRASONIC S ENSOR-SR04 :

a)

b)
Fig 3.1 a) Ultarspnic-sensor distance sensor module working principle
b) Ultrasonic-sensor (HC-SR04)
It emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or
obstacle on its path It will bounce back to the module. Considering the travel time and the speed
Of the sound you can calculate the distance.An Ultrasonic Sensor have 4 pins, as we see in fig
3.1 the pins are VCC, Trig, Echo, GND. Ultrasonic Range Detection Sensor ,works by sending
an ultrasound pulse at around 40 KHz, It then waits and listens for the pulse to echo back,
calculating the time taken in microseconds. We can trigger a pulse as fast as 20 times a second
9
and it can determine objects up to 3 metres away and as near as 3cm. The snapshot of the sensor
and working process of the sensor is shown in fig 3.1The sensor needs a 5V power supply to run.

3.2 16x2 LCD DISLAY:

Fig3.2 : 16x2 Lcd display


LCD (Liquid Crystal Display) screen is an electronic display module and find a wide range of
applications. A 16x2 LCD display is very basic module and is very commonly used in
various devices and circuits. These modules are preferredover seven segments and other multi-
segment LEDs. The reasons being: LCDs are economical; easily programmable; have no
limitation of displaying special & even custom characters (unlike in seven segments), animations
and so on. A 16x2 LCD means it can display 16 characters per line and there are 2 such lines. In
this LCD each character is displayed in 5x7 pixel matrix. This LCD has two registers, namely,
Command and Data.

Table 3.1: 16x2 LCD display pin diagrams

10
3.3 Arduino UNO Based on ATMega-328 Microcontroller:

Fig 3.3 : Arduino UNO Based on ATMega-328 Microcontroller


The Arduino Uno is an open-source microcontroller board based on the ATmega328. It has 14
digital input/output pins (of which 6 can be used as PWM outputs) and 6 analog input. It contains
everything needed to support the microcontroller, and it can be simply connected to a computer
with a Universal Serial Bus (USB) cable to get started. The Arduino Uno can be programmed
with the Arduino Integrated Development Environment (IDE). The C-based simple program
code for the Arduino is referred to as a sketch. Collection of sketches for specific functionalities
is referred to as libraries.The Arduino can be programmed upto 32 KB memory. Arduino can
function autonomously without being connected to a computer, or alternatively programmed to
respond mainly to commands sent from the computer via various software interfaces or
to the data acquired from the input channels. The Arduino UNO based on ATMega-328
Microcontroller In the figure there is a Arduino UNO Based on ATMega-328 Microcontroller
The ultrasonic sensor is attached with the breadboard. The sensor measure the distance around
the sensor. The measured distance is calculated using Arduino controller within a predefined
time interval. The analog output read from the sensor module is transferred to personal computer
through serial port via Arduino.

11
3.4 Potentiometer :

Fig 3.4 : Potentiometer

Potentiometer is a device used to measure the internal resistance of a cell, to compare the e.m.f.
of two cells and potential difference across a resistor. It consists of a long wire of uniform
cross sectional area and of 10 m in length. The material of wire should have a high resistivity and
low temperature coefficient. The wires are stretched parallel to each other on a wooden
board. The wires are joined in series by using thick copper strips. A metre scale is also attached
on the wooden board. It works on the principle that when a constant current flows through a wire
of uniform cross sectional area, potential difference between its two points is directly
proportional to the length of the wire between the two points

3.5 Tactile PushButton:

Fig 3.5 : Push buttons


Push button is a mechanical device which is usually in always open mode. When the mechanical
button is pressed two of its bridge are closed by mechanically connecting the two bridges by an
arm which then allows current to flow through the bridges.

12
Chapter 4

4.1 Methodology :
1. The ultrasonic sensor transmits ultrasound waves. Ultrasonic Sensor HC SR-04 has 4 pins.
VCC, GND, trig and echo. The VCC and GND are connected to the +5v and GND pins of
Arduino. The trig and echo pins to digital pins on Arduino.
2. 16x2 Lcd Display interfacing with Arduino pin diagrams

LCD Pin # LCD PIN NAME Arduino Pin

1 VSS GND

2 VDD 5V

3 V0 Pot Center Pin

4 RS 10

5 RW GND

6 E 9

7 DB0 NOT CONNECTED

8 DB1 NOT CONNECTED

9 DB2 NOT CONNECTED

10 DB3 NOT CONNECTED

11 DB4 Pin 5

12 DB5 Pin 4

13 DB6 Pin 3

14 DB7 Pin 2

15 Backlight LED +V 5V

16 Backlight LED GND GND

Table 4.1: 16x2 Lcd Display interfacing with Arduino pin diagrams

13
Fig 4.1: Circuit diagram of Distance Measurement using Ultrasonic-sensor(HC-SR04)

14
Fig 4.2 : Schematic diagram Distance Measurement using Ultrasonic-sensor(HC-SR04)

3. The LCD display will need resistors for limiting current and potentiometers for
controlling the brightness and contrast of the LCD display
4. The signal from the sensor are fed in to Arduino pins
5. Arduino sends trigger signal of specific pulse width to the ultrasonic sensor.
6. Next it gets input from the ultrasonic sensor by the echo pin
7. The time between the sending and reception of signal is then multiplied with
constant speed of sound to give the distance
8. The Distance is halved to get the distance from sensor to the object.
9. The distance is coverted to inch and centimeter from miles using conversion
calculation and modifying the basic code in Arduino IDE 1.0.6
10. The measured value is then displayed on the LCD screen.
11. A push button is attached with a digital pin of Arduino which when pressed toggles
the mode between centimeter and inches reading on the LCD screen.

4.2 Results: In this Distance Measurement using Ultrasonic-sensor(HC-SR04) when


Ultrasonic-sensor(HC-SR04) faces an obstacle, within 3mm to 2 meter distance it will send the
input to arduino as arduino sense the distance of the obstacle through ultrasonic sensor and The
distance is coverted to inch and centimeter from miles using conversion calculation and

15
modifying the basic code in Arduino IDE 1.0.6, The measured value is then displayed on the LCD
screen.

Fig 4.3: Sample result on display

16
Chapter 5

5.1 Future Work and Conclusion :


I. New prototyping hardware & capatibility & interfacing with other consumer
elatrone/tv/smartphones & flooding of shields

II. Mining equepments may require where entail.

III. Acready compatible with many major simulation software like MATLAB & lab view,
we may see even move flexible programming environment & development option.

IV. Using temp. Compensation, it can be used over wide temp


range.

V. Height meausrment, agriculture velide, collision /protection


can be other application.

The importance of the project is calculating accurate distance from any obstacle that we want to
measure. The device can be used in many different fields and categories like distance calculation
in construction field, robots, car sensor to avoid obstacles and many other applications.

5.2 Reference:
1. https://circuitdigest.com/microcontroller-projects/arduino-ultrasonic-sensor-
based-distance-measurement (25-02-2018)

2. https://circuiteasy.com/distance-measurement-using-ultrasound/ (25-02-2018)

3. Distance Measurement using Ultrasonic Sensor and Microcontroller volume 07


CH. Neeraja Soni1, CH.Sarita2, Shrikant Maheshwari3, Basant kumar Sahu4,
Bhavishya Jain5, Garima Shrivastava Assistant Profeesor
YITM, Rajnandgaon, India (25-02-2018)

4. Distance Measurement using Ultrasonic Sensor by Prakhar Shrivastava,


Praveen Kumar, Ankit Tiwari National Conference on Synergetic Trends in
engineering and Technology (STET-2014) (27-02-2018)

17
APPENDIX : c++ programming code for Arduino IDE 1.0.6

#include <LiquidCrystal.h> //Load Liquid Crystal Library

LiquidCrystal LCD(10, 9, 5, 4, 3, 2); //Create Liquid Crystal Object called LCD

int trigPin=13; //Sensor Trip pin connected to Arduino pin 13

int echoPin=11; //Sensor Echo pin connected to Arduino pin 11

int buttonPin = 12; //Button pin connected to Arduino pin 12

float pingTime; //time for ping to travel from sensor to target and return

float targetDistance; //Distance to Target in inches

float targetDistance_cm; //Distance in cm

float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

int toggleMode = 0;

int state;

void setup() {

Serial.begin(9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(buttonPin, INPUT_PULLUP);

LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD

LCD.setCursor(0,0); //Set LCD cursor to upper left corner, column 0, row 0

LCD.print("Target Distance:"); //Print Message on First Row

void loop() {

18
digitalWrite(trigPin, LOW); //Set trigger pin low

delayMicroseconds(2000); //Let signal settle

digitalWrite(trigPin, HIGH); //Set trigPin high

delayMicroseconds(15); //Delay in high state

digitalWrite(trigPin, LOW); //ping has now been sent

delayMicroseconds(10); //Delay in high state

pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds

pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in


a second)

pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)

targetDistance= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per
hour

targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must
divide by 2 for actual target distance.

targetDistance_cm = targetDistance * 160934;

targetDistance= targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per


mile)

//state = digitalRead(buttonPin);

//Serial.println(state);

printDistance();

delay(20);

state = digitalRead(buttonPin);

Serial.println(state);

if(state == 0){

if(toggleMode == 0){

toggleMode = 1;

19
}else if(toggleMode == 1){

toggleMode = 0;

void printDistance(){

LCD.setCursor(0,1); //Set cursor to first column of second row

LCD.print(" "); //Print blanks to clear the row

LCD.setCursor(0,1); //Set Cursor again to first column of second row

if(toggleMode == 0){

LCD.print(targetDistance); //Print measured distance

LCD.print(" inch"); //Print your units.

}else if(toggleMode == 1){

LCD.print(targetDistance_cm); //Print measured distance

LCD.print(" cm"); //Print your units.

delay(100);

20