Ultrasonic distance meter using HC-SR04 and 8051

ultrasonic-range-detector-project

Ultrasonic waves are of great use and holds key in fields such as automation, marine and so.One of the familiar and trusted application of Ultrasonic waves are range and distance detection, you might have seen some of these in robots to detect obstacles and in automated factories.Similarly we are going to build Ultrasonic distance meter to measure the distance between the module and an object, this will be extremely useful for robotic applications.

SCHEMATIC DESIGN OF ULTRASONIC DISTANCE METER:

ultrasonic-interface-8051-microcontroller

PARTS REQUIRED:

Here is a list of parts required to build this project.We have added direct links to buy these parts in the below table.Since we are an affiliate marketing partner of "Amazon.in" we will get a referral fee for every purchase you make, which would make you a contributor for our development.

PART NAME COUNT LINK TO BUY
AT89S52 1 Buy here
HC-SR04 1 Buy here
LCD 1 Buy here
Connecting Wires As Required
Resistors As Required
Capacitors As Required

HC-SR04 ULTRASONIC MODULE:

hc-sr04-ultrasonic-module

188bet服务官网HC-SR04 is an  ultrasonic ranging module designed for embedded projects like this.It has a resolution of 0.3cm and the ranging distance is from 2cm to 400cm.This 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.

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

2)Trigger: The trigger signal is to start the transmission of waves which 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.

timing-diagram-ultrasonic-module

This timing diagram explains the way this module measures the distance.A pulse trigger input to the module will enable it to produce a burst of 8 sonic pulse of 40Khz.When the pulse hits the obstacle it reflects the ultrasonic waves as echo which is sensed by the echo pin of the module.The echo signal can be obtained from the Echo pin of the Ultrasonic module.The length of the of echo pulse is proportional to the distance of the object/obstacle.Refer the"Data Sheet"for more information about this module.

USING 8051 TO MEASURE ECHO PULSE:

Now we are clear about the module pins and its operation let's see how to make this work with 8051 to measure distance.The measurement of Echo pulse can be done with the help of external Interrupt and Timer modules of AT89S52 Microcontroller.In capture mode when external pulse is fed to the interrupt the timer starts counting and will stop only after the state of pulse is changed.Steps to follow to program controller as a capture device.

  1. Initialize Timer0 module as 16 bit timer with Gate bit as 1.
  2. Making Gate bit as 1 will transfer the control of timer to external interrupt pin INT0.
  3. When there is an interrupt pulse timer 0 starts counting and stops as soon as the interrupt disappears.
  4. The value in the Timer 0 (TH0 and TL0) gives the time period or length of the pulse.

OPERATION:

  1. Connect the Trigger pin of the HC-SR04 module to any I/O pins of 8051 controller.
  2. Connect the Echo pin of the module to interrupt pin INT0 of the Microcontroller.
  3. Send a pulse of minimal timer period 10us, this will make the Ultrasonic module to send burst of data.
  4. The reflected waves will be sensed by the module and it exhibits the output in Echo pin logic high or 1 to INT0 pin of Microcontroller.
  5. This creates an interrupt and Timer 0 will start counting.
  6. When the pulse from echo pin alters its state to logic 0 or low the timer 0 stops counting
  7. The length of pulse from echo pin is proportional to the distance at which the object is located.
  8. The value in the Timer 0 gives the distance of course with some simple calculations.

CALCULATION:

According to the data sheet the distance can be given by the formula :

Distance in cm = Timer 0/58

Distance in inch = Timer 0/148

CODE:

#include
         
          #define port P2  #define dataport P0  int cms!sbit trig=P3^5!sbit rs=port^0!sbit rw=port^1!sbit e=port^2!void delay(unsigned int msec)  {    int i,j!for(i=0;i
          
           

WORKING VIDEO:

NOTE:

  • You can use this method to any robotic path guidance application.
  • You can get more range by replacing the sensor module we used above.

27 Comments

  1. Atif ali

    Lcd display just start and how to measure distance plz give me solution of that problem

    Reply
  2. Atif ali

    How to display distance by ultrasonic sensor and they just display start in lcd ?Give me solution of that problem.

    Reply
  3. Ahmer

    plz help me if any one run this program because when i gave the power it automatically stuck at M letter without show another thing if anyone have the write code plz reply

    Reply
  4. bharat

    is this possible to interface two ultrasonic sensors with p89v51rd2 microcontroller???

    Reply
    1. Frank Donald Frank Donald

      Yes it is possible to connect two Ultrasonic sensors to it.

      Reply

Leave a Comment

Your email address will not be published.Required fields are marked*

Baidu
map