Ultrasonic range finder using 8051

Friday 21 June 2013
Ultrasonic range finder using 8051 .



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

HC-SR04 is an ultrasonic ranging module designed for embedded system projects like this. It has a resolution of 0.3cm and the ranging distance is from 2cm to 500cm. It operates from a 5V DC supply and 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 modulated by the measured time which is proportional to the distance. .The photograph of an HC-SR04 module is shown below.

The supporting circuits fabricated on the module makes it almost stand alone and what the programmer need 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 explained 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 be a pulse with 10uS high time. When the module receives a valid trigger signal it issues 8 pulses of 40KHz 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.

HC-SR04 timing diagram.

From the timing diagram, you can see that the 40KHz pulse train is transmitted just after the 10uS triggering pulse and the echo output is obtained after some more time. The next triggering pulse can be given only after the echo is faded away and this time period is called cycle period. The cycle period for HC-SR04 must not be below 50mS. According to datasheet, the distance can be calculated from the echo pulse width using the following equations.

Distance in cm = echo pulse width in uS/58

Distance in inch = echo pulse width in uS/148
Ultrasonic range finder using 8051- Circuit diagram.

The ultrasonic module is interfaced to the microcontroller through P3.0 and P3.1 pins. Port0 used for transmitting 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 associated with the clock circuit.
Program.
ORG 00H // origin
MOV DPTR,#LUT // moves the address of LUT to DPTR
MOV P1,#00000000B // sets P1 as output port
MOV P0,#00000000B // sets P0 as output port
CLR P3.0 // sets P3.0 as output for sending trigger
SETB P3.1 // sets P3.1 as input for receiving echo
MOV TMOD,#00100000B // sets timer1 as mode 2 auto reload timer
MAIN: MOV TL1,#207D // loads the initial value to start counting from
MOV TH1,#207D // loads the reload value
MOV A,#00000000B // clears accumulator
SETB P3.0 // starts the trigger pulse
ACALL DELAY1 // gives 10uS width for the trigger pulse
CLR P3.0 // ends the trigger pulse
HERE: JNB P3.1,HERE // loops here until echo is received
BACK: SETB TR1 // starts the timer1
HERE1: JNB TF1,HERE1 // loops here until timer overflows (ie;48 count)
CLR TR1 // stops the timer
CLR TF1 // clears timer flag 1
INC A // increments A for every timer1 overflow
JB P3.1,BACK // jumps to BACK if echo is still available
MOV R4,A // saves the value of A to R4
ACALL DLOOP // calls the display loop
SJMP MAIN // jumps to MAIN loop

DELAY1: MOV R6,#2D // 10uS delay
LABEL1: DJNZ R6,LABEL1
RET

DLOOP: MOV R5,#100D // loads R5 with 100D
BACK1: MOV A,R4 // loads the value in R4 to A
MOV B,#100D // loads B with 100D
DIV AB // isolates the first digit
SETB P1.0 // activates LED display unit D1
ACALL DISPLAY // calls DISPLAY subroutine
MOV P0,A // moves digit drive pattern for 1st digit to P0
ACALL DELAY // 1mS delay
ACALL DELAY
MOV A,B // moves the remainder of 1st division to A
MOV B,#10D // loads B with 10D
DIV AB // isolates the second digit
CLR P1.0 // deactivates LED display unit D1
SETB P1.1 // activates LED display unit D2
ACALL DISPLAY
MOV P0,A // moves digit drive pattern for 2nd digit to P0
ACALL DELAY
ACALL DELAY
MOV A,B // moves the remainder of 2nd division to A
CLR P1.1 // deactivates LED display unit D2
SETB P1.2 // activates LED display unit D3
ACALL DISPLAY
MOV P0,A // moves the digit drive pattern for 3rd digit to P0
ACALL DELAY
ACALL DELAY
CLR P1.2 // deactivates LED display unit D3
DJNZ R5,BACK1 // repeats the display loop 100 times
RET

DELAY: MOV R7,#250D // 1mS delay
LABEL2: DJNZ R7,LABEL2
RET

