You are on page 1of 56

ULTRASONIC RANGE FINDER

A PROJECT REPORT

Submitted by

NISHIT CHITTORA

in partial fulfillment for the award of the degree

of

BACHELOR OF ENGINEERING

in

ELECTRONICS AND COMMUNICATION ENGINEERING

GEETANJALI INSTITUE OF TECHNICAL STUDIES, UDAIPUR

RAJASTHAN TECHNICAL UNIVERSITY

MAY 2011
GEETANJALI INSTITUTE OF TECHNICAL STUDIES,
DABOK, UDAIPUR, RAJASTHAN, INDIA
(Affiliated to Rajasthan Technical University, Kota, Rajasthan)

CERTIFICATE

Certified that this project report “ULTRASONIC RANGE FINDER”

is the bonafide work of “NISHIT CHITTORA” who carried out the project

work under my supervision.

SIGNATURE SIGNATURE

Mr. Rajeev Mathur Mr. Satish Somani

HEAD OF THE DEPARTMENT SUPERVISOR


ECE Department Senior lecturer,
GITS, Dabok, Udaipur ECE Department
GITS,Dabok, Udaipur
ABSTRACT

The report details the implementation of distance measurement system using the

ultrasonic waves. As the human ear’s audible perception range is 20 Hz to 40

kHz, it is insensitive to ultrasonic waves, and hence the ultrasound waves can be

used for applications in industries/vehicles without hindering human activity.

They are widely used as range meters and proximity detectors in industries also

it can be used in parking assistance system. The distance can be measured using

pulse echo and phase measurement method. Here the pulse echo methods used.

The measurement unit uses a continuous signal in the transmission frequency

range of ultrasonic transducers. The signal is transmitted by an ultrasonic

transducer, reflected by an obstacle and received by another transducer where

the signal is detected. The time delay of the transmitted and the received signal

corresponds to the distance between the system and the obstacle.


TABLE OF CONTENTS

CHAPTER NO. TITLE PAGE NO.

ABSTRACT iii

LIST OF FIGURES vi

1. INTRODUCTION 1

1.1 OBJECTIVE

1.2 ULTRASONIC RANGE FINDER 2

1.3 BLOCK DIAGRAM OF ULTRASONIC 4

RANGE FINDER

1.4 THEORY OF OPERATION 4

1.5 ULTRASONIC WAVES 5

1.5.1 LONGITUDINAL WAVES 6

1.5.2 TRANSVERSE WAVES 6

1.6 ULTRASONIC DISTANCE SENSORS 7

2 CIRCUIT DESCRIPTION 8

2.1 POWER SUPPLY 11

2.2 CONSTRUCTION AND TESTING 12

3 SOFTWARE 14

3.1 ASSEMBLY LANGUAGE PROGRAM 14

3.2 OTHER USES 20


4 CONCLUSION 23

4.1 APPLICATION 24

APPENDICES 25

APPENDIX-A 25

APPENDIX-B 27

APPENDIX-C 28

APPENDIX-D 31

APPENDIX-E 36

REFERENCES 49
LIST OF FIGURES

1. BLOCK DIAGRAM OF ULTRASONIC RANGE FINDER 4

2. TRANSMITTING AND RECEIVING WAVES 5

