Interfacing Ultrasonic Distance Sensor : ASCII Output with PIC Microcontroller

Interfacing Ultrasonic Distance Sensor : ASCII Output with PIC Microcontroller

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

Ultrasonic Distance Sensor interfaced with PIC Microcontroller

The sensor has three pins…

  • Vcc  – +5V DC Supply to the Sensor
  • GND –  Ground Level of the Power Supply
  • SIG – Signal, Serial Data Out

Circuit Diagram

Interfacing Ultrasonic Sensors with PIC Microcontroller
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 Output of Rhydolabz Distance Sensor

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

ASCII Value Ultrasonic Distance Sensor
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
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″]

Share this post

  • My teacher has given me the task to do the same project which show output on dial like meter not on LCD or dial like mater in matlab to do this what should i do? need help

  • hello do you have the code for ultrasonic sensor with pic18f4550 for mplab xc8??
    i tried it but i cant seem to get the distance displayed in cm on the LCD

  • how to interface a motor to microcontroller and how to interface the signals sent by the sensor to motor on object detection

  • Hello, i have followed the instructions exactly as they are described here but after downloading the hex to my pic through a velleman k8076 my microcontroller does not do anything… i’m reading back the hex from the microcontroller and it is loaded correctly but stil it does not do anything.. i have connected the display and the below line is just black. Any suggestion?

  • can i use Ultrasonic sensors – HC-SR04 in this circuit in place of given sensor(Rhydolabz’s “ECHO” Ultrasonic Distance Sensor)?

  • Hi
    I’m doing water level controller project of residential water tank. i decided to measure the level of water using ultrasonic sensor with cheap rate. please suggest what type of ultra sonic sensor should i use ? Will ultra sonic sensor detect the water level ?
    please give details of the sensor and where to buy it ?

  • I can able measured upto 40 cm only. can u pls give the procedure to measure upto 4meter?

  • and one more thing how we read the timer value after stop nd by time of echo pulse how we calculate te distance????

  • kya ap mujay bna k day sktayy hai??? actually i m the beginner … nd its rquest to u… pllllzzzzz create this code???

  • It is very simple, follow these steps..
    1. Trigger the module
    2. Listen for echo
    3. Start Timer when Echo comes
    4. Stop Timer when Echo stops
    5. Read the timer
    6. Convert to distance

  • anybody have mplab code for just ultrasonic sensor???? nd how to connect the more numbers of sensors if numbers are other than one????nd plllzz tell me which ping ultrasonic sensor sensor is used here?????

  • It this you should use .. SR-05 ultrasonic sensor. .It is low cost.. and can be connected to any pin of pic microcontroller..
    If you want to use above ascii output sensor.. you can use software uart libraries in mikroc pro

  • yes sandeep i also want to know how we use 2 or 3 sensors simultaneously….
    if u got ur answer then plz tell me……

  • how can we use 2 of the sensors at a time .it is working individually bbut some problem when using 2 of them simultaneously

  • can you please explain the internal operation of this serial ascii output sensor? internal block daigram?

  • i have 4 pin sensor, one trigger, 1 echo and 2 power pins, , can you please tell me the pin connection

  • why are we checking this input data ready ” UART1_Data_Ready()” twice?
    please help me

  • with which software you simulated this project?
    ultrasonic library don’t exist in proteus libraries.

  • sir what does this mean where is the 5us pulse for the sensor?
    for(i=0;i<5;)
    {
    if(UART1_Data_Ready()) // if data ready
    {
    dist[i] = UART1_Read(); // read data
    i++;
    }

  • hello i used that idea and make every things same but i think the code not complete
    because it dose not work with me . can a have full explanation about code ?

  • Yes… UART of most of the commonly used modules accept TTL logic… No need to use max232 here….
    Now RS232 is rarely used…… it was used in older PC’s…. todays PC’s hasn’t any built in RS232….
    So most of us use.. .USB to UART converter…. output of this converter is TTL compatible… not RS232..

  • Does USART of pic accept TTL logic? Do we need a max232 module to convert ultrasonic output signals to RS232 logic?

  • There is no ultrasonic components in proteus….
    So to simulate it, you need to create the component… I haven’t yet created my own components in proteus…..
    The above program can be built with mikroc pro compiler… to get the corresponding hex file..

  • HI, how can i simulate it at protues ISIS????
    i understand yr circuit diagram but why the ultrasonic sensor display by a image???

  • how does the sensor send the data..i mean in what format..say the distance is 123.4 cm , then ll it send the ASCII of 123.4 separately .?? like ASCII of 4 , then ‘.’ , then ASCII of 3 etc..??

  • Not sure…. Just read its datasheet or manual….
    I think.. when you provide trigger signal it sends an ultrasonic burst and gives echo signal when the ultrasonic burst receives back….. So to calculate distance you should use the equation Distance = time * velocity… where velocity is a constant..

  • I already have ultrasonic sensor with echo and trigger pins instead of SIG pin..Can i know how micropic program should be changed according to echo and trigger pins..

  • Please try it yourself.. arduino has all the required built in libraries… so just replace the functions used in this program.. to arduino functions.. it will work..

  • Can you please give me arduino code for the above ASCII output sensor? Can’t find it anywhere

  • 1. char o[7]
    Whats o here?

    2. Why is ‘j’ initialised? Its nowhere used.

  • char o[7]
    Whats o here?
    Why is ‘j’ initialised? Its nowhere used.
    for(i=0;i<5;)
    Why isn't there an increment like i++?

  • RB is used instead of PORT register and TRIS itself a register. TRIS register is used to set the direction of each pins of that port while PORT register is used to set the status of each pin of that port…. Compiler needs these registers during Building process, since LCD can be connected to any of the pins of the microcontroller..

  • Why are these LCD pins initialised twice? Once as RB and next as TRISB? What is RB and TRISB(tri state buffer?) used for?

  • Ligo George, even i have he query as MOHITH. can u please help me out or rather help us out?

  • Can you please explain me these?
    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;
    I have no idea of your coding this platform, all I know is 8051. could you please explain me these LCD initializations in 8051 C programming words? What is the logic behind the serial communication? As in, how long should the SIG send out pulses and what should the delay be?

  • Do you have the 8051 code for this? If yes,please give me the link or the code itself.

  • can i use some another ultrasonic sensor with this same code?? thanks..

  • If anybody give a c code for interface this distance sensor with pic16f877a for hi-tech c compiler….


  • >