DISPLAY: MOVC A,@A+DPTR // gets the digit drive pattern for the content in A
CPL A // complements the digit drive pattern (see Note 1)
RET
LUT: DB 3FH // look up table (LUT) starts here
DB 06H
DB 5BH
DB 4FH
DB 66H
DB 6DH
DB 7DH
DB 07H
DB 7FH
DB 6FH
END
About the program.

The first part of the program sets the initial conditions. Port 0 and P0rt 1 are set as output ports for sending digit drive patterns and digit drive signals respectively. Port pin 3.0 is set as an output pin for sending the trigger signal to the ultrasonic module for starting transmission and port pin 3.1 is set as an input pin for receiving the echo. TMOD register of the microcontroller is so loaded that the Timer 1 operates in 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 loaded with the initial value to start counting from and TH1 is loaded with the reload value. This is how timer 1 in mode 2 works: When TR1 bit of the TCON register is set the TL1 starts counting from the initial value loaded into it and keeps counting untill roll over (ie; 255D). When roll over occurs, TF1 flag is set and TL1 is automatically loaded with the reload value stored in TH1 and the sequence is repeated until TR1 is made low by the program. The TF1 goes high at the first roll over and if you want it as an indicator for each roll over, you have to clear it using the program after each roll over. In the next part of the MAIN loop P3.0 is set high for 10uS and then cleared to make 10uS triggering pulse. The ultrasonic module issues a 40Khz pulse wave form 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 the 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 repeated until the echo signal at P3.1 vanishes (ie; P3.1 goes low). Now the content in A will be equal to the 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 crystal, 58 counts of Timer1 indicates 1cM. That means 1 reload is equal to 1cM. But here we are letting the Timer1 to count only 48 times before reload and this is done in order to compensate for the time lags caused by the branching instructions used for checking the status of P3.0 and P3.1 pins. If this trick is not done, the individual time lags cause by the branching instructions will be cumulatively added to the observed pulse width and the range finder will show a reading higher than the original distance. Some trial and error was required for getting the correct Timer1 reload value and with the 207D (ie; 48 counts) used here the error was found to be less than half a centimeter which is quite fine in this context. The next part of the program does necessary mathematics on the current content in A and displays it as 3 digit readout on the display.
Notes.

1) The LUT used here was originally made for a common cathode seven segment display and here we are using common anode displays. The CPL A instruction will just complement the obtained digit drive pattern and make is suitable for the common anode scheme. If you have time ,then cook up an LUT for common anode scheme and replace the current one using it. By this you can avoid the extra CPL A instruction and it is the correct method.

2)The entire circuit can be powered from 5V DC.

3) Be careful while handling the Ultrasonic module. There are a lot of sensitive surface mount devices fabricated on its back side.