3. ULTRASONIC TRANSDUCER (TRANSMITTER AND RECEIVER 7

4. CIRCUIT DIAGRAM OF ULTRASONIC RANGE FINDER 9

5. POWER SUPPLY CIRCUIT DIAGRAM 11

6. TRANSMITTED AND RECEIVED PULSES 13

7. PCB LAYOUT (TOP) 25

8. PCB LAYOUT (BOTTOM) 25

9. CAPACITOR CHARGE FORMATION 29

10 CAPACITOR CHARGE FORMATION 2 29

11. IC 89C2051 PINOUT DIAGRAM 35

12. IC 89C2051 BLOCK DIAGRAM 36

13. (A) OSCILLATOR CONNECTIONS 38

(B) EXTERNAL CLOCK DRIVE CONFIGURATION 38

14. (A) PROGRAMMING THE FLASH MEMORY 42

(B)VERIFYING THE FLASH MEMORY 42

15. FLASH PROGRAMMING AND VERIFICATION WAVEFORMS 43

16. EXTERNAL CLOCK DRIVE WAVEFORM 45


1. INTRODUCTION

The techniques of distance measurement using ultrasonic in air include


continuous wave and pulse echo technique. In the pulse echo method, a burst of
pulses is sent through the transmission medium and is reflected by an object
kept at specified distance. The time taken for the pulse to propagate from
transmitter to receiver is proportional to the distance of object. For contact less
measurement of distance, the device has to rely on the target to reflect the pulse
back to itself. The target needs to have a proper orientation that is it needs to be
perpendicular to the direction of propagation of the pulses. The amplitude of the
received signal gets significantly attenuated and is a function of nature of the
medium and the distance between the transmitter and target. The pulse echo or
time-of-flight method of range measurement is subject to high levels of signal
attenuation when used in an air medium, thus limiting its distance range.

1.1 OBJECTIVE

The object of this work is to replace the old traditional range detector, used in
several applications. In present work the object position is measured
electronically by using seven segment displays by replacing the heavy and
bulky circuits with the compact circuits using intelligent Microcontroller. The
bulky pressing switch is replaced by the small and one touch tactile switch. It
saves electric consumption, saves the no. of man power, through seven segment
display and one microcontroller as well as ultrasonic receiver & transmitter
sensors.
1.2 ULTRASONIC RANGE FINDER

There are several ways to measure distance without contact. One way is to use
ultrasonic waves at 40 kHz for distance measurement. Ultrasonic transducers
measure the amount of time taken for a pulse of sound to travel to a particular
surface and return as the reflected echo. This circuit calculates the distance
based on the speed of sound at 25°C ambient temperature and shows it on a 7-
segment display. Using it, you can measure distance up to 2.5 meters. For this
particular application, the required components are AT89C2051
microcontroller, two 40kHz ultrasonic transducers (one each for transmitter
and receiver), current buffer ULN2003, operational amplifier iM324I inverter
Ca4M4VI four T-segment displays I five transistors and some discreet
components. The ultrasonic transmitter- receiver pair is shown in Ultrasonic
generators use piezoelectric materials such as zinc or lead zirconium tartrates or
quartz crystal. The material thickness decides the resonant frequency when
mounted and excited by electrodes attached on either side of it. The medical
scanners used for abdomen or heart ultrasound are designed at 2.R Mez. fn this
circuit, a 40kHz transducer is used for measurement in the air medium. The
velocity of sound in the air is around 330 m/s at 0°C and varies with
temperature.
In this project, you excite the ultrasonic transmitter unit with a 40kHz pulse
burst and expect an echo from the object whose distance you want to
measure. Fig. 2 shows the transmitted burst, which lasts for a period of
approximately 0.5 ms. It travels to the object in the air and the echo
signal is picked up by another ultrasonic transducer unit (receiver), also a 40
kHz pre-tuned unit. The received signal, which is very weak is amplified several
times in the receiver circuit and appears somewhat as shown in Fig. 2 when
seen on a CRO. Weak echoes also occur due to the signals being directly
received through the side lobes. These are ignored as the real echo received
alone would give the correct distance. That is why we should have a level
control. Of course, the signal gets weaker if the target is farther than 2.5 and
will need a higher pulse excitation voltage or a better transducer. Here the
microcontroller is used to generate 40 kHz sound pulses. It reads when the echo
arrives; it finds the time taken in microseconds for to-and-fro travel of sound
waves. Using velocity of 333 m/s, it does the calculations and shows on the four
7-segment displays the distance in centimeters and millimeters (three digits for
centimeters and one for millimeters).
1.3 BLOCK DIAGRAM OF ULTRASONIC RANGE FINDER

Figure 1 : Block Diagram of Ultrasonic Range Finder

1.4 THEORY OF OPERATION

The Ping sensor detects objects by emitting a short ultrasonic burst and then
"listening" for the echo. Under control of a host microcontroller (trigger pulse),
the sensor emits a short 40 kHz (ultrasonic) burst. This burst travels through the
air at about 1130 feet per second, hits an object and then bounces back to the
sensor. The PING sensor provides an output pulse to the host that will terminate
when the echo is detected, hence the width of this pulse corresponds to the
distance to the target.

Figure 2: Transmitting and Receiving Waves

1.5 ULTRASONIC WAVES

Sound waves with frequency range from 20 Hz to 20 KHz are responsive to the
human ear. Vibrations above this frequency are termed as ultrasonic. Ultrasonic
signals are affected by the properties of the medium. Thus while passing
through a particular medium these signals get attenuated. The attenuation of
ultrasonic signal is taken as the means for the measurement of distance of the
target and for different other applications Ultrasonic distance sensors are used to
detect the presence of flaw by measuring the distance. They do so by evaluating
the echo of a transmitted pulse with concern to its travel time. Time dependent
control of sensitivity is used to compensate the distance dependency of the echo
amplitude, while different reflection properties are compensated by an
automatic gain control, which holds the average echo amplitude constant. Echo
amplitude therefore has very little influence on the accuracy of the distance
measurement provided the signal to noise ratio is not very low. By considering
whether the echo has been received within a time window, i.e. a time interval,
which can be preset by the user, the distance range is given in which the sensor
responds to the presence of an object. Using this technique, interference can be
suppressed and relevant objects are monitored more reliably.

A variety of ultrasonic presence sensors with different operation frequencies are


designed for different distance range and different resolution. Such sensors are
employed in the automation of industrial processes as well as in traffic control
systems, for example to monitor, whether car parking places are occupied.
Ultrasonic distance meters are used for the measurement of the filling level in
containers or the height of material on conveyor belts. Ultrasonic waves are
generally used two types which are given as :-

1.5.1 LONGITUDINAL WAVES


Longitudinal waves exist when the motion of the particle and the medium is
parallel to the direction of propagation of the waves. These types of waves are
referred as L –waves. Since these can travel in solid, liquid and gases. These
waves can be easily detected.

1.5.2 TRANSVERSE WAVES


In this case particles of the medium vibrate at right angle to the direction of
propagation of the waves. These are also called shear waves.
1.6 ULTRASONIC DISTANCE SENSORS

Ultrasonic sonar sensors actively transmit acoustic waves and receive them
later. This is done by ultrasonic transducers, which transform an electrical
signal into an ultrasonic wave and vice versa. The ultrasound signal carries the
information about the variables to be measured. The task for the ultrasonic
sensors is not merely to detect ultrasound, as intelligent sensors they have to
extract the information carried by the ultrasonic signals efficiently and with
high accuracy. To achieve this performance, the signals are processed,
demodulated and evaluated by dedicated hardware. Algorithms based on models
for the ultrasonic signal propagation and the interaction between the physical or
chemical variables of interest are employed (munich,1994).
Furthermore, techniques of a sensor specific signal evaluation are being applied.
Ultrasonic sensors can be embedded into a control system that accesses
additional sensors, combines information of the different sensors, handles the
bus protocols and initiates actions.

Figure 3: Ultrasonic Transducer (Transmitter and Receiver)

2.
3. CIRCUIT DESCRIPTION

Figure 4 shows the circuit of the microcontroller based distance meter. The
40kHz pulse bursts from the microcontroller are amplified by transistor T5.
Inverting buffer CD4049 drives the ultrasonic sensor used as the transmitter.
Three inverters (N1, N2 and N3) are connected in parallel to increase the
transmitted power. This inverted output is fed to another set of three inverters
(N4, N5 and N6). Outputs of both sets of parallel inverters are applied as a push
pull drive to the ultrasonic transmitter.
The positive going pulse is applied to one of the terminals of the ultrasonic
sensor and the same pulse after 180-degree phase shift is applied to another
terminal. Thus the transmitter power is increased for increasing the range. If you
want to increase the range up to 5 meters, use a ferrite-core step-up pulse
transformer, which steps-up the transmitter output to 60V (peak-to- peak). The
echo signal received by the receiver sensor after reflection is very weak. It is
amplified by quad operational amplifier LM324. The first stage (A1) is a buffer
with unity gain. The received signal is directly fed to the non-inverting input
(pin 3) of A1 and coupled to the second stage by a 3.3nF (small-value)
capacitor. If you use the ubiquitous 0.01μF capacitor for coupling, there will be
2-mega-ohm resistor for feedback. The third stage is a precision rectifier
amplifier with a gain of 10. The rectifier functions, unlike a simple diode, even
for signal voltage of less than 0.6V. The output is filtered to accept 40 kHz
frequencies and fed to pin 12 of microcontroller AT89C2051, which is an
analogue comparator. Pin 13 is the other pin of the
Figure 4: Circuit Diagram of Ultrasonic Range Finder

Comparator used for level adjustment using preset VR1. The ultrasonic
transducer outputs a beam of sound waves, which has more energy on the main
lobe and less energy (60 dB below the main lobe) on the side lobes as shown in
Fig. 4. Even this low side-lobe signal is directly picked up by the receiver unit.
So you have to space the transmitter and receiver units about 5 cm apart. The
two units are fixed by cello tape on to a cardboard, with the analogue circuit at
one end. Microcontroller AT89C2051 is at the heart of the circuit. Port-1 pins
P1.7 through P1.2, and port-3 pin P3.7 are connected to input pins through 1 to
7of IC2 (IC ULN2003), respectively. These pins are pulled up with a 10-kilo-
ohm resistor network RNW1. They drive all the segments of the 7-segment
display with the help of inverting buffer IC2. Port-3 pins P3.0 through P3.3 of
the microcontroller are connected to the base of transistors T1 through T4 to
provide the supply to displays DIS1 through DIS4, respectively. Pin P3.0 of
microcontroller IC1 goes low to drive transistor T1 into saturation, which
provides supply to the common- anode pin (either pin 3 or 8) of display DIS1.
Similarly, transistors T2 through T4 provide anode currents to the other three 7-
segment displays. Microcontroller IC1 provides the segment data and display-
enable signal simultaneously in time-division multiplexed mode for displaying a
particular number on the 7-segment display unit. Segment data and display-
enable pulse for the display are refreshed every 5 ms. Thus the display appears
to be continuous, even though the individual LEDs used in it light up one by
one. Using switch S1 you can manually reset the microcontroller, while the
power on reset signal for the microcontroller is derived from the combination of
capacitor C4 and resistor R8. A 12MHz crystal is used to generate the basic
clock frequency for the microcontroller. Resistor R16 connected to pin 5 of
DIS2 enables the decimal point. The comparator is inbuilt in microcontroller
AT89C2051. The echo signal will make port-3 pin 3.6 low when it goes above
the level of voltage set on pin 13. This status is sensed by the microcontroller as
programmed. When port-3 pin P3.6 goes high, we know that the echo signal has
arrived; the timer is read and the 16-bit number is divided by twice the velocity
of sound and then converted into decimal format as a 4-digit number.

2.1 POWER SUPPLY


Figure shows the circuit of the power supply. The 230V AC mains is stepped
down by transformer X1 to deliver the secondary output of 15V-0-15V, 500
mA. The transformer output is rectified by a full-wave bridge rectifier
comprising diodes D3 through D6, filtered by capacitors C8 and C9 and then
regulated by ICs 7815 (IC5), 7915 (IC6)and 7805 (IC7). Regulators 7815, 7915
and 7805 provide +15V, -15V and+5V regulated supply, respectively.
Capacitors C10 through C12 bypass the ripples present in the regulated power
supply.
Figure 5: Power Supply Circuit Diagram

2.2 CONSTRUCTION AND TESTING


An actual-size, single-side PCB for the microcontroller-based distance meter is
shown in Fig. 6 and its component layout in Fig. 7. Assemble the PCB and put
the programmed microcontroller into the socket. After switching on the power
supply and microcontroller automatically getting reset upon power-’on,’ pin 8
will pulse at 40kHz bursts. This can be seen using an oscilloscope. Give this
signal to channel 1 of the oscilloscope. Adjust the time base to 2 ms per division
and set it to trigger mode instead of normal mode. Adjust the potentiometer on
the oscilloscope labeled ‘level’ such that the trace starts with the burst and
appears steady as shown. Connect the transmitter and receiver ultrasonic units
either by a twisted pair of wire or by a shielded cable to the board. Give the
received signal to channel 2 of the oscilloscope. Then, place an A4-size plastic
sheet in front of the ultrasonic transducers and observe the echo signal. It will
appear as shown. The two transducers can be fixed to a thick cardboard with
two wires leading to the circuit—two 40cm long shielded cables will do. The
laser pointer is fixed such that it is axial to the transducers. Channel 2 is
connected to pin 12, which is the positive non-inverting terminal of
AT89C2051’s comparator. The negative inverting terminal (pin 13) is
connected to a preset reference. Adjust the preset such that the voltage is 0.1V-
0.2V at pin 13. This will enable detection of weak echoes also. When the echo
signal goes above the level of reference voltage set on pin 13, it will make P3.6
low; the arrival of echo is sensed by the program using jnb p3.6 (jump not bit)
instruction. Software The software is \ written in Assembly language and
assembled using 8051 cross-assembler. It is well commented and easy to
understand. The pulse train for 0.5 ms is started by making pin 8 high and low
alternately for 12.5 microseconds so that the pulse frequency is 40 kHz. After
25 such pulses have passed, a waiting time is given to avoid direct echoes for
about 20 μs. Then the signal is awaited, while the timer runs counting time in
microseconds. When the echo arrives, port-3 pin P3.6 goes high, the timer reads
and the 16-bit number is divided by twice the velocity and converted into
decimal format as a 4-digit number. If the echo does not arrive even after 48
milliseconds, the waiting loop is broken and the pulse train sequence is started
once again. If the echo comes within this time, it is displayed for half a second
before proceeding to another measurement. Thus, the display appears
continuous and flicker-free. Other uses Simply by changing this program, the
same unit can be made to detect moving objects (such as cars racing on the
street) and find their range and speed. It can also be used with suitable
additional software as a burglar alarm unit for homes or offices.
Figure 6: Transmitted and Received Pulses

3. SOFTWARE

The software is written in Assembly language and assembled using 8051 cross-
assembler. It is well commented and easy to understand. The pulse train for 0.5
ms is started by making pin 8 high and low alternately for 12.5 microseconds so
that the pulse frequency is 40 kHz. After 25 such pulses have passed, a waiting
time is given to avoid direct echoes for about 20 μs. Then the signal is awaited,
while the timer runs counting time in microseconds. When the echo arrives,
port-3 pin P3.6 goes high, the timer reads and the 16-bit number is divided by
twice the velocity and converted into decimal format as a 4-digit number. If the
echo does not arrive even after 48 milliseconds, the waiting loop is broken and
the pulse train sequence is started once again. If the echo comes within this
time, it is displayed for half a second before proceeding to another
measurement. Thus, the display appears continuous and flicker-free.

3.1 ASSEMBLY LANGUAGE PROGRAM


PROGRAM LISTING:
$mod51
ORG 0H
AJMP 30H
ORG 0BH ; //TIMER 0 INTERRUPT VECTOR;
AJMP TIMER0ISR ; //Timer 0 Interrupt
//service routine address
ORG 30H
MOV SP,#60H ; //set stack pointer
MOV P3,#0FFH ; //set all port 3 bits high
//to enables inputs also
MOV P1,#03 ; //set port 1 to all zeros
//expect bits 0,1
MOV TMOD,#01100001B ; //TIMER 1 - MODE 2COUNTER,
//TIMR-0 TO MODE 1

BEG: MOV TH0,#0H ; //TIMER REG.0 IS SET TO 0, GIVES


//64ms
MOV TL0, #0 ;

/*timer low reg. is also so; total cycle time is 64.6ms ,350m/s gives
0.35mx65=22.5m b; up and down 10 meters say! .35 m/ms,.35 mm/us, 1mm
per 3 micros ; up and down .35/2 mm/us = 1/6 mm/us ; velocity of sound in
air is 350 m/s ; after 100 times, we have to stop transmitting for a time of
about .1 s ; so we stop for this amount of time and expect an echo.*/

mov r2,#25 ; //25 pulses 26 us =.53 ms //


(343m/s*.5ms=17cm)
pulse: setb p3.4 ; //generates 40KHz
mov r1,#5
djnz r1,$
clr p3.4
mov r1,#5
djnz r1,$ ; //wait for 13 us
djnz r2, pulse ; //20pulses
setb tr0 ; //start timer
mov r2,#10
djnz r2,$ ; //wait 20 us

/*check_echo:*/
jnb p3.6, //check timeout
MOV 40h,TL0 ; //read timer count
MOV 41h,TH0
mov r0,40h
mov r1,41h
mov r3,#0
mov r2,#6
call UDIV16 ; //divide by 6
mov 40h,r0
mov 41h,r1
mov 50h,#25
disp: call disp1 ; //show the value on LED
djnz 50h,disp ; //so many times for a visible time limit
jmp beg
checktimeout: mov a,th0
cjne a,#0c0h,check_echo ; //upto 4 metres
jmp beg;

/*subroutine UDIV16 ;16 bit/16bit unsigned divide ;input r1,r0 =dividend


X ;input r3,r2 =divisor Y ;output r1,r0 =quotient q of x/y ;output r3,r2 =
remainder ; alters acc,r4-47,flags,dptr*/

UDIV16: mov r7,#0 ; //clear partial remainder


mov r6,#0 ;
mov B,#16 ; //set loop count
div_loop: clr C ; //clear carry flag
mov a,r0 ; //shift the highest bit of dividend into
rlc a
mov r0,a
mov a,r1
rlc a
mov r1,a
mov a,r6 ; //the lowest bit of partial remainder
rlc a
mov r6,a
mov a,r7
rlc a
mov r7,a
mov a,r6
mov r0,a
mov a,r7 ; //get rem. saved before the
mov r3,a ; //last subtraction.
mov a,r6
mov r2,a
ret

/*;16 Bit Hex to BCD Conversion for 8051 Microcontroller ; This routine is
for 16 bit Hex to BCD conversion; ;Accepts a 16 bit binary number in
R1,R2 and returns 5 digit BCD in ; R7,R6,R5,R4,R3(up to 64K )
Hex2BCD: ;r1=high byte ;r7 most significant digit*R2 = LSByte*/

MOV R3,#00D
MOV R4,#00D
MOV R5,#00D
MOV R6,#00D
MOV R7,#00D
MOV B,#10D
MOV A,R2
DIV AB
MOV R3,B ;
MOV B,#10 ; R7,R6,R5,R4,R3
DIV AB
MOV R4,B
MOV R5,A
CJNE R1,#0H, //HIGH_BYTE ; CHECK FOR HIGH
BYTE
SJMP ENDD
HIGH_BYTE: MOV A,#6
ADD A,R3
MOV B,#10
DIV AB
MOV R3,B
ADD A,#5
ADD A,R4
MOV B,#10
DIV AB
MOV R4,B
ADD A,#2
ADD A,R5
MOV B,#10
DIV AB
MOV R5,B
CJNE R6,#00D,ADD_IT
SJMP CONTINUE
ADD_IT: ADD A,R6
CONTINUE: MOV R6,A
DJNZ R1,HIGH_BYTE
MOV B, #10D
MOV A,R6
DIV AB
MOV R6,B
MOV R7,A
ENDD: ret
DISP1:
REFRESH: ;

/*content of 18 to 1B memory locations are output on LEDs;


only numbers 0 to 9 and A to F are valid data in these locations*/

mov r1,41h
mov r2,40h
CALL HEX2BCD
MOV 18H,r3 ; // least significant digit
MOV 19H,r4 ; //next significant digit
MOV 1AH,r5
mov r2,#10
djnz r2,$ ; //wait 20 us
check_echo:
jnb p3.6, //check timeout
MOV 40h,TL0 ; //read timer count
MOV 41h,TH0
mov r0,40h
mov r1,41h
mov r3,#0
mov r2,#6
call UDIV16 ; //divide by 6
mov 40h,r0
mov 41h,r1
mov 50h,#25
disp: call disp1 ; // show the value on LED
djnz 50h,disp ; //so many times for a visible time limit
jmp beg
checktimeout: mov a,th0
cjne a,#0c0h,check_echo ; //upto 4 metres
jmp beg
;subroutine UDIV16

/*;16 bit/16bit unsigned divide ;input r1,r0 =dividend X ;input r3,r2


=divisor Y ;output r1,r0 =quotient q of x/y ;output r3,r2 = remainder ;
alters acc,r4-47,flags,dptr*/

UDIV16: mov r7,#0 ; //clear partial remainder


mov r6,#0 ;
mov B,#16 ; //set loop count
div_loop: clr C ; //clear carry flag
mov a,r0 ; //shift the highest bit of dividend into
rlc a
mov r0,a
mov a,r1
rlc a
mov r1,a
mov a,r6 ; //the lowest bit of partial remainder
rlc a
mov r6,a
mov a,r7
rlc a
mov r7,a
mov a,r6
clr C
subb a,r2
mov dpl,a
mov a,r7
subb a,r3
mov dph,a
cpl C
jnc div_1 ; //update partial remainder if borrow
mov r7,dph
mov r6,dpl ; //update parital reminder
div_1: mov a,r4
rlc a
mov r4,a
mov a,r5
rlc a
mov r5,a
djnz B,div_loop
mov a,r5
mov r1,a ; //put qt. in r0,r1
mov a,r4
MOV 1BH,R6 ; //most significant digit (max:9999)
refresh1: MOV R0,#1bh ; //1b,1a,19,18,holds values for 4 digits
MOV R4,#8 ; // pin p3.3_ 0 made low one by one starts //with
18
mov r7,#2 ; //decimal pt.on 3rd digit from left (2 nd
//fromright)
PQ2: CALL SEGDISP
deC R0
mov a,r4
rrc a
mov r4,a
jnc pQ2
PV3:
RET
SEGDISP:
mov dptr,#ledcode
MOV A,@R0
ANL A,#0FH
MOVC A,@A+dptr
segcode:
MOV R5,A
ORL A,#03H ;

/*WE WANT TO USE PORT 1 BITS 0 AND 1 FOR INPUT ANLOG ; so


retain them high*/
S3: MOV P1,A ; //SEGMENT_PORT
MOV A,R5 ; //we use p3.7 for the segment ‘a’ of display
RRC A ; //so get that bit D0into carry
; cpl c
; mov p3.5,c ; //dec pt is D0 bit that is wired to p3.5
rrc a
mov p3.7,c ; //segment ‘a;
S1: MOV A,R4 ; //get digit code from r4 00001000
cpl a ; //11110111
rrc a ; //11111011-1
mov p3.0,c ; //output to drive transistors for digit lighting
rrc a ; //11111101-1
mov p3.1,c
rrc a ; //11111110-1
mov p3.2,c
rrc a ; //1111111-0 yes low makes //leftmost //digit show ms digit
mov p3.3,c
S5:
S4: ACALL DELAY1 ; //let it burn for some time
MOV A,#0ffH ; //extinguish the digit after that time
MOV P3,A ; //to prevent shadow
s6: RET
ledcode:
DB 7EH, 0CH, 0B6H, 9EH, 0CCH, 0DAH, 0FAH
DB 0EH, 0FEH, 0CEH, 0EEH, 0F8H, 72H, 0BCH, 0F6H;
//these is code for
//the numbers 0 to 9 and A to F
DELAY1: MOV R1, #0ffH
N: NOP
DJNZ R1, N
RET
END

3.2 OTHER USES


Simply by changing this program, the same unit can be made to detect moving
objects (such as cars racing on the street) and find their range and speed. It can
also be used with suitable additional software as a burglar alarm unit for homes
or offices.
4.CONCLUSION
The objective of this project is to design and implement an Ultrasonic
Obstruction Detection and Distance Measurement device. As described in this
report a system is developed that can detect objects and calculate the distance of
the tracked object. With respect to the requirements for an ultrasonic range
finder the following can be concluded.
• The system is able to detect objects within the sensing range.
• The system can calculate the distance of the obstruction with
sufficient accuracy.
• This device has the capability to interact with other peripheral if
used as a secondary device.
• This can also communicate with PC through its serial port.
• This offers a low cost and efficient solution for non contact type
distance measurements.
The Range Finder has numerous applications. It can be used for automatic
guided vehicles, positioning of robots as well as measuring generic distances,
liquid levels in tanks, and the depth of snow banks. The device can serve as a
motion detector in production lines. The ultrasonic detection range relates with
size, figure, material and position of the object. The bigger the reflector is, the
better the reflectance is, and the stronger the reflection signal is. The ultrasonic
distance measurement is an untouchable detection mode. Compared with else
detection modes, it does not get much influenced by ray, temperature and colour
etc, and it has the great capability to adapt to various circumstances and ambient
conditions. A restricted target angle (it requires a near perpendicular surface)
and large beam, which can create poor resolution, seem to be the Range
Finder’s only limitations. Also there is a blind area and distance limitation in
ultrasonic distance measurement. Despite these drawbacks, we find the device’s
main features to be extremely useful.
4.1 APPLICATIONS

Applications of ultrasonic can be divided into two categories.


1. Ultrasonics in industry
2. Ultrasonics in medicine
Both are big fields in themselves. The concentration would be more on the
former one.
In industry ultrasonic is employed for :-
a) Low power applications where in the ultrasonic energy explores a body
of material and is thereby modified.
b) High power application where in the ultrasonics energy modifies the
body of material to which it is applied.
Some of the important low power applications are :-
1) Flow detection,
2) Thickness gauging,
3) Measurement of various physical properties of materials.
4) Extent of corrosion
5) Estimation of grain sizes in polycrystalline materials.
6) Measurement of pressure, concentration temperature, viscosity and flow
rates.
7) leak detection
8) Variable delay lines for computer applications and imaging,
9) Liquid level control
APPENDIX –A

