Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

Status
Not open for further replies.

ronexg

Newbie level 5
Joined
Nov 21, 2013
Messages
8
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,283
Visit site
Activity points
1,347
Hello friends,

I want to share my small project...Ultrasonic distance measuring.

Compiler : MikroC Pro V.6
Xtal : 16MHz
Sensor : HC-SR04 from ebay
LCD : 20x4

Trigger pin was connected to RC3
Echo pin connected to RD0

Duration measure formula:

Duration(us) = TMR * Clock speed per instruction(Fosc/4) * timer pre-scale(1:4)

Clock speed per instruction(Fosc/4) : 16/4= 4MHz

Here i am using 1:1 Prescaler

So we get us = TMR*0.25*1............ 4MHz == 250 Nano sec == 0.25 us
for distance in centemeter divide by 58

Suppose we get 9327 as TMR value, the distance in mm is: (9327/58)*25 == 4020 mm (25 is 0.25 * 100 for avoiding floating value)
 

Attachments

  • HC-SR04.txt
    1.4 KB · Views: 110

hello,

Good application !
Thank's for sharing..

you can also increase accuracy by adding air temperature measurement ,
to correct sound speed in the air... you can earn any % of accuracy..
Code:
// Via la mesure de temperature LM35DZ en °C
// reajuste  la vitesse son dans l'air f(Temperature)
// y = 0,5803x + 328,39
     Sound_Speed= Filtered_Temp *0.5803 + 328.39;
     fltToa (Sound_Speed, ValFt2,1); // 1 decimale
     k=fprintf(_H_USART,(Flash_Mem *)"Vs=%s;",ValFt2);

but , written with C18 MPLAB.. with Pick18F26K22 ...
So it's just the formula to use...

you must add ADC measure for LM35DZ 0 to 50°C .. no measure in Alaska !
and use the new air sound speed for distance calculation..

i will try to translate it in MikroC for 16F8x7 and gives feed back to this post..
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…