Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Distance calculation formula for ultrasonic sensor and its calibration for PIC16F877A

Status
Not open for further replies.

udayan92

Full Member level 5
Joined
Mar 2, 2014
Messages
284
Helped
40
Reputation
80
Reaction score
39
Trophy points
28
Location
India
Activity points
1,496

hello,

the best is to start from scratch !
and so understand all parameters and final equation for calculus..

i just tested an ultrasonic sensor and i did this kind of approach ,...
then included temperature correction..
you can have a look on this
**broken link removed**
and then start your own application....with YOUR DATA..
even physical values will be allways the same
ie: 5.88µS/mm at 20°C for the time to cover 1 mm of distance....
..except for different absolute air pressure (1013mBar)

After do it, you will undertsand other way to get same final result...
 
hello,


distance=time*0.028+1.093

I tried also to understand and find out this:

FOSC=20Mhz
Cycle= 4/20=0.2µS
timer count= 0.2*8=1.6µS (prescaler=8)
at 20°C sound speed = 34000 cm/sec
within 1.6µS distance = 1.6*0.000001*34000=0.0544 cm per count
but sound distance is twice ( to come an go back)
so relationchip becomes 0.0544/2=0.0272 cm per count

to START the measure ,the device needs a pulse of 10µS on trigger input
then send (itself) a burst of 8 periodes of 40Khz (so during 200µS)
then echo output signal goes to 1 status...an return to 0 status when echos is back
Timer1 measure this duration
To avoid to ear the receiver when emitter send the burst salve of 40khz ,
timer1 start must begin to count after :10+210=210µS .
so minimum distance (theorical) is :
210*0.0544=5.712 cm (0.0544 because olny one way)

and add a litle offset for calibration here offset is 1.093 cm
(maybe half of the heigh of each sensor * 2)
This offset can change also with the START event for timer1 counting !

HC-SR04_Timming.jpg

delta temperature of 10°C => delta 1.7% on distance without compensation.
So it is better to add temperature compensation on this model of sensor..
I did it with a LM35DZ sensor..

or use another type witch include Temperature compensation
Modele US-100 Y4U1 with 5 pins (instead of 4 )
 

After reading the explanation,I calibrated my sensor in this way:
The crystal on my development board has a frequency of 12MHz,so the cycle time is 4/12M =0.33µs
and by using this approximate formula for speed of sound depending upon temperature:
**broken link removed**
I got the speed of sound in 27 degrees centigrade as 347.2m/s.
The distance travelled by pulse for a single count time,I got it as
distance=0.33*10^-6 * 347.2*10^2=0.01146 cm. Hence d/2 = 0.00573cm
The new formula is d=time*0.0058+1.093..
Strangely,when I substituted 0.058,it worked well..Initially I was getting a max range of 177cm,
Then after doing the new substitution,The max range I got was 278cm.But when I caught the error
and re substituted it to 0.0058,I got the range drastically reduced to just 10cm..I guess I have to go with 0.058 as the correct number.

Can you please explain again how did you minimum distance for 20MHz clock frequency as 5.7cm and how to calibrate the sensor for temperature compensation? I did not understand it fully..

Correction----
I realized that I did not multiply the 0.33µs with 8...so the time for single cycle will be 0.33µs*8=2.64mS.
I multiplied it with the speed of sound in cm/s and got the distance as:
distance=2.64ms*347.2*10^2=0.09166cm/count... that when I divide it by 2...it becomes 0.0458....I got the error!!
Ignore the previous post.
 
Last edited:

Correction----
I realized that I did not multiply the 0.33µs with 8...so the time for single cycle will be 0.33µs*8=2.64mS.

Code:
With FOS of 12MHz and prescaler of 8 
Fosc	12	MHz
1 cycle	0,333	µS
prescaler	8,000	
1 count	2,667	µS
air sound speed	347,200	M/sec
air sound speed	34720,000	cm/sec
distance/count	0,926	cm/count
d/2	[B][COLOR="#FF0000"]0,463	cm/count[/COLOR][/B]

because 2.667µS instead of mS


with prescaler=1, better resolution to use
Code:
Fosc	12	MHz
1 cycle	0,333	µS
prescaler	1,000	
1 count	0,333	µS
air sound speed	347,200	M/sec
air sound speed	34720,000	cm/sec
distance/count	0,116	cm/count
d/2	0,058	cm/count
 

I din not clear in offset value selection for ultrasonic sensor ....could you explain again?
 

There are some working projects at libstock.com but they use mikeoe compilers.
 

Hello,

I din not clear in offset value selection for ultrasonic sensor ....could you explain again?

At least, offset value is done to adjust calculated measure by your program and real measure....
it can differe between sensors.. and don't forget with temperature if no compensation
I join a recap of my point of vue about this subject..

y= ax + b with (b=offset) x = a * speed of sound (optional * coeff temperature)


 

Attachments

  • US_theorie.jpg
    US_theorie.jpg
    91.2 KB · Views: 146
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top