SEMICONDUCTORS:
S.No. NOTATTION COMPONENT
1. IC1 AT89C2051 microcontroller
2. IC2 ULN2003 current buffer
3. IC3 CD4049 hex inverting buffer
4. IC4 LM324 quad operational amplifier
5. IC5 7815, 15V regulator
6. IC6 7915, -15V regulator
7. IC7 7805, 5V regulator
8. T1,T4 BC557 pnp transistor
9. T5 2N2222 npn transistor
10. D1, D2 1N4148 switching diode
11. D3-D6 1N4007 rectifier diode
12. DIS1-DIS4- LTS 542 common-anode, 7-segment display

Table 1: Semiconductor Components

RESISTORS (all ¼-watt, ±5% carbon):


S.No. Notation Rating
1. R1, R2 2-mega-ohm
2. R3 82-kilo-ohm
3. R4, R7-R10 10-kilo-ohm
4. R5 33-kilo-ohm
5. R6 100-kilo-ohm
6. R11 1-kilo-ohm
7. R12-R15 1.2-kilo-ohm
8. R16 220-ohm
9. RNW1 10-kilo-ohm resistor network
10. VR1 1-kilo-ohm preset

Table 2: Resistors

CAPACITORS
S.No. Notation Rating
1. C1, C2 3.3nF ceramic disk
2. C7, C10-C12 0.1μF ceramic disk
3. C3 2.2nF ceramic disk
4. C4 10μF, 16V electrolytic
5. C5, C6 22pF ceramic disk
6. C8, C9 1000μF, 50V electrolytic

