Ultrasonic sensor interfacing with pic microcontroller : distance measurement




Ultrasonic sensor interfacing with pic microcontroller and how to measure distance with HC-SR04 ultrasonic sensor ? Hi everyone, I hope every one of you are fine and doing well. In today’s tutorial, I am come up with another interesting sensor interfacing with pic microcontroller that is HC-SR04 ultrasonic sensor. It is very useful sensor for embedded systems projects. Before I start this tutorial, I recommend you to read about ultrasonic sensors and its working. In this tutorial I am using HC-SR04 ultrasonic sensor which will be used to measure distance. PIC16F887 microcontroller will be used for this project. 16X2 LCD is used to display measured value of distance on LCD.  So let’s start with introduction of HC-SR04 ultrasonic sensor and how it can be used to measure distance. ultrasonic sensor interfacing with pic microcontroller




HC-SR04 ultrasonic sensor

This is a contactless measurement sensor. It can measure distance up  to 2cm – 400cm.  Its accuracy is about three mili meter which is very efficient in water level and depth measurement applications.  HC-SR04 ultrasonic sensor is consists of three modules. Description of each module is given below:HC-SR04 ultrasonic sensor

  • Transmitter module:  This module sends eight 40KHZ pulses and detects if there is any signal back with the help receiver pin.
  • Receiver module: It is basically a trigger pin on HC-SR04 ultrasonic sensor which detects a pulse of at least 10 micro second. So to measure distance with HC-SR04 ultrasonic sensor  and pic microcontroller, we need to send trigger signal from pic microcontroller which should be high for at least 1o micro seconds. It will explain it in more details in later part of this tutorial .
  • If there is any obstacle in front of electronics 8 electronics signals, it will go back to HC-SR04 ultrasonic sensor.
  • When signal reaches back, Echo pin of ultrasonic sensors generates a pulse which is equal to total time taken by electronics burst from sending and receiving back.  pulse width will be between 150us to 25ms. So we need to measure this pulse time to measure distance. I will explain it later.

Distance can be calculated by using following formula:




distance = ( total time taken * velocity of sound ) / ( 2 ) ;

distance = ( total time taken * 34000(m/s)) / ( 2 ) ;

So we need to measure time of output pulse to measure distance with the help of HC-SR04 ultrasonic sensor. Pin out of ultrasonic sensor is given below:timing diagram of ultrasonic sensor HC-SR04

so according to above diagram, we will send trigger pulse of at least 10 micro seconds to sensor and sensor will generate a electronics burst. This electronics burst come back after colliding with obstacle.  Echo pin will produce a pulse of time taken by electronics burst to send and receive back. To measure distance we need to measure time of this pulse. After measuring time, we can easily calculate distance with the help of above given formula.

Ultrasonic sensor HC-SR04 interfacing with pic microcontroller

To measure time you need to use timers of pic microcontroller, I have already posted a article on how to use timers of pic microcontroller, you should read this article to know how to measure time using timers of pic microcontroller and you can also check this article on how to measure frequency using pic microcontroller which will also help you to get understanding of how to measure time of pulse.  you can also use CCP module of pic microcontroller to measure pulse width.  you can also check this article LCD interfacing with pic microcontroller , if you do not know how to interface LCD with pic microcontroller.  Circuit diagram of Ultrasonic sensor HC-SR04 interfacing with pic microcontroller is given below:

Ultrasonic sensor HC-SR04 interfacing with pic microcontroller

Video results for distance measurement

Code of Ultrasonic sensor HC-SR04 interfacing with pic microcontroller

Code is written using Mikro c for pic and it will work perfectly with pic16f877a and pic16f887 microcontroller. Crystal oscillator frequency is 8HMz.

// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD6_bit;
sbit LCD_D4 at RD5_bit;
sbit LCD_D5 at RD4_bit;
sbit LCD_D6 at RD3_bit;
sbit LCD_D7 at RD2_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD6_bit;
sbit LCD_D4_Direction at TRISD5_bit;
sbit LCD_D5_Direction at TRISD4_bit;
sbit LCD_D6_Direction at TRISD3_bit;
sbit LCD_D7_Direction at TRISD2_bit;
// End LCD module connections

void main() 
{
 int a;
 char txt[7];
 Lcd_Init();
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 TRISB = 0b00010000;
 TRISD=0X00;
 PORTD.B1=0;
 Lcd_Out(1,1,"Developed By");
 Lcd_Out(2,1,"electroSome");
 
 Delay_ms(3000);
 Lcd_Cmd(_LCD_CLEAR);
 
 T1CON = 0x10;
 
 while(1)
 {
 TMR1H = 0;
 TMR1L = 0;
 
 PORTB.F0 = 1;
 Delay_us(10);
 PORTB.F0 = 0;
 
 while(!PORTB.F4);
 T1CON.F0 = 1;
 while(PORTB.F4);
 T1CON.F0 = 0;

a = (TMR1L | (TMR1H<<8))/58.82 - 10;
 
 if(a>=0 && a<=400)
 {
 IntToStr(a,txt);
 Ltrim(txt);
 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Out(1,1,"Distance = ");
 Lcd_Out(1,12,txt);
 Lcd_Out(1,15,"cm");
 }
 /* else
 {
 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Out(1,1,"Out of Range");
 } */
 Delay_ms(500);
 }
}

timer zero of pic16f877A microcontroller is used to measure output pulse width of ultrasonic sensor.  Rest of the code is self explanatory but if you have any issue, let me know with your comments.



12 Comments

  1. eslam June 9, 2017
    • Bilal Malik June 9, 2017
      • eslam June 9, 2017
        • Bilal Malik June 9, 2017
  2. eslam June 9, 2017
    • Bilal Malik June 9, 2017
      • eslam June 9, 2017
      • eslam June 9, 2017
  3. eslam June 10, 2017
  4. surya teja August 8, 2017
  5. ara September 16, 2017

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,376 other subscribers