Pantech.AI

How to Interface Ultrasonic Module HC-SR04 with PIC18F4550

Ultrasonic Sensor HC-SR04

Ultrasonic HC-SR04 Module

The HC-SR04 Ultrasonic Module operates on the principles of SONAR and RADAR systems.

This module combines an ultrasonic transmitter, receiver, and control circuit on a single board. It has four pins: Vcc, Gnd, Trig, and Echo.

When a pulse of 10µs or longer is applied to the Trig pin, the module generates 8 pulses at 40 kHz. After this, the control circuit activates the Echo pin, which stays high until it receives the echo signal from the transmitted pulses. The duration for which the Echo pin remains high represents the time taken for the ultrasonic sound to travel to the object and return.

By measuring this time and knowing the speed of sound in air, we can calculate the distance to the object using the basic formula of distance = speed × time.

For more information on the HC-SR04 Ultrasonic Module and how to use it, refer to the “Ultrasonic Module HC-SR04” section in the Sensors and Modules guide.

Connection Diagram of Ultrasonic Sensor to PIC18F4550

Ultrasonic Module HC-SR04 Interfacing with PIC18F4550

Measure the Distance using HC-SR04 and PIC18F4550

In this application, we will calculate the distance to an object by interfacing the HC-SR04 Ultrasonic Module with the PIC18F4550 and displaying the distance on a 16×2 LCD.

Steps for Programming:

  1. The PIC18F4550 microcontroller sends a trigger pulse of at least 10 µs to the HC-SR04’s Trig pin.
  2. Upon receiving the trigger pulse, the HC-SR04 generates eight 40 kHz sound waves and waits for a rising edge on the Echo pin.
  3. When the rising edge is detected on the Echo pin (connected to the input of the PIC18F4550), the microcontroller starts its timer.
  4. The microcontroller waits for the falling edge on the Echo pin.
  5. Once the falling edge is captured, the PIC18F4550 reads the timer count. This time count is then used to calculate the distance to the object.

By calculating the time taken for the sound waves to travel to the object and return, and knowing the speed of sound, we can determine the object’s distance.

Calculation (distance in cm)

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

  = (34300 * TIMER) / 2

= 17150 * (TIMER) 

If we select an 8 MHz oscillator frequency for the PIC18F4550, the timer frequency will be 2 MHz, meaning each instruction execution time is 0.5 µs.

The timer will increment every 0.5 µs.

The distance formula can be derived as:

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

  = 0.5 * (TIMER value)/58.30 cm

 =0.5 * (TIMER value) / 58.30 cm

or

    = (TIMER value) / 117 cm

Ultrasonic Sensor HC-SR04 Code for PIC18F4550

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

  http://www.electronicwings.com
 */


#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 */
    LCD_Init();
    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:");
while(1)
	{    
    	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 */
    	sprintf(Total_distance,"%.03f",Distance);
    	LCD_String_xy(2,1,Total_distance);
    	LCD_String("  cm");
    	MSdelay(50);
	}   
}

void Trigger_Pulse_10us()
{
    Trigger_Pulse = 1;
    __delay_us(10);
    Trigger_Pulse = 0;
}

Leave a Comment

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