8051 – Ultrasonic Sensor Interfacing

Hi guys… Today we are going to see Ultrasonic Sensor Interfacing with 8051.

Ultrasonic Sensor Interfacing with 8051

Suggest to Read

Before we will start i would suggest you to read these topics. Then you can understand this strongly. If you already know, please go ahead.

Components Required

  • 8051 Microcontroller
  • Ultrasonic Sensor [HC-SR04]
  • LCD Module (To print the Distance)

Measuring Echo Pulse

The measurement of Echo pulse can be done with the help of external Interrupt and Timer modules of 8051 Microcontroller. In capture mode when external pulse is fed to the interrupt the timer starts counting and will stop only after the state of pulse is changed. Steps to follow to program controller as a capture device.

  1. Initialize Timer0 module as 16 bit timer with Gate bit as 1.
  2. Making Gate bit as 1 will transfer the control of timer to external interrupt pin INT0.
  3. When there is an interrupt pulse timer 0 starts counting and stops as soon as the interrupt disappears.
  4. The value in the Timer 0 (TH0 and TL0) gives the time period or length of the pulse.


  1. Connect the Trigger pin of the HC-SR04 module to any I/O pins of 8051 controller.
  2. Connect the Echo pin of the module to interrupt pin INT0 of the Microcontroller.
  3. Send a pulse of minimal timer period 10us, this will make the Ultrasonic module to send burst of data.
  4. The reflected waves will be sensed by the module and it exhibits the output in Echo pin logic high or 1 to INT0 pin of Microcontroller.
  5. This creates an interrupt and Timer 0 will start counting.
  6. When the pulse from echo pin alters its state to logic 0 or low the timer 0 stops counting
  7. The length of pulse from echo pin is proportional to the distance at which the object is located.
  8. The value in the Timer 0 gives the distance of course with some simple calculations.


According to the data sheet the distance can be given by the formula :

Distance in cm = Timer 0/59

Distance in inch = Timer 0/148



  • RS – P0.0
  • RW – P0.1 
  • EN – P0.2
  • Data Lines – P2 

Ultrasonic Sensor:

  • Trigger – P3.5
  • Echo – P3.2


Sending Trigger Pulse

Here I’m making timer value to zero. Then we are giving high to trigger pin (1). We have to wait for 10u Seconds. So i’m using _nop_() function. Each -nop_() function will generate 1us. For that we need to use intrins.h header file. Then make trigger pin to low (0).

Distance Calculating

This is the full function to calculate the distance.

  1. First i’m sending trigger pulse.
  2. Then waiting until that echo pin goes high (1). In this stage timer would be start.
  3. Again i’m waiting until that echo goes low (0). If it goes to zero, timer will OFF.
  4. Then I’m taking value from timer register. Here i’m using DPTR Register. Because in 8051 DPTR Register size is 16bits. so i’m using that.
  5. Then using formula i can calculate the Distance.

Full Code

So this code will print the distance in LCD module. You can use this module for any robot projects.



0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
Yashwant Rao
Yashwant Rao
August 30, 2018 2:36 AM

echo pin goes high (1).rt this stage timer would be start.

how you are starting the timer?? , where is the code for that ??

by interrupt ??

EmbeTronicx India
EmbeTronicx India
Reply to  Yashwant Rao
August 30, 2018 4:16 AM

Hi Yashwant Rao,

When we are initializing the timer, we are setting that Gate0 bit in TMOD register (TMOD=0x09). That means timer will be controlled by hardware. In timer0, INT0 will be the control pin. When this pin goes high, timer will start. We have connected the echo pin to that INT0 (P3.2). So timer will start when that echo pin goes high. please refer timer tutorial if you have doubt. https://embetronicx.com/tutorials/microcontrollers/8051/8051-timer-counter-tutorial/

April 9, 2019 1:43 AM

why have you added 48 could you please explain the last three lines of the code.


EmbeTronicx India
EmbeTronicx India
April 11, 2019 9:13 AM

Hi Aparna, Here I will explain those 3 lines. We will go one by one. Let’s assume range = 123. So we need to show 123 in Display. Line 1: range/100 123/100 = 1.23 = 1 (because only whole number) If we want to print 1 then we have to convert this 1 to ASCII value. We can convert any value to ASCII value by using below method. <code> any value + (0x30 or 48) 48 is the decimal of 0x30. You can refer ASCII table for further information. </code> 1234 <code>          any value + (0x30 or 48)             48 is the decimal… Read more »

May 20, 2019 12:40 AM

In interfacing diagram you use timer 0 but in program you use timer 1
is it correct?

EmbeTronicx India
EmbeTronicx India
Reply to  kavita
May 20, 2019 12:55 AM

Hi Kavita,

Thanks for commenting. In program, we did not use Timer 1. If you see there, we used TH0, TL0, TR0. So it is respected to Timer 0. And FYI we are using this P3.5 as GPIO.

Would love your thoughts, please comment.x
%d bloggers like this: