Sunday 12 July 2015

Ultrasonic Distance Sensor HC-SR04 Interfacing with 8051

          In this tutorial you will learn how to interface Ultrasonic Distance Measurement Sensor with AT89S52. Ultrasonic ranging module HC - SR04 provides 2cm - 400cm non-contact measurement function.the ranging accuracy can reach to 3mm. The modules includes ultrasonic transmitters, receiver and control circuit.I also Provide you complete Project code you can download it from the bottom of the page.

Now working of HC-SR04 as follow:

  1. Send 10us HIGH pulse on TRIG pin of HC-SR04.
  2. The sensor sends out a “sonic burst” of 8 cycles.and detect whether there is a pulse signal back.If there is an obstacle in-front of the module, it will reflect the ultrasonic burst.
  3. If the signal is back, ECHO output of the sensor will be in HIGH state (5V) for a duration of time taken for sending and receiving ultrasonic burst. Pulse width ranges from about 150μS to 25mS and if no obstacle is detected, the echo pulse width will be about 38ms.





Pinout of HC-SR04:




HC-SR04
HC-SR04 Front view



  • VCC –   5V Power supply.
  • TRIG –  Trigger Pin connect to the P1.1 of AT89S52.
  • ECHO – Echo Pin connect to the P1.0 of AT89S52.
  • GND –   -0V  Ground.


Timing Diagram of HC-SR04:


Timing Diagram of HC-SR04



Circuit Diagram:




Programming Steps:


  1. Provide TRIGGER to Ultrasonic module HC-SR04.
  2. Listen for Echo.
  3. Start Timer when ECHO HIGH is received.
  4. Stop Timer when ECHO goes LOW.
  5. Read Timer Value.
  6. Convert it to Distance.

        Here Trigger pin of HC-SR04 is connected P1.1 of AT89S55 which is output pin.Echo pin is connected to P1.0 of AT89S52 which is Input. 

sbit Echo = P1^0;

sbit Trig  = P1^1;

Timer 0 is used in 16-bits mode for measuring time.
TMOD |= 0x01;//Timer 0 in 16-bit mode.

Now Send 10us HIGH pulse on TRIG pin of HC-SR04.

Trig = 1;

delay_us(10);

Trig = 0;


Wait for rising edge at echo pin of HC-SR04.
while(Echo == 0);

Start Timer 0 and clear timer count register.

TR0=1;

TL0=TH0=0;



Now wait for falling edge at Echo pin and stop timer and Calculate time.

while(Echo == 1)
{
if(TF0 == 1)//timer over if no obstacle is detected
break;
}
TR0=0;
TF0 = 0;
//Calculate number of count
Count = TL0 + TH0*256;
//Calculate total time in uS.
Time = Count*1.085;
//As per datasheet of HC-SR04 Distance is in Centimeter.
Distance = Time/58;

//The distance is then sent using serial communications.


Download:


You can download entire project files here.



