ultrasonic hc sr04

Digital Fiber, Photoelectric, Laser Range, Optical, Temperature, Rotary Encoder, Ultrasonic, Gas, Gyro, Accelerometer, FlexiBend, Flexiforce, Compass......

Re: ultrasonic hc sr04

Postby ABSF » Fri May 04, 2012 3:44 pm

Would you please explain how did you get this formula:

distance = ((time*340)/2)*100 //in cm

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: ultrasonic hc sr04

Postby rezza_mrkool » Fri May 04, 2012 5:25 pm

From the datasheet
elecfreaks.com/store/download/HC-SR04.pdf
rezza_mrkool
Newbie
 
Posts: 8
Joined: Mon Apr 30, 2012 1:10 am

Re: ultrasonic hc sr04

Postby ABSF » Fri May 04, 2012 7:03 pm

The Oscillator is 20 Mhz. After Fosc/4, each instruction cycle is 20/4=5Mhz or 200nS per cycle. Since the prescaler is set to 1:2, the per clock cycle becomes 400nS.

The timer1 counts from 0-65535, so let's say value of stop time - start time = 100 or time=100. Substituting that into your formula,
distance = ((time*340)/2)*100 //in cm
distance=((100*340)/2)*100 = (34000/2)*100 = 1700000 cm = 17000m
Since per clock is 400nS instead of 1uS, the distance should be divided by 1000nS/400nS = 2.5
So final distance =17000m/2.5 = 6800M or 6.8Km.

That's why your formula cannot be right.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: ultrasonic hc sr04

Postby rezza_mrkool » Sun May 06, 2012 3:46 am

Alright...there's another formula to calculate the distance......
distance = time(us)*58 //in cm unit & time should be in second not microsecond
So, i have 20Mhz Oscillator and prescalar of 1:1.....it means that i have per clock cycle of 0.2us and i have to divide by 5 to get 1us....
Then, what is time unit i getting? is it in microsecond?

Here's my code
CODE: SELECT_ALL_CODE
#include <pic.h>
__CONFIG (0x3732);

#define trigger         RC2
#define echo         RC3
#define _XTAL_FREQ      20000000
#define   MHZ   *1000L
#define   KHZ   *1

#define   DelayUs(x)   { unsigned char _dcnt; \
           _dcnt = (((x)*(20MHZ))/(24MHZ))|1; \
           while(--_dcnt != 0) \
              continue; \
           _dcnt = (((x)*    (20MHZ))/(24MHZ))|1; \
           while(--_dcnt != 0) \
              continue; }

void DelayMs(unsigned char y);

void main (void)
{
   unsigned char i;
   unsigned int time;
   unsigned int distance;
   
   TRISC = 0b00001000;
   PORTC = 0b00000000;
   TRISD = 0b00000000;
   PORTD = 0b00000000;
   

   T1CON = 0b00000000;
   TMR1H = 0;
   TMR1L = 0;

   while(1)
   {
      for (i=0;i<8;i++)
      {   TMR1H=0;
         TMR1L=0;

         trigger = 0;
         DelayUs(2);

         trigger = 1;
         DelayUs(10);

         trigger = 0;
         

         while (echo==1);
         TMR1ON = 1;
         while (echo==0);
         TMR1ON = 0;

         time = TMR1H;
         time = time<<8;
         time = time|TMR1L;
         DelayMs (10);
         
      }      
         time = time/5;
         distance = time/58;
      
      
      if(distance<15){
      PORTD = 0b10000000;
      }
   }
}

void DelayMs(unsigned char y)
{
   unsigned char   i;
   do {
      i = 4;
      do {
         DelayUs(250);
      } while(--i);
   } while(--y);
}
rezza_mrkool
Newbie
 
Posts: 8
Joined: Mon Apr 30, 2012 1:10 am

Re: ultrasonic hc sr04

Postby ABSF » Sun May 06, 2012 7:02 am

Looks like you didnt use the Arduino SR04 page that I gave you earlier. :?

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}


That's how you should get time(in uS)/58 and NOT time(us)*58. :mrgreen:

The timer1 should count 58 to give a distance of 1 cm and the max count of 65535 would give ~= 1130 cm. 8-)

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: ultrasonic hc sr04

Postby rezza_mrkool » Sun May 06, 2012 2:46 pm

I'm sorry...there's a typo....I did write distance = time/58 in my program instead of distance = time*58 in my comment........
I already did in my last coding....but still cant work...

CODE: SELECT_ALL_CODE
time = time/5;
         distance = time/58;
rezza_mrkool
Newbie
 
Posts: 8
Joined: Mon Apr 30, 2012 1:10 am

Re: ultrasonic hc sr04

Postby ABSF » Mon May 07, 2012 9:46 am

It's time for you to do some serious debugging assuming the hardware is good.
try google "how to debug with MPLAB"....

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: ultrasonic hc sr04

Postby rezza_mrkool » Tue May 08, 2012 11:49 pm

Is there any other solution ? I think there's something to do with my coding......although I understand the concept of it.....can u check it one more time...plzzzz...
CODE: SELECT_ALL_CODE
#include <pic.h>
__CONFIG (0x3732);

#define trigger         RC2
#define echo         RC3
#define _XTAL_FREQ      20000000


void main ()
{
   unsigned char i;
   unsigned int time;
   unsigned int distance;
   
   TRISC = 0b00001000;
   PORTC = 0b00000000;
   TRISD = 0b00000000;
   

   T1CON = 0b00000000;            //prescalar of 1:1
   TMR1H = 0;
   TMR1L = 0;

   while(1)
   {
      for(i=0;i<8;i++)
      {
         TMR1H=0;
         TMR1L=0;

         trigger = 0;
         __delay_us(2);

         trigger = 1;
         __delay_us(10);

         trigger = 0;
         

         while(echo==1);
         TMR1ON = 1;
         while(echo==0);
         TMR1ON = 0;

         time = TMR1H;
         time = time<<8;
         time = time|TMR1L;
         __delay_ms (10);
       }   
         
            
         distance = time/58;         
         distance = time/5;         //to get time of 1us
         
      
      if(distance<15){
      PORTD = 0b10000000;            //to enable LED on port D7
      }
   }
}

rezza_mrkool
Newbie
 
Posts: 8
Joined: Mon Apr 30, 2012 1:10 am

Re: ultrasonic hc sr04

Postby ABSF » Thu May 10, 2012 6:52 am

You only suspect but not confirm that the problem is in the software........
That's why I said debugging is needed. :mrgreen:

Did you confirm that the "trigger" pulse is there on the SR04 using a scope or logic probe?
Does the "echo" pin change state after the "trigger pulse" is sent?
Does the program flow reach the calculation part?
Are you simulating or testing the circuit on real components?

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Previous

Return to Sensor

Who is online

Users browsing this forum: No registered users and 11 guests

cron