electroSome

Interfacing Ultrasonic Distance Sensor : ASCII Output with PIC Microcontroller

Ultrasonic Distance Sensor

Ultrasonic Distance Sensor

In some of our projects, we may want to measure the distance of an object from a point. Ultrasonic Distance Sensors are the best sensor which provides stable, accurate, precise, non-contact distance measurements from 2cm to 4m. Ultrasonic Sensors can be used to measure distance between moving or stationary objects. Being very accurate and stable, these devices find large number of applications in robotics fields. For example it can be used as an excellent replacement for IR sensors in  a Micromouse. In this tutorial we will lean to interface an Ultrasonic Distance Sensor with PIC Microcontroller.

Here for demonstration we are using Rhydolabz Ultrasonic Distance Sensor with ASCII Output. It can be easily interfaced with a PIC Microcontroller using USART by just connecting the output pin of the sensor to RX pin of the microcontroller. In every 500ms this sensor transmits an ultrasonic burst and listens for its echo. The sensor sends out ASCII value corresponds to the time required for the ultrasonic burst to return to the sensor. The UART of the sensor is operates at a baud rate 9600 and the sensor can be powered by  a 5V DC Supply. The ASCII output of the sensor will be equal to the distance to the obstacle in centimeter (cm).


Ultrasonic Distance Sensor interfaced with PIC Microcontroller

The sensor has three pins…

Circuit Diagram

Interfacing Ultrasonic Sensors with PIC Microcontroller

The sensor outputs distance in centimeter (cm) via USART. The output format can be found from the datasheet of the sensor as follows.

ASCII Output of Rhydolabz Distance Sensor

ASCII values corresponds to each character can be found from the following table.

ASCII Value Ultrasonic Distance Sensor

The sensor outputs error message if the target object distance is above 4.3m.

ERROR Message from Ultrasonic Distance Sensor

MikroC Program

// LCD module connections
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections

void main() 
{
 int i;
 char dist[] = "000.0";
 Lcd_Init(); // Initialize LCD
 UART1_Init(9600); //Initialize the UART module
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 Lcd_Out(1,1,"Distance= cm");
 do
 {
   if(UART1_Data_Ready()) //if data ready
   {
     if(UART1_Read() == 0x0D) //check for new line character
     {
       for(i=0;i<5;)
       {
         if(UART1_Data_Ready()) // if data ready
         {
           dist[i] = UART1_Read();  // read data
           i++;
         }
       }
     }
   }
  Lcd_Out(1,10,dist);
}while(1);
}

I think the program is self explanatory. Every time program checks for new line character <CR> (0x0D), when it founds it, the program reads next 5 characters as the distance data. The distance data is assigned to a character array and is displayed in the LCD.

Buy Here

[products ids=”8605, 8838, 9566″]