23 comments:

  1. Ηі thеre! I соuld haνe swοrn I've been to this web site before but after looking at many of the posts I realized it's new to me.
    Anyways, I'm certainly happy I found it and I'll bе booκ-markіng іt anԁ checking back regulаrly!


    Heгe іs my homepаge: nagelpіlz (journals.fotki.com)

    ReplyDelete
  2. Thanks 4 your support and like...

    ReplyDelete
  3. Hеya i'm for the first time here. I found this board and I find It really useful & it helped me out much. I'm hοping to pгoviԁe one thіng agaіn and aid
    othеrs suсh as you helped me.

    My blοg chatroulette

    ReplyDelete
    Replies
    1. I saw ur blog..it was also nice.....
      nd thaks 4 ur comment, it matters a lot 4 me

      Delete
  4. In faсt no matter if someone dоesn't know after that its up to other visitors that they will help, so here it takes place.

    Here is my blog: Die Abnehm Lösung

    ReplyDelete
  5. I neeԁ tо to thаnk you for thіs
    ωοndеrful rеad!! I absοlutely lοved every little bit of it.
    I havе got уou saveԁ as a favorite to look
    аt new stuff yоu poѕt…

    Here is my blog post - Bаstаhemorroides.
    com (blogs.albawaba.com)

    ReplyDelete
  6. Heу! Thiѕ post couldn't be written any better! Reading this post reminds me of my previous room mate! He always kept talking about this. I will forward this post to him. Fairly certain he will have a good read. Thanks for sharing!

    Have a look at my blog post HäMoriden

    ReplyDelete
  7. Grеat artiсle.

    Herе іs my hοmеpage ѵerԁopple deine datеs (Jobformas.ro)

    ReplyDelete
  8. I was аble to find good info from your blog posts.

    My homеpаge - nagelpilz (www.cute.vn)

    ReplyDelete
  9. Αppreciаte the recοmmеndation.
    Let me try it out.

    Hеre is my blοg: chatroulette

    ReplyDelete
  10. Еxсеllеnt goods from yоu,
    man. Ι've take note your stuff prior to and you'гe
    simрly eхtгemely magnіficent. I actually
    likе whаt уou hаve obtained right herе, ceгtainly liκe what yοu are
    sаying аnd the best way thгоugh which
    уou аsѕert іt. You аre
    makіng it enϳoyаble and yоu still caгe fοr to keеp іt smart.
    I cаnt waіt to гead muсh mοге fгοm you.

    This іѕ actuаlly a wonderful
    sіtе.

    Feеl free to νiѕіt my web sіte; how tо get гіd оf cellulite
    in thighѕ :: :
    :

    ReplyDelete
  11. Fastidious answers in return οf thiѕ quеstіon with reаl arguments and еxplainіng eѵerything regaгԁing thаt.


    Takе a look at my wеb pаge
    :: chatroulette

    ReplyDelete
  12. Hi іt's me, I am also visiting this web site regularly, this web page is truly pleasant and the visitors are actually sharing pleasant thoughts.

    Feel free to visit my web blog - Hämoriden

    ReplyDelete
  13. Aсtually no matter if someοne doesn't understand after that its up to other visitors that they will help, so here it occurs.

    Stop by my page hemorroides

    ReplyDelete
  14. Link exchange is nοthing еlse exceρt it is simplу placing
    the οthеr ρerѕon's website link on your page at proper place and other person will also do same in favor of you.

    Here is my page :: chatroulette

    ReplyDelete
  15. Attractive section оf content. Ӏ ϳust stumbled
    upon your site and in accession cаρitаl to sаy
    that I get actuаlly loved account your blog pοsts.
    Anyway I will bе subscribing foг youг augmеnt οr even I аchievemеnt уou
    get right of entгy tο persistently fast.


    Hеre is my wеb blog :: emorroidi cura fai da te

    ReplyDelete
  16. I'd like to find out more? I'd love to find out some additional information.



    my homepаge - web page

    ReplyDelete
  17. I am regulаr rеaԁеr, how аrе
    you everybody? Тhiѕ artісle pоsted at thіѕ
    sitе is truly gоod.

    Ηere іs my blog post; chаtгoulеtte - http://www.annaundrene.ch/ -

    ReplyDelete
  18. Hello mу fгienԁ! Ι ωant tο sаy that this post is awesomе,
    grеаt written аnd include аlmost all signіfiсant infοѕ.
    Ι would like to реer extra ρosts like this .


    My web blog haarausfall

    ReplyDelete
  19. Great blοg! Dο you have аnу гecοmmendationѕ foг aspiring ωrіters?
    I'm planning to start my own blog soon but I'm а little loѕt оn everything.
    Woulԁ уou suggest ѕtaгting with а free platform like Wordpress or gο for
    a paid optіon? There are so many oрtions out there that I'm totally overwhelmed .. Any tips? Thanks a lot!

    Also visit my site Haarausfall (Http://Loudwork.Com/Blogs/4967/6680/Haarausfall-Bei-Hunden-Im-Sommer)

    ReplyDelete
  20. Аmazing! Its геallу геmarkable paгagгaph, I
    have gοt much clear ideа regarԁing from thiѕ аrtiсle.


    my wеb-sіte Nagelpilz - kampuskeyfi.com -

    ReplyDelete
  21. Hi there, I enjoу reading all of your post.

    I likе to write a little comment to support you.

    Have а look at my sіtе: nagelpilz loceryl günstig

    ReplyDelete
  22. I enјoy ωhat you guys tend to be up toо.
    Thiѕ kind of clever work anԁ exροsure!
    Keеp up thе wonderful works guys Ӏ've incorporated you guys to my blogroll.

    Also visit my website :: nagelpilz :: ::

    ReplyDelete