View previous topic :: View next topic |
Author |
Message |
Bhanu Watawana
Joined: 01 Jun 2011 Posts: 15
|
Ultrasonic Distance measuring code HC-SR04 & DYP ME007 |
Posted: Mon Jun 13, 2011 11:07 pm |
|
|
This is a simple code for taking the distance using an ultrasonic module.
This is compatible with any sonar(Ultrasonic ranging module) available in the market.
Note that you need the trigger pin for this as some of the ultrasonic modules are only for obstacle detection and not for range calculations, so that they don't have the trigger pin.
Code: |
#include <16F877.h>
#device adc=8
#FUSES NOWDT, HS, NOPUT, PROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=20000000)
/************************************************************************************************
/ Used sonar - HC-SR04 & DYP ME007
/ If you are using DYP ME 007 or any other 5 pin sonar, Please note that not to take the
/ pin named 'out'. Take echo instead.
/ Pin configuration (5 pin)- VCC(+5) TRIG(Trigger pin) ECHO(Output pin) OUT(Not needed) GND (Ground)
/ Pin configuration (4 pin)- VCC(+5) TRIG(Trigger pin) ECHO(Output pin) GND (Ground)
/
/ Code by Bhanu Watawana
/ 0778111887
/ Uva Wellassa University - Sri Lanka
/ Mechatronics
*/////////////////////////////////////////////////////////////////////////////////////////////////
#define LCD_TYPE 2
#include <lcd.c>
int16 distance, time; // Defining variables
// Defining the pins
#define trig pin_B1 // Change as you wish, can use any pin in the MCU
#define echo pin_B0 // Change as you wish, can use any pin in the MCU
void main()
{
lcd_init(); // initiating the LCD
printf(LCD_PUTC, "\f Sonar test \n Code by Bhanu ");// for LCD & MCU restart troubleshooting
delay_ms(1000); // Boot-up delay, for troubleshooting
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // initiating timer
while(true)
{
output_high(trig); // ping the sonar
delay_us(20); // sending 20us pulse
output_low(trig);
while(!input(ECHO)) // wait for high state of echo pin
{}
set_timer1(0); // setting timer zero
while(input(ECHO)) // Wait for high state of echo pin
{}
time=get_timer1(); // Getting the time
distance=time*0.028 + 1.093 ; // Calculating the distance
printf(LCD_PUTC, "\fTime :%Lu \nDistance = %Lu",time,distance); // Putting the time and
//distance to the LCD
delay_ms(1000);
}
}
|
The distance calculation equation is
Test distance = (high level time * sound velocity (340M/S) / 2.
I've used one of my own by simply taking some readings using a meter ruler and time variable. Then calculated a linear equation using a statistical software. You can use MS excel as well.
This is far more accurate than the predefined equation. That is mainly due to there are some other factors effecting the calculations.
such as Temperature, Humidity, distance between transmitter & receiver etc.
You can use Serial communication as well. All you need to do is remove
"lcd_init();", "LCD_PUTC, parts from all printf commands".
%Lu is for printing a int 16 variable. _________________ ~~~~~~~~~~~~~~~~
Bhanu Watawana
Uva Wellassa University
Sri Lanka |
|
|
taher
Joined: 12 Mar 2012 Posts: 4
|
|
Posted: Wed Mar 14, 2012 5:30 pm |
|
|
Hi
In your formula of distance you used 0.028+1.093
Can you say why they are used ?
Thanks |
|
|
Bhanu Watawana
Joined: 01 Jun 2011 Posts: 15
|
Re: Formula |
Posted: Wed Mar 21, 2012 7:43 am |
|
|
I've calibrated a formula for my ultrasonic range finder as there are many conditions that effect the result.
For example, the time taken depends upon humidity.
Once you calibrate an equation for your ambient conditions, the output will get the maximum accuracy instead of putting the speed of sound.
Use MS excel and measure some data, then calibrate your own equation. _________________ ~~~~~~~~~~~~~~~~
Bhanu Watawana
Uva Wellassa University
Sri Lanka |
|
|
drx2k
Joined: 31 Mar 2010 Posts: 10
|
|
Posted: Fri Jun 22, 2012 10:50 pm |
|
|
hi!, how to show the distance in cm or inches? |
|
|
Bhanu Watawana
Joined: 01 Jun 2011 Posts: 15
|
Re: Measuring in cm or inches |
Posted: Sat Jun 23, 2012 3:13 am |
|
|
All you have to do is when you build up the formula(calibrating), make all the measurements in any scale you need. Then build the formula accordingly. _________________ ~~~~~~~~~~~~~~~~
Bhanu Watawana
Uva Wellassa University
Sri Lanka |
|
|
rchelicopter
Joined: 19 Feb 2013 Posts: 3
|
|
Posted: Wed Feb 20, 2013 1:56 pm |
|
|
heyy, just wanted to know. does your formula gives the result in cm or meters or inches? thanks |
|
|
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
|
Posted: Sat Mar 02, 2013 4:14 pm |
|
|
The formula is giving the result in cm.
Thanks for the routine, I'm testing it and works perfectly! _________________ MGLSOFT |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Mar 11, 2013 8:41 am |
|
|
have you compared the reading you get when using each of these two lines of code?
Code: |
distance=time*0.03 + 1 ;
vs
distance=time*0.028 + 1.093 ;
|
|
|
|
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
|
Posted: Mon Mar 11, 2013 9:04 am |
|
|
I really do not use it as this code.
If for some reason the sensor is disconnected or is not responding to ping, this leaves the PIC code in an infinite loop which no longer exits, waiting for the ECHO pin.
In practice, no use to me, and I should integrate to many more features that can not wait endlessly.
I'll see to use time measurements of a CCP module or other akin, and if after sending the PING does not answer, then give alarm sensor, but continue in the program normally.
I would appreciate suggestions on how to do it right.
From already thank you. _________________ MGLSOFT |
|
|
tehmaas hasan
Joined: 06 Sep 2014 Posts: 1 Location: pakistan
|
Multiple sensor problem |
Posted: Sat Sep 06, 2014 12:36 pm |
|
|
Thank you for this great help the code is perfectly working. I want to use the same sensor, i am using 4 of them and i want to display the time and distance separately with little delay for every sensor whose distance and time are to be displayed.
Kindly help me. I am stuck i have made a code but problem is that its first sensor is responding OK the changes that i made to replicate for second sensor, it did not respond, the code compiles perfectly hex file is generated but it does not give output.
I can share the code as well.
Thank you. _________________ zart |
|
|
|