Table 3: Capacitors

MISCELLANEOUS

S.No. Notation Compnent


1. X1 230V AC primary to
15V-0-15V, 500mA secondary
transformer
2. XTAL 12MHz crystal
3. S1 Push-to-on switch
4. S2 On/off switch
5. TX1 40kHz ultrasonic transmitter
6. RX1 40kHz ultrasonic receiver

Table 4: Miscellaneous

APPENDIX-B
PCB LAYOUT DESIGN
Figure 7: PCB layout (Top)

Figure 8: PCB layout (Bottom)


APPENDIX- C
RESISTANCE
The electrical resistance of a circuit component or device is defined
as the ratio of the voltage applied to the electric current which flows
through it:

If the resistance is constant over a considerable range of voltage, then


Ohm's law, I = V/R, can be used to predict the behavior of the
material. Although the definition above involves DC current and
voltage, the same definition holds for the AC application of resistors.

Whether or not a material obeys Ohm's law, its resistance can be described in
terms of its bulk resistivity. The resistivity, and thus the resistance, is
temperature dependent. Over sizable ranges of temperature, this temperature
dependence can be predicted from a temperature coefficient of resistance. \

RESISTIVITY AND CONDUCTIVITY

The electrical resistance of a wire would be expected to be greater for a longer


wire, less for a wire of larger cross sectional area, and would be expected to
depend upon the material out of which the wire is made. Experimentally, the
dependence upon these properties is a straightforward one for a wide range of
conditions, and the resistance of a wire can be expressed as
The factor in the resistance which takes into account the nature of the material is
the resistivity. Although it is temperature dependent, it can be used at a given
temperature to calculate the resistance of a wire of given geometry.

