Ultrasonic Range Finder Using 8051 - 4 Steps - Instructables

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ultrasonic Range Finder Using 8051

By ELECTROGINEER in CircuitsMicrocontrollers

Introduction: Ultrasonic Range Finder Using 8051


A simple ultrasonic range finder using 8051 microcontrollers is presented in this article. This ultrasonic
rangefinder can measure distances up to 2.5 meters at an accuracy of 1 centimeter. AT89s51 microco
ntroller and the ultrasonic transducer module HC-SR04 form the basis of this circuit. The ultrasonic mo
dule sends a signal to the object, then picks up its echo and outputs a waveform whose time period is
proportional to the distance. The microcontroller accepts this signal, performs necessary processing, a
nd displays the corresponding distance on the 3 digits seven-segment display. This circuit finds a lot of
application in projects like automotive parking sensors, obstacle warning systems, terrain monitoring r
obots, industrial distance measurements, etc.

Supplies
1. 8051 microcontroller

2. HC-SR04 ultrasonic module

3. 3 digits 7segment display

4. 2N2222 NPN BJT

5. 100ohm resistor

6. 8.2kohm resistor

6. 11.0592MHz crystal

7. 33pF capacitor

8. 10uF capacitor
Step 1: HC-SR04 Ultrasonic Module

HC-SR04 is an ultrasonic-ranging module designed for embedded system projects like this. It has a re
solution of 0.3cm and the ranging distance is from 2cm to 500cm. It operates from a 5V DC supply an
d the standby current is less than 2mA. The module transmits an ultrasonic signal, picks up its echo,
measures the time elapsed between the two events, and outputs a waveform whose high time is modu
lated by the measured time which is proportional to the distance. The photograph of an HC-SR04 mod
ule is shown in 1st photo.

The supporting circuits fabricated on the module make it almost stand alone and what the programmer
needs to do is to send a trigger signal to it for initiating transmission and receive the echo signal from it
for distance calculation. The HR-SR04 has four pins namely Vcc, Trigger, Echo, GND and they are ex
plained in detail below.

1) VCC: 5V DC supply voltage is connected to this pin.

2) Trigger: The trigger signal for starting the transmission is given to this pin. The trigger signal must b
e a pulse with 10uS high time. When the module receives a valid trigger signal it issues 8 pulses of 40
KHz ultrasonic sound from the transmitter. The echo of this sound is picked by the receiver.

3)Echo: At this pin, the module outputs a waveform with high time proportional to the distance.

4) GND: Ground is connected to this pin.

From the timing diagram, you can see that the 40KHz pulse train is transmitted just after the 10uS trig
gering pulse and the echo output is obtained after some more time. The next triggering pulse can be gi
ven only after the echo is faded away and this time period is called the cycle period. The cycle period f
or HC-SR04 must not be below 50mS. According to the datasheet, the distance can be calculated fro
m the echo pulse width using the following equations.
Distance in cm = echo pulse width in uS/58
Step 2: CIRCUIT DIAGRAM

The ultrasonic module is interfaced to the microcontroller through P3.0 and P3.1 pins. Port0 used for tr
ansmitting the 8-bit display data to the display and port pins P1.0, P1.1, P1.2 are used for transmitting
display drive signals for the corresponding display units D1, D2, D3. Push-button switch S1, capacitor
C3 and resistor R9 forms a de-bouncing reset circuitry. Capacitors C1, C2, and crystal X1 are associat
ed with the clock circuit.
Step 3: Code
The first part of the program sets the initial conditions. Port 0 and Port 1 are set as output ports for sen
ding digit drive patterns and digit drive signals respectively. Port pin 3.0 is set as an output pin for send
ing the trigger signal to the ultrasonic module for starting transmission and port pin 3.1 is set as an inp
ut pin for receiving the echo. TMOD register of the microcontroller is so loaded that Timer 1 operates i
n mode2 8 bit auto-reload mode. Timer 0 of the microcontroller is not used here. In the next part of the
program (loop MAIN), the TL1 and TH1 registers of Timer1 are loaded with the initial values. TL1 is loa
ded with the initial value to start counting from and TH1 is loaded with the reload value. This is how tim
er 1 in mode 2 works: When the TR1 bit of the TCON register is set the TL1 starts counting from the in
itial value loaded into it and keeps counting until rollover (ie; 255D). When a rollover occurs, the TF1 fl
ag is set and TL1 is automatically loaded with the reload value stored in TH1 and the sequence is repe
ated until TR1 is made low by the program. The TF1 goes high at the first rollover and if you want it as
an indicator for each rollover, you have to clear it using the program after each rollover. In the next part
of the MAIN loop, P3.0 is set high for 10uS and then cleared to make a 10uS triggering pulse. The ultr
asonic module issues a 40Khz pulse waveform after receiving this trigger and the program waits until
a valid echo is received at P3.1. The pulse width of the echo signal is proportional to the distance to th
e obstacle and so the next job of the program is to measure the pulse width. Whenever there is a valid
echo pulse at P3.1, the Timer1 starts and it counts from the initial value to 255 ie: 255-207= 48 counts.
Then the counter restarts and accumulator increments by one for every restart. This sequence is repe
ated until the echo signal at P3.1 vanishes (ie; P3.1 goes low). Now the content in A will be equal to th
e number of Timer1 reloads which is in fact proportional to the distance. From the datasheet, it is clear
that 58uS echo pulse width indicates 1cM distance. When the processor is clocked by a 12MHz crysta
l, 58 counts of Timer1 indicate 1cM. That means 1 reload is equal to 1cM. But here we are letting the T
imer1 count only 48 times before reload and this is done in order to compensate for the time lags caus
ed by the branching instructions used for checking the status of P3.0 and P3.1 pins. If this trick is not d
one, the individual time lags caused by the branching instructions will be cumulatively added to the ob
served pulse width and the range finder will show a reading higher than the original distance. Some tri
al and error were required for getting the correct Timer1 to reload value and with the 207D (ie; 48 coun
ts) used here the error was found to be less than half a centimeter which is quite fine in this context. T
he next part of the program does necessary mathematics on the current content in A and displays it as
3 digit readout on the display.

the hex file for the microcontroller is provided below or u can generate it using the Keil software step a
re given in pdf

Attachments

Step 4: Library Files and Other Files


Ultrasonic library file to download click here

(copy library files to

C:\program files (x86)\labcenter electronics\proteus professional\LIBRARY)

the ultrasonic hex file for simulation to download click here

Proteus simulation files click here

You might also like