SENSOR- Ultrasonic HC-SR04 (Interfacing HC-SR04 with 8051)

Hi friends,
I am doing a project based on obstacle detector. It is a outdoor project hence i choose to buy an ultrasonic sensor to work efficiently on both bright sun light as well as dark nights. Now my problem is i am not able to interface this sensor with my controller. Anyone please tell me what input i must give to trig input of that sensor and what kind of output i get form the echo pin. also tell me how can i interface this sensor with the AT89S52.
i have attached the user manual which i downloaded from internet. 
Please help me with your valuable comments, Thanks in advance.

sort by: active | newest | oldest
gr8yt1 year ago

Perhaps my Instructable on the HC-SR04 will help. You'll find it here; https://www.instructables.com/id/Simple-Ultrasonic-...

Let me know if you still need help after reading through that.

RhythemK1 year ago
hey i want to glow a led when an object come to HC_SR04 .i need a code???plz hlp

Here you can find complete solution


knachuthan (author) 2 years ago

The trig input of sensor needs high pulse of min 10us so i did this!

Port 2.0 = 1

1 ms delay

Port 2.0 = 0

then waited for echo pulse!!

As per manual The echo pulse should become 1 after transmitting 8 x 40KHz pulse. but there is no such transformation. It remained same (0) after and before trig i/p.

instead of giving clock pulse i tried this way!!!
What i feel is i made trig active for 1ms! does it affect my output?
Because trig requires just 10us! immediately after 10us the sensor emits sonic waves and recieves! does this process gets over in less than a milli sec.?
Even in this case when there is no obstacle the echo is high for 38ms ( as per manual). So where is rest 37ms response!!!?? there is no such high pulse from echo.

Could anybody get where i am making mistake

The mistake is still your input signal and how you read the output.

1ms is far too long and you have to measure the pulse lenght of the output signal - there is no simple 0 or 1 here.

Either do it by the book or get an oscilloscope to check your signals and correct them until you have it working with the right specs.

You won't be able to use a multimeter for this and without a properly written program you can read and calculate anything coming from the sensor - that is if you get the input signal correct.

Quite simple really, you need a square wave input signal with at least 10µs pulse length.

The output is a square wave too, the distance measured is reflected by the pulse width on the echo pin.

Should be explained a bit more in the manual but in case you don't have the manual:


knachuthan (author)  Downunder35m2 years ago

Thanks a lot, but i already have this manual! I refered this manual before buying the product! In my program i made trigger Pin as one and waited for response! But echo output is always 1. It depends on my trig i/p. When trig is 1 Echo is 1 and vice versa. Even i gave 1kHz clock pulse to trigger. Same! The output (echo) is always 1.

Ok, but what is your pulse lenght?

For a 10µs pulse at a 50% setting you are looking at a 50kHz input signal, 30kHz gives a longer pulse. - But I might be wrong here, way past my bed time tonight LOL

I guess there is a reason why it operates at 40kHz?

If in doubt there is always the official documentation with testing procedures based on a little Arduino, code is provided too.


knachuthan (author)  Downunder35m2 years ago

Thats really gud! Tommorrow wil try and wil post my results! Also i got an Small idea of interfacing this module without clock pulse! let me check it out! ;)