The inverse of resistivity is called conductivity. There are contexts where the
use of conductivity is more convenient.

Electrical conductivity = σ = 1/ρ

RESISTOR COMBINATIONS

The combination rules for any number of resistors in series or parallel can be
derived with the use of Ohm's Law, the voltage law, and the current law.

RESISTIVITY CALCULATION

The electrical resistance of a wire would be expected to be greater for a longer


wire, less for a wire of larger cross sectional area, and would be expected to
depend upon the material out of which the wire is made (resistivity).
Experimentally, the dependence upon these properties is a straightforward one
for a wide range of conditions, and the resistance of a wire can be expressed as
Resistance = resistivity x length/area
APPENDIX-D

CAPACITOR

A capacitor consists of two electrodes or plates, each of which stores an


opposite charge. These two plates are conductive and are separated by an
insulator or dielectric. The charge is stored at the surface of the plates, at the
boundary with the dielectric. Because each plate stores an equal but opposite
charge, the total charge in the capacitor is always zero.

Figure 9: Capacitor charge formation


When electric charge accumulates on the plates, an electric field is created in
the region between the plates that is proportional to the amount of accumulated
charge. This electric field creates a potential difference V = E·d between the
plates of this simple parallel-plate capacitor.

Figure 10: Capacitor Charge formation 2


