Ultrasonic Module HC-SR04 interfacing with PIC18F4550

Ultrasonic Sensor HC-SR04

This is the picture of Ultrasonic Module HC-SR04
Ultrasonic HC-SR04 Module


Ultrasonic Module HC-SR04 works on the principle of SONAR and RADAR system.

  • The HC-SR04 module has an ultrasonic transmitter, receiver, and control circuit on a single board.
  • The module has only 4 pins, Vcc, Gnd, Trig, and Echo.
  • When a pulse of 10µsec or more is given to the Trig pin, 8 pulses of 40 kHz are generated. After this, the Echo pin is made high by the control circuit in the module.
  • The echo pin remains high till it gets an echo signal of the transmitted pulses back.
  • The time for which the echo pin remains high, i.e. the width of the Echo pin gives the time taken for generated ultrasonic sound to travel towards the object and return.
  • Using this time and the speed of sound in air, we can find the distance of the object using a simple formula for distance using speed and time.

For more information about ultrasonic module HC-SR04 and how to use it, refer to the topic Ultrasonic Module HC-SR04 in the sensors and modules section.


Connection Diagram of Ultrasonic Sensor to PIC18F4550

This is the picture of Ultrasonic Module Interfacing with PIC18f4550
Ultrasonic Module HC-SR04 Interfacing with PIC18F4550  


Measure the Distance using HC-SR04 and PIC18F4550

Here let’s design an application in which we will find a distance to an object by interfacing ultrasonic module HC-SR04 with PIC18F4550 and display the distance on a 16x2 LCD.

Steps of Programming

  1. PIC18F4550 microcontroller needs to transmit at least 10 us trigger pulse to the HC-SR04 Trig Pin.
  2. After getting a trigger pulse, HC-SR04 automatically sends eight 40 kHz sound waves and waits for rising edge output at the Echo pin.
  3. When the rising edge capture occurs at the Echo pin which is connected to the input of PIC18F4550, start Timer of PIC18F4550 and again wait for the falling edge on the Echo pin.
  4. As soon as the falling edge is captured at the Echo pin, the microcontroller reads the count of the Timer. This time count is used to calculate the distance to an object.

Calculation (distance in cm)

Sound velocity = 343 m/s = 34300 cm/s 

Distance\,of\,Object(in\,cm) = \frac{Sound\,Velocity \ast Timer}{2}

                                                                = (34300 * TIMER) / 2

                                                                = 17150 * (TIMER) 

Now, if we selected 8 MHz oscillator frequency for PIC18F4550, timer frequency in PIC18F4550 will be 2 MHz. So time to execute 1 instruction is 0.5 us.

So timer gets incremented after 0.5 us time elapse.

                        = 17150 * (TIMER value) x 0.5 x 10^-6 cm

                        = 0.5 * (TIMER value)/58.30 cm

                        =0.5 * (TIMER value) / 58.30 cm


                        = (TIMER value) / 117 cm


Ultrasonic Sensor HC-SR04 Code for PIC18F4550 

  Interfacing Ultrasonic module HC-SR04 with PIC18F4550
  for finding distance to an object


#include <xc.h>
#include <pic18f4550.h>
#include <stdio.h>
#include "Configuration_Header_File.h"
#include "16x2_LCD_4bit_File.h"

void Trigger_Pulse_10us();

#define _XTAL_FREQ 8000000	/* Define freq */
#define Trigger_Pulse LATD0	/* Define Trig pin of HC-SR04 */

void main()
    float Distance;
    int Time;
    float Total_distance[10];
    OSCCON=0x72;		/* Use internal oscillator frequency */
    TRISB = 0xff;		/* Make PORTB as Input port*/
    TRISD = 0;			/* Make PORTD as Output port*/
    INTCON2bits.RBPU=0;		/* Enable PORTB Pull-ups */
    Trigger_Pulse = 0;
/* Enable 16-bit TMR1 Register,No prescale, internal clock, Timer OFF */    
    T1CON = 0x80;
    TMR1IF = 0;			/* Make Timer1 Overflow Flag to '0' */
    TMR1=0;			/* Load Timer1 with 0 */
    LCD_String_xy(1,1," Distance:");
    	Trigger_Pulse_10us();	/* Transmit 10us pulse to HC-SR04 */
    	while(PORTBbits.RB0==0);	/* Wait for rising edge at Echo pin */
    	TMR1=0;			/* Load Timer1 register with 0 */
    	TMR1ON=1;			/* Turn ON Timer1*/
    	while(PORTBbits.RB0==1 && !TMR1IF);/* Wait for falling edge */
    	Time = TMR1;		/* Copy Time when echo is received */
    	TMR1ON=0;			/* Turn OFF Timer1 */
    	Distance = ((float)Time/117.00);/* Distance =(velocity x Time)/2 */
    	LCD_String("  cm");

void Trigger_Pulse_10us()
    Trigger_Pulse = 1;
    Trigger_Pulse = 0;


Video of Distance Measurement using Ultrasonic HC-SR04 Sensor

Components Used

Ultrasonic Module HC-SR04
Ultrasonic module HC-SR04 is generally used for finding distance value and obstacle detection. It can operate in the range 2cm-400cm.
LCD16x2 Display
LCD16x2 Display


HCSR04 Ultrasonic Interfacing PIC18F4550 Project File Download