Distance Measurement and Obstacle Detection Using NXP 89v51rd2fn and Ultrasonic Sensor HC-SR04.




About: http://instagram.com/saurabhprashar1/

Everyone uses arduino uno or mega to make projects as they are easy to use and the code for arduino is easily available on the arduino forums and no one uses atmel,philips or other microcontroller to make such projects as the coding is somewhat difficult . In this project of mine i have used philips nxp microcontroller and embedded c language to write the code and this is my first contribution to instructables. The total cost of the project was under 25$.

Step 1: Philips NXP 89v51rd2fn and Ultrasonic Sensor HC-SR04

A simple distance measurement and obstacle detection using NXP 89v51rd2fn and ultrasonic sensor HC-SR04 is presented in this article. This ultrasonic rangefinder can measure distances up to 4.0 meters at an accuracy of 1 centimeter. NXP89v51rd2 microcontroller and the ultrasonic transducer module HC-SR04 forms the basis of this circuit. The ultrasonic module sends a signal to the object, then picks up its echo and outputs a wave form whose time period is proportional to the distance. The microcontroller accepts this signal, performs necessary processing and displays the corresponding distance on the 16x2 lcd display. This circuit finds a lot of application in projects like automotive parking sensors, obstacle warning systems, terrain monitoring robots, industrial distance measurements etc.

Step 2: HC-SR04 Ultrasonic Module.

HC-SR04 is an ultrasonic ranging module designed for embedded system projects like this. It has a resolution of 0.3 cm and the ranging distance is from 2cm to 500cm. It operates from a 5V DC supply and the standby current is less than 2mA. The module transmits an ultrasonic signal, picks up its echo, measures the time elapsed between the two events and outputs a waveform whose high time is modulated by the measured time which is proportional to the distance.

Step 3: Material Required::


2.NXP 89v51rd2 microcontroller

3.crystal 11.0592mhz

4.capacitor 150pf-2 10microfarad-2

5.resistors 330ohm-2 10k-4


7.DB9 connector female

8.1x10k pullup resistor


10.4xn 4007 diodes

11. 10 microfard capacitor

12. ultrasonic sensor HC-sr04

and other basic material like soldering iron , connector,flux etc.

Step 4: Working of HC-SR04 Ultrasonic Ranging Module.

The supporting circuits fabricated on the module makes it almost
stand alone and what the programmer need to do is to send a trigger signal to it for initiating transmission and receive the echo signal from it for distance calculation. The HR-SR04 has four pins namely Vcc, Trigger, Echo, GND and they are explained in detail below.

1) VCC : 5V DC supply voltage is connected to this pin.

2) Trigger: The trigger signal for starting the transmission is given to this pin. The trigger signal must be a pulse with 10uS high time. When the module receives a valid trigger signal it issues 8 pulses of 40KHz ultrasonic sound from the transmitter. The echo of this sound is picked by the receiver.

3) Echo: At this pin, the module outputs a waveform with high time proportional to the distance.

4) GND: Ground is connected to this pin.

the 40KHz pulse train is transmitted just after the 10uS triggering pulse and the echo output is obtained after some more time. The next triggering pulse can be given only after the echo is faded away and this time period is called cycle period. The cycle period for HC-SR04 must not be below 50mS. According to datasheet, the distance can be calculated from the echo pulse width using the following equations.

Distance in cm = echo pulse width in uS/58

Distance in inch = echo pulse width in uS/148

Step 5: Philips NXP 89v51RD2fn.

P89V51RD2FN - 8-bit 80C51, 5 V, 64 kB flash, 1 kB RAM 40-PDIP, NXP Semiconductor

  • 80C51 CPU
  • 5 V operating voltage from 0 MHz to 40 MHz
  • 64 kB of on-chip flash user code memory with ISP and IAP
  • Supports 12-clock (default) or 6-clock mode selection via software or ISP
  • SPI and enhanced UART
  • PCA with PWM and capture/compare functions
  • Four 8-bit I/O ports with three high-current port 1 pins (16 mA each)
  • Three 16-bit timers/counters 32 General Purpose I/O
  • Programmable watchdog timer
  • Eight interrupt sources with four priority levels
  • Second DPTR register
  • Low EMI mode (ALE inhibit)
  • TTL- and CMOS-compatible logic levels
  • Factory loaded bootloader for Serial Programming through UART.

Step 6: Circuit Diagram.

Step 7: Loading HEX File Into 8051 (p89v51rd2) .

Micro-controller only understands binary language. So we need to install hex code into the ROM of controller chip. There are so many loaders available in the market to flash the ROM of micro-controllers. But they are very costly. As i posted earlier about flash magic and specification of p89v51xx chip. We can program hex file easily into ROM. We are going to use DB-9 cable or RS-232 for this and flash magic software.

flash magic is only support those device which supports ISP. So such chip has boot loading software in built and it is known as a boot loader. So our main target is to put our victim chip into boot loading mode. This is achieved by resetting chip.

when start button of flash magic is pressed to indicate flashing the ROM, it continuously sends 'U' to controller and when the chip is being reset, it goes into boot load mode and gives the echo to flash magic and hex code is then loaded into ROM.

So there is mainly two ways to put chip in boot loading mode..

1) Using Reset switch

2) Without Reset switch

Using Reset switch

Using reset switch you can put chip into boot loading mode by using two method, in both the method circuit diagram remains same.

Method-1 :

1) Configure flash magic

2) Power up your circuitry

3) Click on "start" button in flash magic

4) When it ask for "Reset device" press reset switch

Method-2 :

1) Configure flash magic

2) Power up your circuitry

3) Press and hold reset switch

4) Click on "start" button in flash magic

5) When it ask for "Reset device" release reset switch

Without using reset switch

Circuit diagram is given below and the only difference in this is that it does not contain reset switch. So the feature which we gonna use here is brown out detection.

1) Configure Flash magic

2) Click on "start" in flash magic

3) When it asks for "Reset device" power up your circuitry

So its very easy and less time consuming. You can use any method suitable to you.

Step 8: Circuit Connections.

Step 9: Program Code for Nxp Microcontroller.

The code for this project written in embedded c language.

Step 10: Final Product.

for the casing i have used an old stethoscope box which was lying in my house and was of no use.

Step 11: Testing.

I have tested this by placing an object in front of the ultrasonic sensor and moving the object towards and away from the sensor.



    • Arduino Contest 2019

      Arduino Contest 2019
    • Tape Contest

      Tape Contest
    • Trash to Treasure

      Trash to Treasure

    9 Discussions


    2 years ago

    can you provide video tutorial.. It's somewhat confusing for me as i am a starter and i donno about these projects. Please


    2 years ago


    i need c file please send me at my gmail:-ds99290@gmail.com


    3 years ago

    Can we use any other micro controller ? Like ATtiny ? This one is not available in my area ?

    1 reply

    3 years ago

    please provide hex file for this code,u can send to nbsmadhav@gmail.com,thankyou'

    1 reply
    saurabh prasharBHARGAVC1

    Reply 3 years ago

    The hex file is attached in step no. 9 . You can download from there.

    If you need more help PM me.


    4 years ago on Introduction

    could u please send the c file to gagan.surathkal@gmail.com

    thank u