The electrons in the molecules move or rotate the molecule toward the
positively charged left plate. This process creates an opposing electric field that
partially annuls the field created by the plates. (The air gap is shown for clarity;
in a real capacitor, the dielectric is in direct contact with the plates.)

Capacitance

The capacitor's capacitance (C) is a measure of the amount of charge (Q) stored
on each plate for a given potential difference or voltage (V) which appears

between the plates:

In SI units, a capacitor has a capacitance of one farad when one coulomb of


charge causes a potential difference of one volt across the plates. Since the farad
is a very large unit, values of capacitors are usually expressed in microfarads
(µF), nanofarads (nF) or picofarads (pF).

The capacitance is proportional to the surface area of the conducting plate and
inversely proportional to the distance between the plates. It is also proportional
to the permittivity of the dielectric (that is, non-conducting) substance that
separates the plates.

Stored energy

As opposite charges accumulate on the plates of a capacitor due to the


separation of charge, a voltage develops across the capacitor owing to the
electric field of these charges. Ever increasing work must be done against this
ever increasing electric field as more charge is separated. The energy (measured
in joules, in SI) stored in a capacitor is equal to the amount of work required to
establish the voltage across the capacitor, and therefore the electric field. The
energy stored is given by:
where V is the voltage across the capacitor.

In electric circuits

Circuits with DC sources

Electrons cannot directly pass across the dielectric from one plate of the
capacitor to the other. When there is a current through a capacitor, electrons
accumulate on one plate and electrons are removed from the other plate. This
process is commonly called 'charging' the capacitor even though the capacitor is
at all times electrically neutral. In fact, the current through the capacitor results
in the separation rather than the accumulation of electric charge. This separation
of charge causes an electric field to develop between the plates of the capacitor
giving rise to voltage across the plates. This voltage V is directly proportional to
the amount of charge separated Q. But Q is just the time integral of the current I
through the capacitor. This is expressed mathematically as:

where

I is the current flowing in the conventional direction, measured in


amperes
dV/dt is the time derivative of voltage, measured in volts / second.
C is the capacitance in farads

For circuits with a constant (DC) voltage source, the voltage across the
capacitor cannot exceed the voltage of the source. Thus, an equilibrium is
reached where the voltage across the capacitor is constant and the current
through the capacitor is zero. For this reason, it is commonly said that
capacitors block DC current.

