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

Leave a Reply

92 Comments on "Interfacing Ultrasonic Distance Sensor : ASCII Output with PIC Microcontroller"

avatar
  Subscribe  
newest oldest most voted
Notify of
Gobalakrishnan.M
Guest

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

Ligo George
Guest

I haven’t yet used hi-tech c compiler… try converting this code to hi-tech c..

studyembedded
Guest

Thanks this helped me a lot…thanks for sharing

pratik
Guest

is it complete code?

Ligo George
Guest

Yes……

Guest

Thanx bro

steveyd
Guest

Did you use any header files? and if so which ones

Thanks

Ligo George
Guest

Nop, I have used built in library functions of mikroC pro..

npyasith
Guest

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

Ligo George
Guest

Nop, it depends on the ultrasonic sensor you use..
If it is ascii output ultrasonic sensor, it may work…

npyasith
Guest

thanks,,,

Sharath
Guest

Whats the cost of this module? It sends out ASCII values?

Ligo George
Guest

It’s cost is about 20$. Yes.. it sends out ASCII values… PWM output sensors are also available in the market..

Likhith
Guest

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

Mohith
Guest

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?

Karthik
Guest

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

Sharath
Guest

The module works fine right? Is it worth the buy?

Ligo George
Guest

Sorry, Kindly convert the above code to 8051 by writing a UART function for it…

Ligo George
Guest

Yes it is working fine… Its cost ranges from 15$ to 20$..

Ligo George
Guest

That question is answered..

Ligo George
Guest

These statements are used to tell the compiler that a 16X2 LCD is connected to the above defined pins… and the LCD display is accessed using Lcd_ etc functions…. Please go to this link to do this in 8051 using keil c
http://www.electrosome.com/interfacing-lcd-with-8051-using-keil-c-at89c51/

UART serial communication is used, you can use the internal USART module of 8051 for that..

Mohith
Guest

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
Guest

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..

Sharath
Guest

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++?

Sharath
Guest

1. char o[7]
Whats o here?

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

Ligo George
Guest

Sorry, it was used in other applications.. they are not needed here…So I removed them… thanks for the query..

harish kumar
Guest

Do you have MPLAB program for this???

Ligo George
Guest

Nope…

Randy Ndzi
Guest

Please sir, do you have ccs c code for this?

Ligo George
Guest

Sorry I am little busy in these days.. Surely I wlll do it one day..

Sharath
Guest

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

Ligo George
Guest

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..

Nibhath
Guest

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..

Ligo George
Guest

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..

Encik
Guest

how to use this coding in proteus…. i use same circuit.. above

Ligo George
Guest

For simulating this in proteus.. you should create a new component (ultrasonic distance sensor) in proteus.. as it is not present in proteus library..

Encik
Guest

can i coding in MPlab… i new in progrm..

Ligo George
Guest

Sure, you proceed…
Please send us the code after completing the mplab code..

pratyush
Guest

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..??

Ligo George
Guest

yes.. it will send ASCII of 123.4 separately.. as given in the table..

low
Guest

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

Ligo George
Guest

There is no library for ultrasonic distance sensor in proteus… so you can’t simulate…

Hashim King
Guest

plz i want to know how u simulate this circuit

if u use protes can u show me the HEX file

Ligo George
Guest

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..

marwan
Guest

what’s RV1 stand for? is it resistance 100 ohm?

Ligo George
Guest

Use 2.2K ohm..

bob
Guest

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

Ligo George
Guest

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..

cute
Guest

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 ?

Ligo George
Guest

this code is written and tested only for the ultrasonic sensor prescribed in the article..