In this tutorial you will learn how to interface Ultrasonic Distance Measurement Sensor with AT89S52. Ultrasonic ranging module HC - SR04 provides 2cm - 400cm non-contact
measurement function.the ranging accuracy can reach to 3mm. The modules
includes ultrasonic transmitters, receiver and control circuit.I also Provide you complete Project code you can download it from the bottom of the page.
Now working of HC-SR04 as follow:
- Send 10us HIGH pulse on TRIG pin of HC-SR04.
- The sensor sends out a “sonic burst” of 8 cycles.and detect whether there is a pulse signal back.If there is an obstacle in-front of the module, it will reflect the ultrasonic burst.
- If the signal is back, ECHO output of the sensor will be in HIGH state (5V) for a duration of time taken for sending and receiving ultrasonic burst. Pulse width ranges from about 150μS to 25mS and if no obstacle is detected, the echo pulse width will be about 38ms.
Pinout of HC-SR04:
HC-SR04 Front view |
- VCC – 5V Power supply.
- TRIG – Trigger Pin connect to the P1.1 of AT89S52.
- ECHO – Echo Pin connect to the P1.0 of AT89S52.
- GND – -0V Ground.
Timing Diagram of HC-SR04:
Timing Diagram of HC-SR04 |
Circuit Diagram:
Programming Steps:
- Provide TRIGGER to Ultrasonic module HC-SR04.
- Listen for Echo.
- Start Timer when ECHO HIGH is received.
- Stop Timer when ECHO goes LOW.
- Read Timer Value.
- Convert it to Distance.
Here Trigger pin of HC-SR04 is connected P1.1 of AT89S55 which is output pin.Echo pin is connected to P1.0 of AT89S52 which is Input.
sbit Echo = P1^0;
sbit Trig = P1^1;
Timer 0 is used in 16-bits mode for measuring time.
TMOD |= 0x01;//Timer 0 in 16-bit mode.
Now Send 10us HIGH pulse on TRIG pin of HC-SR04.
Trig = 1;
delay_us(10);
Trig = 0;
Wait for rising edge at echo pin of HC-SR04.
while(Echo == 0);
Start Timer 0 and clear timer count register.
TR0=1;
TL0=TH0=0;
Now wait for falling edge at Echo pin and stop timer and Calculate time.
while(Echo == 1)
{
if(TF0 == 1)//timer over if no obstacle is detected
break;
}
TR0=0;
TF0 = 0;
//Calculate number of count
Count = TL0 + TH0*256;
//Calculate total time in uS.
Time = Count*1.085;
//As per datasheet of HC-SR04 Distance is in Centimeter.
Distance = Time/58;
//The distance is then sent using serial communications.
Nice tutorial..and also post some Protocols, because its very much for begineers, including me.
ReplyDeleteYaah its really nice
ReplyDeleteThank you bro ..for this openelectronics blog ....and for share this knowledge also...
ReplyDeleteaccording to this codding part what is the code for glow a led when ultrasonic interface an object?
ReplyDelete@Rhythm Kr. Dasgupta
ReplyDeleteCan you tell me maximum distance of the robot from the object that you want to detect?.
As ex. If Max. distance is 10 cm than add small code in main function
if(Distance<=10)
{
//LED ON
}
else
{
//LED OFF
}
plz share all programe of this project
Deletei cant understand where insert this function. would u help me give me the total codding part to glow a led ????
Deleteplz share complete programme of this project
ReplyDeleteYou can download complete project from link given at bottom of this post
DeleteHere i also give you link you can download from here.
http://www.mediafire.com/download/55mpyr839kc8yc6/HC_SR04_Ultrasonic_Sensor.rar
Hello nice tutorial
DeleteWhile going through the code, I saw this line of code:
Serial_Init();
TX_String("Silicon TechnoLabs\r\n");
TX_String("Distance Measurement using Ultrasonics Sensor HC-SR04\r\n");
Please how can I integrate the LCD and then display this strings on the LCD
Thanks
My email is joshuachinemezu@gmail.com
i cant understand where insert this function.
ReplyDelete****
if(Distance<=10)
{
//LED ON
}
else
{
//LED OFF
}
would u help me give me the total codding part to glow a led ????
#include
ReplyDelete#include "Serial.h"
sbit Echo = P1^0;
sbit Trig = P1^1;
LED = P1^2;
void delay_us(unsigned int us)//This function provide delay in us uS.
{
while(us--);
}
void delay_ms(unsigned int ms)//This function Provide delay in ms Ms.
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<127;j++);
}
void main()
{
unsigned int Count,Time,Distance;
Echo = Trig = 0;
TMOD |= 0x01;//Timer 0 in 16-bit mode
Serial_Init();
TX_String("Silicon TechnoLabs\r\n");
TX_String("Distance Measurement using Ultrasonics Sensor HC-SR04\r\n");
do{
Trig = 1;//Send 10us start pulse to HC-SR04 as per datasheet of HC-SR04
delay_us(10);//~10us delay
Trig = 0;
while(Echo == 0);//Wait for Rising edge at Echo pin
TR0=1;//Start Timer
TL0=TH0=0;//Clear timer count register
while(Echo == 1)//Wait for Falling edge at Echo pin
{
if(TF0 == 1)//timer over if no obstacle is detected
break;
}
TR0=0;//Stop Timer.
TF0 = 0;//clear Timer Over Flow Flag
Count = TL0 + TH0*256;//Calculate number of count
Time = Count*1.085;//Calculate total time in uS.
Distance = Time/58;//As per datasheet of HC-SR04 Distance is in Centimeter
TX_String("--------------\r\n");
TX_String("Distance:-");
TX_Int(Distance);//Send distance to serial
TX_String("\r\n");
if(Distance<=10)
{
LED = 1;//LED ON
}
else
{
LED = 0;//LED OFF
}
delay_ms(2000);
}while(1);
}
thank you
ReplyDeleteits gives an syntax error :
Deletecompiling HC_SR04.c...
HC_SR04.c(64): error C141: syntax error near 'delay_ms', expected 'while'
HC_SR04.c(65): error C141: syntax error near ''
Target not created.
Build Time Elapsed: 00:00:00
Batch-Build summary: 0 succeeded, 1 failed, 0 skipped - Time Elapsed: 00:00:01
its gives an syntax error :
ReplyDeletecompiling HC_SR04.c...
HC_SR04.c(64): error C141: syntax error near 'delay_ms', expected 'while'
HC_SR04.c(65): error C141: syntax error near ''
Target not created.
Build Time Elapsed: 00:00:00
Batch-Build summary: 0 succeeded, 1 failed, 0 skipped - Time Elapsed: 00:00:0
hi,this post is quite useful.Please help me with this. I am working on obstacle masurement and i have to connect 4 hc sr04 ultrasonic range finder with Microcontroller At89S52. please provie the circuit diagram for it. Please provide as soon as possible.
ReplyDeleteemail me at: richapatel053@gmail.com
If you want to interface four sensor than you can not able to read all the sensors at same time.
Deleteplease , can you help me to explain why the project dose not operate when i load hex file to the microcontroller , only appear ( range finder )on lcd , and not display the distance
ReplyDeleteemail me at : zayd_hussam@yahoo.com
Have you integrate code for lcd?
DeleteIf yes than show me your code.
i using the code . that i download from the link
Deletehttp://www.mediafire.com/download/55mpyr839kc8yc6/HC_SR04_Ultrasonic_Sensor.rar
Dear zaid, this code doesn't contain LCD interfacing.
DeleteThis comment has been removed by the author.
ReplyDeletehello,
ReplyDeletehow do you find '1.085' and why do you do 'TH0*256'?
//Calculate number of count
Count = TL0 + TH0*256;
//Calculate total time in uS.
Time = Count*1.085;
Which clock do you use for Timer0 ? ( you don't use CKCON here, why?)
Thanks !
1.085 is time in microsecond taken by timer 0 for 1 count.
Delete(TH0*256) - Because timer 0 in 16-bit mode there value store in 2 separate 8-bit register so for calculate total time you need to multiply higher 8-bit with 256 + lower 8-bit.
Timer 0 clock is 921.6 KHz
good work unfortunately i also did the same project with different approach
ReplyDeleteDear suryateja polina,
DeleteYou can also share your idea. :)
Most of the time I don’t make comments on any post, but I'd like to say that this content is really helpful. Please share with us more post like this.
ReplyDeleteUltrasonic Level Sensor
Thank you very much Deba Sheesh. We'll share some of our work here stay tuned.
DeleteNice post,In modern applications, ultrasonic sensors are described by their dependability and extraordinary flexibility. Ultrasonic sensors can be utilized to unravel even the most complex errands including object discovery or level estimation with millimeter accuracy, in light of the fact that their measuring strategy works dependably under all conditions.see more:-www.seeautomation.com
ReplyDeleteLooking at been given within your blog still putting solution obviously only a bit small submits. Enjoyable technique for future forthcoming, We've been book-marking at a stretch protect variations give up soars together with each other. UKdistance
ReplyDeleteThanks For sharing very helpful information
ReplyDeleteVibrating Fork Point Level Switch for Liquids
Great and I have a dandy offer: Who Repair House Windows home reno costs
ReplyDeleteGreat and I have a dandy present: Whole House Renovation Checklist Pdf house renovation process
ReplyDelete