You are on page 1of 26

ULTRASONIC RANGEFINDER USING AT89S52

For the degree of


Bachelor of Technology in Electronics and Telecommunication Engineering
By
Anand Kadu
(ID 10004005)
THIRD YEAR B.TECH (ENTC)
Mr. Nikhil R. Munot
(ID 10004012)
THIRD YEAR B.TECH (ENTC)

Under the Guidance of
Mr. R. W. Somkuwar



DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGINEERING
Government College of Engineering, Amravati
Maharashtra State, India
2013






CERTIFICATE
This is to certify that the project entitled ULTRASONIC RANGEFINDER
USING AT89S52is being submitted herewith as the minor project in 6
th
semester . This is
the result of the work completed by Mr.Anand Kadu and Mr. Nikhil Munot under my
supervision and guidance. The work embodied in thisproject has not formed earlier for the
basis of the award of any degree or compatible certificate or similar title of this for any other
diploma/examining body or university to the best of knowledge and belief.


Place: Amravati Mr. R W Somkuwar
Date: Project Guide


DECLARATION
We hereby declare that the project entitled, ULTRASONIC RANGEFINDER
USING AT89S52 completed and written by us has not been previously formed the basis for the
award of any degree or diploma or certificate for any other diploma/examining body or university.



Place: Amravati Mr. Anand Kadu(10004005)
Date: Mr. Nikhil Munot(10004012)


















ACKNOWLEDGEMENT

It gives us immense pleasure on bringing out the project entitled ULTRASONIC
RANGEFINDER USING AT89S52. We express our sincere gratitude to our guide Mr. R.
W. Somkuwar. We thank him for extending the necessary help, providing facilities and time
to time guidance and motivation. We are grateful to our Head of the Dept. Prof. D.S.
Chaudhari for giving constant encouragement and support.
Last but not the least; we would like to thank all the staff members of Electronics and
Telecommunication Department and the people who directly or indirectly helped us in
completing this task successfully.

Mr. Anand Kadu(10004005)
Mr. Nikhil Munot(10004012)







INDEX

List of Figures i
List of Tables ii
1. Introduction 1
1.1 Overview 1
1.2 Background 1
1.3 Objectives 2
2. Literature Review 3
2.1 Ultrasound 3
2.2 Ultrasonic Sensors 3
2.2.1 Transmitter 3
2.2.2 Receiver 3
3. System Development 4
3.1 Power Supply 4
3.1.1 Bridge Rectifier 4
3.1.2 Filter Circuit 4
3.1.3 Voltage regulator 5
3.2 HC SR04 Ultrasonic Rangefinder Module 5
3.3 Microcontroller AT89S52 6
3.4 LCD Display 16X2 8
4. Performance Analysis 9
4.1 Block Diagram 9
4.2 Circuit Diagram 10
4.3 Working 11
4.3.1 Algorithm 11
4.3.2 Hardware Implementation 11
5. Conclusion 13
5.1 Conclusion 13
5.2 Future Scope 13
5.3 Applications 13
References 14
Appendix 15

LIST OF FIGURES
Figure No. Title Page No.
2.1 Working of Ultrasonic Sensor 3
3.1 Power Supply 4
3.2 HC SR04 Timing Diagram 5
3.3 Pin diagram of AT89S52 7
4.1 Block diagram 9
4.2 Interfacing of HC SR 04 and LCD with AT89S52 10



























LIST OF TABLES

Table No. Title Page No.
3.1 Pin Description of LCD 8































1. INTRODUCTION

