Ultrasonic Module HC-SR04 interfacing with PIC18F4550

Ultrasonic Module HC-SR04 interfacing with PIC18F4550


Introduction

Ultrasonic Module
Ultrasonic HC-SR04 Module
Ultrasonic Module HC-SR04 works on the principle of SONAR and RADAR system.
  • The HC-SR04 module has 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.
  • Echo pin remains high till it gets 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 the topic Ultrasonic Module HC-SR04 in the sensors and modules section.

Interfacing diagram

Ultrasonic Module Interfacing with PIC18f4550
Ultrasonic Module HC-SR04 Interfacing with PIC18F4550  

Example

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 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 trigger pulse, HC-SR04 automatically sends eight 40 kHz sound wave and waits for rising edge output at Echo pin.
  3. When the rising edge capture occurs at Echo pin which is connected to the input of PIC18F4550, start Timer of PIC18F4550 and again wait for falling edge on Echo pin.
  4. As soon as the falling edge is captured at the Echo pin, microcontroller read 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
                                          or
                        = (TIMER value) / 117 cm

Program

/*
  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;
}

No comments:

Post a Comment