Series or parallel arrangements

Capacitors in a parallel configuration each have the same potential difference


(voltage). To find their total equivalent capacitance (Ceq):

The current through capacitors in series stays the same, but the voltage across
each capacitor can be different. The sum of the potential differences (voltage) is
equal to the total voltage. To find their total capacitance:

One possible reason to connect capacitors in series is to increase the overall


voltage rating. In practice, a very large resistor might be connected across each
capacitor to divide the total voltage appropriately for the individual ratings.

Capacitor/inductor duality

In mathematical terms, the ideal capacitor can be considered as an inverse of the


ideal inductor, because the voltage-current equations of the two devices can be
transformed into one another by exchanging the voltage and current terms. Just
as two or more inductors can be magnetically coupled to make a transformer,
two or more charged conductors can be electrostatically coupled to make a
capacitor. The mutual capacitance of two conductors is defined as the current
that flows in one when the voltage across the other changes by unit voltage in
unit time.

Capacitor symbols

APPENDIX-E

DATASHEET -IC 89C2051


FEATURES
• Compatible with MCS-51 Products

• 2 Kbytes of Reprogrammable Flash Memory


Endurance: 1,000 Write/Erase Cycles
• 2.7 V to 6 V Operating Range
• Fully Static Operation: 0 Hz to 24 MHz
• Two-Level Program Memory Lock
• 128 x 8-Bit Internal RAM
• 15 Programmable I/O Lines
• Two 16-Bit Timer/Counters
• Six Interrupt Sources
• Programmable Serial UART Channel
• Direct LED Drive Outputs
• On-Chip Analog Comparator
• Low Power Idle and Power Down Modes

DESCRIPTION

The AT89C2051 is a low-voltage, high-performance CMOS 8-bit


microcomputer with 2 Kbytes of Flash programmable and erasable read only
memory (PEROM). The device is manufactured using Atmel’s high density
nonvolatile memory technology and is compatible with the industry standard
MCS-51 instruction set and pinout. By combining a versatile 8-bit CPU with
Flash on a monolithic chip, the Atmel AT89C2051 is a powerful microcomputer
which provides a highly flexible and cost effective solution to many embedded
control applications. The AT89C2051 provides the following standard features:
2 Kbytes of Flash, 128 bytes of RAM, 15 I/O lines, two 16-bit timer/counters, a
five vector two-level interrupt architecture, a full duplex serial port, a precision
analog comparator, on-chip oscillator and clock circuitry. In addition, the
AT89C2051 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
contents but freezes the oscillator disabling all other chip functions until the
next hardware reset.

PIN CONFIGURATION

Figure 11: IC 89C2051 Pinout Diagram


BLOCK DIAGRAM

Figure 12: IC 89C2051 Block Diagram

Pin Description
VCC
Supply voltage.

GND
Ground.

PORT 1
Port 1 is an 8-bit bidirectional I/O port. Port pins P1.2 to P1.7 provide internal
pullups. P1.0 and P1.1 require external pullups. P1.0 and P1.1 also serve as the
positive input (AIN0) and the negative input (AIN1), respectively, of the on-
chip precision analog comparator. The Port 1 output buffers can sink 20 mA and
can drive LED displays directly. When 1s are written to Port 1 pins, they can be
used as inputs. When pins P1.2 to P1.7 are used as inputs and are externally
pulled low, they will source current (IIL) because of the internal pullups. Port 1
also receives code data during Flash programming and program verification.
PORT 3
Port 3 pins P3.0 to P3.5, P3.7 are seven bidirectional I/O pins with internal
pullups. P3.6 is hard-wired as an input to the output of the on-chip comparator
and is not accessible as a general purpose I/O pin. The Port 3 output buffers can
sink 20 mA. When 1s are written to Port 3 pins they are pulled high by the
internal pullups and can be used as inputs. As inputs, Port 3 pins that are
externally being pulled low will source current (IIL) because of the pullups.
Port 3 also serves the functions of various special features of the AT89C2051 as
listed below:

Table 5: Port 3 Functions

Port 3 also receives some control signals for Flash programming and
programming verification.
RST
Reset input. All I/O pins are reset to 1s as soon as RST goes Hig h. Holding the
RST pin high for two machine cycles while the oscillator is running resets the
device. Each machine cycle takes 12 oscillator or clock cycles.
XTAL1
Input to the inverting oscillator amplifier and input to the internal clock
operating circuit.
XTAL2
Output from the inverting oscillator amplifier.
OSCILLATOR CHARACTERISTICS

XTAL1 and XTAL2 are the input and output, respectively, of an inverting
amplifier which can be configured for use as an on-chip oscillator, as shown in
Figure 1. Either a quartz crystal or ceramic resonator may be used. To drive the
device from an external clock source, XTAL2 should be left unconnected while
XTAL1 is driven as shown in Figure 2. There are no requirements on the duty
cycle of the external clock signal, since the input to the internal clocking
circuitry is through a divide by- two flip-flops, but minimum and maximum
voltage high and low time specifications must be observed. Notes: C1, C2 = 30
pF, 10 pF for Crystals= 40 pF, 10 pF for Ceramic Resonators

(a) (b)
Figure13: (a) Oscillator Connections,
(b) External Clock Drive Configuration

PROGRAM MEMORY LOCK BITS

On the chip are two lock bits which can be left unprogrammed (U) or can be
programmed (P) to obtain the additional features listed in the table:

Table 6: Program Memory Lock Bits


IDLE MODE
In idle mode, the CPU puts itself to sleep while all the on-chip peripherals
remain active. The mode is invoked by software. The content of the on-chip
RAM and all the special functions registers remain unchanged during this mode.
The idle mode can be terminated by any enabled interrupt or by a hardware
reset. P1.0 and P1.1 should be set to ’0’ if no external pull ups are used, or set to
’1’ if external pullups are used. It should be noted that when idle is terminated
by a hardware reset, the device normally resumes program execution, from
where it left off, up to two machine cycles before the internal reset algorithm
takes control. On-chip hardware inhibits access to internal RAM in this event,
but access to the port pins is not inhibited. To eliminate the possibility of an
unexpected write to a port pin when Idle is terminated by reset, the instruction
following the one that invokes Idle should not be one that writes to a port pin or
to external memory.