1.1 Overview
This chapter introduces and gives basic information about the project. It also covers the
project background, academic based objectives. Different phases of the project have also
been discussed.
1.2 Background
A rangefinder is a device that measures distance from the observer to a target, for the purposes of
surveying, navigation, determining focus in photography, choosing a golf club, or accurately aiming a
weapon.
Various rangefinders have been proposed for ranging by a time difference between a sending time and
a receiving time of ultrasonic waves. The conventional rangefinders have the following problems as
follows.
In general, a receiving microphone for the ultrasonic waves, which is disposed adjacent to an
ultrasonic wave speaker, receives direct waves which are other than the ultrasonic reflection waves
from a ranging object, and traveling through short space from the speaker to the microphone, although
the strength of the direct waves coming back from various directions depends on directivity of the
sending speaker.
When the sending speaker and the receiving microphone are both mounted on a same supporter or a
frame, solid waves are also transmitted through a direct path in the supporter from the speaker to the
microphone. In the conventional rangefinders, an amplification degree of a signal for the reflection
waves is selected to be sufficient for receiving a small intensity signal produced by the reflection
waves from the object of a maximum range distance L.sub.max. But in such a case, the direct waves
may come back to the rangefinder earlier than the correct reflection waves and even small direct
waves may be amplified well to result in a pseudo-signal which is erroneously used for the time
difference determination. This problem would be resolved by decreasing the amplification degree for
the direct waves, but paradoxically the maximum range distance also becomes shorter by such a
measure. Therefore, the conventional rangefinders have been subject to the problems of the direct
waves. The conventional rangefinders have a further drawback that circuit configurations are
complicated.
Some devices use passive methods to measure e.g trigonometry. But the active method to measure
distance is by the use of ultrasonic waves. These methodologies use a set of known information,
usually distances or target sizes, to make the measurement.



1.3 Objectives
Objective of the project was to develop a device that can be used to measure the accurate distance of
the target using AT89S52 microcontroller. We aimed to design rangefinder free from the conventional
problems arising from the undesirable direct waves, wherein a signal level for detecting a right signal
due to the reflection waves from a ranging object is automatically varied and the detection of the right
signal is made by the time-dependent signal level.































2. LITERATURE REVIEW
2.1 Ultrasound
Ultrasound is a cyclic sound pressure wave with a frequency greater than the upper limit of the human
hearing range. Ultrasound is not interpreted by the humans because it operates on the frequency
greater than upper limit of audible human range. Although this limit varies from person to person, it is
approximately 20 kilohertz (20,000 hertz) in healthy, young adults. Ultrasound devices operate with
frequencies from 20 kHz up to several gigahertz.
2.2 Ultrasonic Sensors
Ultrasonic sensors (also known as transceivers when they both send and receive) work on a principle
which evaluate attributes of a target by interpreting the echoes from radio or sound waves
respectively. Ultrasonic sensors generate high frequency sound waves and evaluate the echo which is
received back by the sensor. Sensors calculate the time interval between sending the signal and
receiving the echo to determine the distance to an object. It has transducer and receiver. Figure 1
shows a very clear picture of the phenomenon.

Fig 2.1 Working Of Ultrasonic Sensor

2.2.1 Transmitter
An ultrasonic transducer is a device that converts energy into ultrasound, or sou nd waves above the
normal range of human hearing. The location at which a transducer focuses the sound can be
determined by the active transducer area and shape, the ultrasound frequency, and the sound velocity
of the propagation medium.
2.2.2 Receivers
It is device that detects the ultrasonic waves and converts ultrasound energy into voltage so that it can
be processed further. It usually comes in pair with transducer which is actually a transmitter.


3. SYSTEM DEVELOPMENT
3.1 Power Supply
The circuit of power supply is as shown below.
Vin Vout
GND
230V
50Hz
Transformer
(9V,1A)
Bridge
Rectifier
7805 Voltage
Regulator
220 ohm
1000F
33pF 104pF
GND