31 comments:

  1. Nice tutorial..and also post some Protocols, because its very much for begineers, including me.

    ReplyDelete
  2. Thank you bro ..for this openelectronics blog ....and for share this knowledge also...

    ReplyDelete
  3. according to this codding part what is the code for glow a led when ultrasonic interface an object?

    ReplyDelete
  4. @Rhythm Kr. Dasgupta

    Can you tell me maximum distance of the robot from the object that you want to detect?.

    As ex. If Max. distance is 10 cm than add small code in main function

    if(Distance<=10)
    {
    //LED ON
    }
    else
    {
    //LED OFF
    }

    ReplyDelete
    Replies
    1. plz share all programe of this project

      Delete
    2. i cant understand where insert this function. would u help me give me the total codding part to glow a led ????

      Delete
  5. plz share complete programme of this project

    ReplyDelete
    Replies
    1. You can download complete project from link given at bottom of this post

      Here i also give you link you can download from here.

      http://www.mediafire.com/download/55mpyr839kc8yc6/HC_SR04_Ultrasonic_Sensor.rar

      Delete
    2. Hello nice tutorial
      While going through the code, I saw this line of code:


      Serial_Init();
      TX_String("Silicon TechnoLabs\r\n");
      TX_String("Distance Measurement using Ultrasonics Sensor HC-SR04\r\n");

      Please how can I integrate the LCD and then display this strings on the LCD
      Thanks
      My email is joshuachinemezu@gmail.com

      Delete
  6. i cant understand where insert this function.
    ****
    if(Distance<=10)
    {
    //LED ON
    }
    else
    {
    //LED OFF
    }


    would u help me give me the total codding part to glow a led ????

    ReplyDelete
  7. #include
    #include "Serial.h"
    sbit Echo = P1^0;
    sbit Trig = P1^1;
    LED = P1^2;
    void delay_us(unsigned int us)//This function provide delay in us uS.
    {
    while(us--);
    }
    void delay_ms(unsigned int ms)//This function Provide delay in ms Ms.
    {
    unsigned int i,j;
    for(i=0;i<ms;i++)
    for(j=0;j<127;j++);
    }
    void main()
    {
    unsigned int Count,Time,Distance;
    Echo = Trig = 0;
    TMOD |= 0x01;//Timer 0 in 16-bit mode
    Serial_Init();
    TX_String("Silicon TechnoLabs\r\n");
    TX_String("Distance Measurement using Ultrasonics Sensor HC-SR04\r\n");
    do{
    Trig = 1;//Send 10us start pulse to HC-SR04 as per datasheet of HC-SR04
    delay_us(10);//~10us delay
    Trig = 0;
    while(Echo == 0);//Wait for Rising edge at Echo pin
    TR0=1;//Start Timer
    TL0=TH0=0;//Clear timer count register
    while(Echo == 1)//Wait for Falling edge at Echo pin
    {
    if(TF0 == 1)//timer over if no obstacle is detected
    break;
    }
    TR0=0;//Stop Timer.
    TF0 = 0;//clear Timer Over Flow Flag
    Count = TL0 + TH0*256;//Calculate number of count
    Time = Count*1.085;//Calculate total time in uS.
    Distance = Time/58;//As per datasheet of HC-SR04 Distance is in Centimeter
    TX_String("--------------\r\n");
    TX_String("Distance:-");
    TX_Int(Distance);//Send distance to serial
    TX_String("\r\n");
    if(Distance<=10)
    {
    LED = 1;//LED ON
    }
    else
    {
    LED = 0;//LED OFF
    }
    delay_ms(2000);

    }while(1);
    }

    ReplyDelete
  8. Replies
    1. its gives an syntax error :

      compiling HC_SR04.c...
      HC_SR04.c(64): error C141: syntax error near 'delay_ms', expected 'while'
      HC_SR04.c(65): error C141: syntax error near ''
      Target not created.
      Build Time Elapsed: 00:00:00

      Batch-Build summary: 0 succeeded, 1 failed, 0 skipped - Time Elapsed: 00:00:01

      Delete
  9. its gives an syntax error :

    compiling HC_SR04.c...
    HC_SR04.c(64): error C141: syntax error near 'delay_ms', expected 'while'
    HC_SR04.c(65): error C141: syntax error near ''
    Target not created.
    Build Time Elapsed: 00:00:00

    Batch-Build summary: 0 succeeded, 1 failed, 0 skipped - Time Elapsed: 00:00:0

    ReplyDelete
  10. hi,this post is quite useful.Please help me with this. I am working on obstacle masurement and i have to connect 4 hc sr04 ultrasonic range finder with Microcontroller At89S52. please provie the circuit diagram for it. Please provide as soon as possible.
    email me at: richapatel053@gmail.com

    ReplyDelete
    Replies
    1. If you want to interface four sensor than you can not able to read all the sensors at same time.

      Delete
  11. please , can you help me to explain why the project dose not operate when i load hex file to the microcontroller , only appear ( range finder )on lcd , and not display the distance
    email me at : zayd_hussam@yahoo.com

    ReplyDelete
    Replies
    1. Have you integrate code for lcd?
      If yes than show me your code.

      Delete
    2. i using the code . that i download from the link
      http://www.mediafire.com/download/55mpyr839kc8yc6/HC_SR04_Ultrasonic_Sensor.rar

      Delete
    3. Dear zaid, this code doesn't contain LCD interfacing.

      Delete
  12. This comment has been removed by the author.

    ReplyDelete
  13. hello,
    how do you find '1.085' and why do you do 'TH0*256'?

    //Calculate number of count
    Count = TL0 + TH0*256;
    //Calculate total time in uS.
    Time = Count*1.085;

    Which clock do you use for Timer0 ? ( you don't use CKCON here, why?)
    Thanks !

    ReplyDelete
    Replies
    1. 1.085 is time in microsecond taken by timer 0 for 1 count.

      (TH0*256) - Because timer 0 in 16-bit mode there value store in 2 separate 8-bit register so for calculate total time you need to multiply higher 8-bit with 256 + lower 8-bit.

      Timer 0 clock is 921.6 KHz

      Delete
  14. good work unfortunately i also did the same project with different approach

    ReplyDelete
    Replies
    1. Dear suryateja polina,

      You can also share your idea. :)

      Delete
  15. Most of the time I don’t make comments on any post, but I'd like to say that this content is really helpful. Please share with us more post like this.
    Ultrasonic Level Sensor

    ReplyDelete
    Replies
    1. Thank you very much Deba Sheesh. We'll share some of our work here stay tuned.

      Delete
  16. Nice post,In modern applications, ultrasonic sensors are described by their dependability and extraordinary flexibility. Ultrasonic sensors can be utilized to unravel even the most complex errands including object discovery or level estimation with millimeter accuracy, in light of the fact that their measuring strategy works dependably under all conditions.see more:-www.seeautomation.com

    ReplyDelete
  17. Looking at been given within your blog still putting solution obviously only a bit small submits. Enjoyable technique for future forthcoming, We've been book-marking at a stretch protect variations give up soars together with each other. UKdistance

    ReplyDelete