top of page

Ultrasonic Range meter Module using PIC16F877A 

By Bernice Zuiya

Introduction

                   Ultrasonic Range Meter module is also one of the interesting module widely used in electronic applications. it is used to measure the distance by using the sound properties where its frequency is above the ability of human audition, basically they operate with high-frequencies. Here it consist of  triggering the TRIG Pin of the module and this will send an inaudible sound and wait to receive the echo if the sent sound cross any obstacle while travelling, then the distance is measured with the time made from the instance of time the sound have been sent to when the echo is received; then this become easy to find an accurate distance. 

WORKING :                          

 

    HC-SR04 Ultrasonic range meter module as explained in the introduction uses the sound properties to perform. the HC-SR04 has for pins which are:

  1. VDD

  2. TRIG

  3. ECHO

  4. GND   

                 The VDD Pin is connected to a +5 volt supply and GND to the common ground respectively while the two remaining pins TRIG and ECHO are interfacing with the input , output pins of the micro controller PIC in our case (16F877A) these pins work at TTL Level (0-5 volt); 

Now let illustrate how this works following the steps bellow :

  • the micro-controller will basically trig the TRIG pin of the module with a signal duration rounded to 10 micro-second. this means one pin from the micro-controller connected to the TRIG pin of the module should remains high ( 5 volt ) for a duration of 10 micro-second.

  • when the TRIG pin of the module is triggered then it start sending automatically the ultrasonic burst of 40 MHz as output, here we have to remember that this step it is not possible to hear the sound emitted by the module, not because the sound are very short but because they have a frequency which the human hears can't be to listen

  • the third step is when the ultrasonic burst sent cross a object within the range of capability of the module             (around 4 meters max ) it will return as an echo an the ECHO pin of the module will be high while receiving.

  • if the four above steps are completed then what is interesting here is finally to know the time taken to the ultrasonic burst while leaving the module up to when the module receive an echo. the time computed with the help of timer module in the micro-controller is  the most important.

Circuit Diagram

The circuit diagram shown below is the very basic diagram required for ultrasonic range meter using the 16F877A micro-controller, you need to respect the connections.

  • a resistor of value 4k7 ohm is connected in series with +5 volt and the reset pin (1st pin)

  • Q1 is the crystal oscillator in this case its frequency oscillation is 20 MHz.

  • supply: the circuit is connected with a voltage maximum equal 5 volt

  • R3 is a limiting current resistor to protect the LED.

  • C1 & C2 are of 22pF.

  • LCD display is used to display the output

  • HC-SR04 is the ultrasonic range meter module

Code and Comment

char txt_message[] = "Real Distance Cm";
char Warning_message[] = "- Out Of Range - ";
char txt0[7];
unsigned int i,j;
int t1;
   int Afficheur_ligne1(char Text_a_ecrire ,int Initial_position){
   //LCD_CMD(_LCD_CLEAR);
   Delay_ms(50);
   LCD_OUT(Initial_position,1,Text_a_ecrire);
   LCD_OUT(2,1,"                       ");
   }
  int Afficheur_ligne2 (char Text_a_ecrire ,int Initial_position){
  //LCD_CMD(_LCD_CLEAR);
   Delay_ms(50);
   lcd_out(1,1,txt_message);
   LCD_OUT(2,1,"Distance:" );
   LCD_OUT(2,Initial_position,Text_a_ecrire);
   }
   void main(void) {
    lcd_Init();
    LCD_CMD(_LCD_CURSOR_OFF);
    Delay_ms(50);
    TRISB.B7 = 0;
    TRISB.B6 = 1; 
    T1CON = 0X30;
       while(1)
            { 
               TMR1H = 0;  
               TMR1L = 0;   

               PORTB.F7 = 1; 
               Delay_us(10); 
               PORTB.F7 = 0;

                    while(!PORTB.F6); 
                          T1CON.F0 = 1;  
                          while(PORTB.F6);
                          T1CON.F0 = 0;
                          t1 = (TMR1L | (TMR1H<<8));  
                          t1 = t1/36.76;
                         if ( t1<=30){
                         ShortToStr(t1,txt0);
                        Afficheur_ligne2(txt0,10);
               }else{ Afficheur_ligne1(Warning_message,1);}
       Delay_ms(100);
    }
}

You will need to add the lines directive for LCD display, This code were written using mikroC compiler software, you can copy and compile it or modify it as well, ultrasonic range meter project. 

Download the full project here

bottom of page