Fig. 3.1 Power Supply (5V DC)
3.1.1 Bridge Rectifier
The output of transformer is ac, but for our operation we want dc voltage. So to convert ac to
dc, we use a bridge rectifier. It consists of four p-n junction diodes connected in the form of
bridge. During positive half cycle of applied ac input, group of two diodes become forward
biased and we get dc output across resistor. Similarly, during negative half cycle, rest two
diodes become forward biased and we again get dc output across resistor.
3.1.2 Filter Circuit
The output of rectifier is pulsating dc i.e. it contains some ripples of ac. To remove these ac
components in dc output, we use a filter circuit. It consists of two capacitors viz. 1000F and
33pF.
The characteristic of a capacitor is that it allows ac to pass through it because of its low
resistance to it and blocks dc due to its infinite resistance to dc. Thus it passes ac ripples
through it and blocks dc. For reducing ripples to some more extent, another capacitor is also
used in filter circuit.






3.1.3 Voltage Regulator(IC 7805)
The output of filter again contains some ac components. To remove this, voltage regulator is
used. Here, we have used IC 7805 as voltage regulator. It provides 5V dc constant desired
output.
3.2 HC SR04 Ultrasonic Rangefinder Module
In the circuit, to measure the distance, Ultrasonic rangefinder Module (HC SR04) is used. This
Module has four pins viz. Vcc, Trig, Echo and Gnd. These are explained a follows.
1) Vcc:- 5V DC supply is given to this pin.
2) Trigger:- The trigger signal for starting the transmission is given to this pin. The trigger signal
must be a pulse with high time for 10s. When the module receives a valid trigger signal, it issues
8 pulses of 40KHz frequency from the transmitter. The echo of this sound is picked by receiver.
3) Echo :- At this pin, the module outputs a waveform proportional to the distance.
4) Gnd:- Ground is connected to this pin


Fig. 3.2 HC SR04 Timing Diagram[2]
The distance can be found out by using following formulae
1) Distance in cm = echo pulse width in s/58
2) Distance in inches = echo pulse width in s/158





Specifications of HC SR04 are as below.
Working Voltage: DC 5V
Working Current:15mA
Working Frequency:40Hz
Max Range:2.55m
Min Range:2cm
measuring Angle: no more than 15 degrees
Trigger input Signal:10uS TTL pulse
Dimension:45*20*15mm
The HC SR04 is interfaced with microcontroller through P3.0 and P3.1. Port 2 used for
transmitting data to display device (LCD). P1.0, P1.1 and P1.2 are used as control bits for LCD (RS,
RW and EN).
3.3 Microcontroller AT89S52
The AT89S52 is a low-power, high-performance CMOS 8-bit microcontroller with 8K bytes of in-
system programmable Flash memory. The device is manufactured using Atmels high-density
nonvolatile memory technology and is compatible with the industry-standard 80C51 instruction set
and pin out. The on-chip Flash allows the program memory to be reprogrammed in-system or by a
conventional nonvolatile memory programmer. By combining a versatile 8-bit CPU with in-system
programmable Flash on a monolithic chip, the Atmel AT89S52 is a powerful microcontroller which
provides a highly-flexible and cost-effective solution to many embedded control applications. The
AT89S52 provides the following standard features: 8K bytes of Flash, 256 bytes of RAM, 32 I/O
lines, Watchdog timer, two data pointers, three 16-bit timer/counters, a six-vector two-level interrupt
architecture, a full duplex serial port, on-chip oscillator, and clock circuitry. In addition, the AT89S52
is designed with static logic for operation down to zero frequency and supports two software
selectable power saving modes. The Idle Mode stops the CPU while allowing the RAM,
timer/counters, serial port, and interrupt system to continue functioning. The Power-down mode saves
the RAM con-tents but freezes the oscillator, disabling all other chip functions until the next interrupt
or hardware reset


Fig. 3.3 Pin diagram of AT89S52