POWER DOWN MODE


In the power down mode the oscillator is stopped, and the instruction that
invokes power down is the last instruction executed. The on-chip RAM and
Special Function Registers retain their values until the power down mode is
terminated. The only exit from power down is a hardware reset. Reset redefines
the SFRs but does not change the on-chip RAM. The reset should not be
activated before VCC is restored to its normal operating level and must be held
active long enough to allow the oscillator to restart and stabilize. P1.0 and P1.1
should be set to ’0’ if no external pullups are used, or set to ’1’ if external
pullups are used.
PROGRAMMING THE FLASH
The AT89C2051 is shipped with the 2 Kbytes of on-chip PEROM code memory
array in the erased state (i.e., contents = FFH) and ready to be programmed. The
code memory array is programmed one byte at a time. Once the array
isprogrammed, to re-program any non-blank byte, the entire memory array
needs to be erased electrically.

INTERNAL ADDRESS COUNTER: The AT89C2051 contains an internal


PEROM address counter which is always reset to 000H on the rising edge of
RST and is advanced by applying a positive going pulse to pin XTAL1.

PROGRAMMING ALGORITHM:

To program the AT89C2051, the following sequence is recommended.

1. Power-up sequence: Apply power between VCC and GND pins Set RST and
XTAL1 to GND With all other pins floating, wait for greater than 10
milliseconds

2. Set pin RST to ’H’ Set pin P3.2 to ’H’

3. Apply the appropriate combination of ’H’ or ’L’ logic levels to pins P3.3,
P3.4, P3.5, P3.7 to select one of the programming operations shown in the
PEROM Programming Modes table. To Program and Verify the Array:

4. Apply data for Code byte at location 000H to P1.0 to P1.7.

5. Raise RST to 12V to enable programming.


6. Pulse P3.2 once to program a byte in the PEROM array or the lock bits. The
byte-write cycle is self-timed and typically takes 1.2 ms.

7. To verify the programmed data, lower RST from 12V to logic ’H’ level and
set pins P3.3 to P3.7 to the appropriate levels. Output data can be read at the
port P1 pins.
8. To program a byte at the next address location, pulse XTAL1 pin once to
advance the internal address counter. Apply new data to the port P1 pins.

9. Repeat steps 5 through 8, changing data and advancing the address counter
for the entire 2 Kbytes array or until the end of the object file is reached.

10. Power-off sequence: set XTAL1 to ’L’ set RST to ’L’ Float all other I/O
pins Turn Vcc power off

DATA POLLING: The AT89C2051 features Data Polling to indicate the end
of a write cycle. During a write cycle, an attempted read of the last byte written
will result in the complement of the written data on P1.7. Once the write cycle
has been completed, true data is valid on all outputs, and the next cycle may
begin. Data Polling may begin any time after a write cycle has been initiated.

READY/BUSY: The Progress of byte programming can also be monitored by


the RDY/BSY output signal. Pin P3.1 is pulled low after P3.2 goes High during
programming to indicate BUSY. P3.1 is pulled High again when programming
is done to indicate READY.

PROGRAM VERIFY: If lock bits LB1 and LB2 have not been programmed
code data can be read back via the data lines for verification:
1. Reset the internal address counter to 000H by bringing RST from ’L’ to ’H’.
2. Apply the appropriate control signals for Read Code data and read the output
data at the port P1 pins.
3. Pulse pin XTAL1 once to advance the internal address counter.
4. Read the next code data byte at the port P1 pins.
5. Repeat steps 3 and 4 until the entire array is read.
The lock bits cannot be verified directly. Verification of the lock bits is
achieved by observing that their features are enabled.

CHIP ERASE: The entire PEROM array (2 Kbytes) and the two Lock Bits are
erased electrically by using the proper combination of control signals and by
holding P3.2 low for 10 ms. The code array is written with all "1"s in the Chip
Erase operatio and must be executed before any non-blank memory byte can be
re-programmed.

READING THE SIGNATURE BYTES: The signature bytes are read by the
same procedure as a normal verification of locations 000H, 001H, and 002H,
except that P3.5 and P3.7 must be pulled to a logic low. The values returned are
as follows.(000H) = 1EH indicates manufactured by Atmel (001H) = 21H
indicates 89C2051
PROGRAMMING INTERFACE : Every code byte in the Flash array can be
written and the entire array can be erased by using the appropriate combination
of control signals. The write operation cycle is self-timed and once initiated,
will automatically time itself to completion.All major programming vendors
offer worldwide support for the Atmel microcontroller series.
FLASH PROGRAMMING MODES

Table 7: Flash Programming Modes

(a) (b)

Figure 14: (a) Programming the Flash Memory


(b)Verifying the Flash Memory
Table 8:Flash Programing and Verification

FLASH PROGRAMMING AND VERIFICATION WAVEFORMS

Figure 15: Flash Programming and Verification Waveforms


ABSOLUTE MAXIMUM POWER RATING

Table 8: Absolute Maximum Power Rating

DC CHARACTERSTICS

Table 9: DC Characterstics
EXTERNAL CLOCK DRIVE WAVEFORM

Figure 16: External Clock Drive Waveform

EXTERNAL CLOCK DRIVE

Table 9: External Clock Drive


REFERENCES
1) Electronics for you - September 1998.
2) www.electronicsforu.com
3) www.scribd.com/doc/7370615/Ultrasonic-Distance-Meter
4) www.atmel.com/atmel/acrobat/doc0368.pdf
5) Kenneth J. Ayala, The 8051 Microcontroller Architecture, Programming
& Applications, West Publishing Company, College & School Division,
1996.
6) Muhammad Ali Mazidi, Janice Gillispie Mazidi, The 8051
Microcontroller & Embedded Systems, Pearson Education.
7) Dhananjay V. Gadre, Programming and Customizing the AVR
Controller.
8) Mattiat. OE, The Ultrasonic Transducers Materials.
9) David A. Bell, Electronic Devices and Circuits, Oxford University Press,
2008.
10) Sensors & Transducers Journal, Vol. 95, Issue 8, August 2008, pp.
49-57
11) Alan Andrews, ABC’s of Ultrasonic, Arthur Barker Limited,
London, 1961.