3.4 LCD Display 16X2
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 preferred over 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. The command register stores the command
instructions given to the LCD. A command is an instruction given to LCD to do a predefined
task like initializing it, clearing its screen, setting the cursor position, controlling display etc.
The data register stores the data to be displayed on the LCD. The data is the ASCII value of
the character to be displayed on the LCD. Click to learn more about internal structure of a
LCD.
Table3.1 Pin description of LCD
Pin No Function Name
1 Ground (0V) Ground
2 Supply voltage; 5V (4.7V 5.3V) Vcc
3 Contrast adjustment; through a variable resistor V
EE

4 Selects command register when low; and data register when high Register Select
5 Low to write to the register; High to read from the register Read/write
6 Sends data to data pins when a high to low pulse is given Enable
7
8-bit data pins
DB0
8 DB1
9 DB2
10 DB3
11 DB4
12 DB5
13 DB6
14 DB7
15 Backlight V
CC
(5V) Led+
16 Backlight Ground (0V) Led-



4. PERFORMANCE ANALYSIS
4.1 Block Diagram



Ultrasonic
Rangefinder
Module
Echo
Trig
IC1
AT89S52
P3.0
P3.1
P2.0-
P2.7
LCD Display 16X2

Fig. 4.1 Block Diagram

















4.2 Circuit Diagram

Fig.4.2. Interfacing Of HC SR 04 and LCD with AT89S52









4.3 Working
4.3.1 Algorithm
1) First transducer (transmitter) converts the electrical signal to ultrasonic waves. This ultrasonic
beam of wave is transmitted and reflected back after hitting the target.
2) The other transducer (receiver) detects the reflected wave.
3) Timer is started when wave is being transmitted and stopped at the moment when receiver
detects it.
4) The time shown by the timer is the time, an ultrasonic wave has taken to go to target and return
back.
5) Half of this time is calculated and put in the given formula to get the distance of the target.
6) Distance = velocity time, where velocity is the speed of sound equal to 340 m/s.

4.3.2Hardware Implementation
For the hardware implementation, there are three main parts,
1) Microcontroller AT89S52
2) Ultrasonic Rangefinder Module (HC SR04)
3) 16X2 LCD Display
P3.0 is used as output pin for sending trigger signal to HC SR04 and P3.1 is used as input pin
for receiving echo. TMOD register of microcontroller is loaded with value 20h so that timer 1
works in mode 2 i.e. auto reload mode. In this mode, TL1 is loaded with a value (207d) to
start counting from and Th1 with a value to be reloaded in TL1 when its count overflows i.e.
crosses FF h. When TR1 bit of TCON register is set to 1, TL1 starts counting from the value
loaded in it. When rollover takes place, TF1 is set to 1 and value in TH1 is loaded into TL1 to
start counting again. This continues until TR1 bit of TCON is made low.
Initially, P 3.0 is made high for period of 10s and cleared to make a trigger pulse of 10s. The
ultrasonic module, on receiving this 10s trigger pulse, issues 40KHz pulse. Now program waits until
wave strikes obstacle and is received at echo terminal. After receiving echo, timer 1 starts counting
from its initial value to final count. After overflow, if echo is still received then counter will again
start counting from the reloaded value. For every reload, accumulator is incremented by 1. This
continues until echo is received. When there is no echo, counter stops counting. The current value of
accumulator is in fact proportional to the distance (and also to pulse width of signal generated at echo
terminal).
The value in accumulator will be a number. To display that number, it is first divided by 100 to get
the first digit of number which is then sent to display function to display on LCD. Then, the remainder
obtained by dividing that number by 100, is divided by 10 to get the second digit. Then the number

obtained by dividing number in accumulator by 100,is again divided by 10 and the remainder is sent
to display function to get the third digit.
From the datasheet of HC SR04, it is clear that pulse with of 58s indicates distance of 1cm. When
the microcontroller is triggered by a 12MHz crystal, 58counts of timer 1 indicates 1cm i.e.
1 reload= 1cm. But, as branching instructions in the program, which are used to check the status of
P3.0 and P3.1,take time to execute. Thus, the individual time lag caused due to each branching
instruction will cumulatively and thus the rangefinder will show higher distance than the original one.
Thus, in order to recover from this lag, count of 48 is to be done by the timer i.e. initial value in TL1
is 207. This reduces the error to less than half a centimeter.



























5. CONCLUSION
5.1 Conclusion
From the project, we concluded that the ultrasonic rangefinder module HC SR04 can measure
distance of 255cmwhich is its maximum range with accuracy of 1cm. To increase the accuracy, the
initial count in the counter should be experimentally changed. Also to increase distance measuring
ability, the frequency of ultrasonic wave emitted by rangefinder should be decreased so as to increase
the wavelength.
5.2 Future Scope
In future, by increasing its distance measuring ability, this device can be used in
warfare activities, in surveillance purposes at borders etc.
5.3 Applications
It can be used in robotics to detect the obstacles in the path of robot and their distance, so as
to avoid it. It can also be used in SONAR system on ships to calculate the distance on and
under water.





















REFERENCES
[1] P. Sumathi and P. A. Jankiraman, SDFT-Based Ultrasonic Range Finder Using AM
Continuous Wave and Online Parameter Estimation, IEEE TRANSACTIONS ON
INSTRUMENTATION AND MEASUREMENT, VOL. 59, NO.8, pp. 1994-2004, August
2010

[2] www.circuitstoday.com
[3] www.electronicsforu.com
[4] www.wikipedia.org


























APPENDIX
Code:-
#include<reg51.h>
sfr lcd=0xa0;
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
sbit trig=P3^0;
void delay( );
void delay1( );
void display(int);
void command(unsigned char);
void lcddata(unsigned char);
void init( );
void string(unsigned char *);
void main( )
{
unsigned int D,b,c,d;
init( );
command(0x80);
lcddata('H');
lcddata('I');
command(0x01);
string("PLEASE WAIT");
lcddata('.');
delay( );
lcddata('.');
delay( );
lcddata('.');
delay( );
delay( );
delay( );
command(0x01);

string("READY");
delay( );
delay( );
command(0x01);
string("The distance");
command(0xc0);
string("is ");
TMOD=0x20;
MAIN:
TL1=207;
TH1=207;
ACC=0;
trig=1;
delay1( );
trig=0;
HERE:
if(P3^1!=1)
{
goto HERE;
}
BACK:
TR1=1;
HERE1:
if(TF1!=1)
{
goto HERE1;
}
TR1=0;
TF1=0;
++ACC;
if (P3^1==1)
{
goto BACK;
}

D=ACC;
b=D/100;
display(b);
delay( );
c=(D%100)/10;
display(c);
delay( );
d=(D%100)%10;
display(d);
delay( );
string(" cm");
delay( );
delay( );
delay( );
command(0x01);
goto MAIN;
}
void init( )
{
command(0x38);
command(0x01);
command(0x06);
command(0x0e);
}
void command(unsigned char a)
{
rs=0;
lcd=a;
rw=0;
en=1;
delay();
en=0;
}


void lcddata(unsigned char a)
{
rs=1;
lcd=a;
rw=0;
en=1;
delay( );
en=0;
}

void delay( )
{
unsigned char i,j;
for(i=1;i<200;i++)
{
for(j=0;j<=250;j++)
{
}
}
}

void string(unsigned char *s)
{
while(*s!='\0')
lcddata(*s++);
}

void delay1( )
{
unsigned char k;
for(k=0;k<=2;k++)
{
}
}


void display(int a)
{
switch(a)
{
case 0: lcddata('0');
break;
case 1: lcddata('1');
break;
case 2: lcddata('2');
break;
case 3: lcddata('3');
break;
case 4: lcddata('4');
break;
case 5: lcddata('5');
break;
case 6: lcddata('6');
break;
case 7: lcddata('7');
break;
case 8: lcddata('8');
break;
case 9: lcddata('9');
break;
}
}

You might also like