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.

[PIC] HC-SR04 ultrasonic sensor problem.

Status
Not open for further replies.

ruben91

Junior Member level 3
Junior Member level 3
Joined
Nov 17, 2014
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
209
can i know why my sensor cannot detect more than 230cm? perhaps in data sheet it stated can detect more than 400 cm.. im using pic18f4580 with 20Mhz crystal frequency
 

hello,

what is your power supply voltage ?
show all your code ..
maybe your device is out of spec. or your code is not compliant for that.
 

hello,currently im using 5v dc supply, this my code
Code:
#include <p18f4580.h>
#include <delays.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#pragma config OSC= HS
#pragma config WDT=OFF
#pragma config LVP=OFF

#define Trig PORTBbits.RB3
#define Echo PORTBbits.RB2

void T0Delay10us(void);
void T0Delay5ms(void);
void uart(void);
void Call_Init_PIC (void);
void sendPulse(void);

unsigned int distance ;
unsigned int time;
unsigned f,b;

void main(void)
{
TRISDbits.TRISD2=0;
TRISDbits.TRISD3=0;
TRISDbits.TRISD1=0;
TRISDbits.TRISD0=0;
Call_Init_PIC ();
uart();
while(1)
{
TMR1H= 0;
TMR1L= 0;
sendPulse();

while(Echo == 0);

T1CONbits.TMR1ON = 1;
while(Echo == 1);
T1CONbits.TMR1ON = 0;  

Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);                   

time= TMR1H;
time= time<<8;
time= time|TMR1L;
//time = (TMR1L + (TMR1H<<8));

distance = time/28.8;
f=distance/10;
b=distance%10;
printf("\r\nDistance = %d.%d cm",f,b);  
PIR1bits.TMR1IF=0;
Delay10KTCYx(255); 
T0Delay5ms(); 
/*
if(distance>=0 && distance<=250)
{
PORTDbits.RD2=1;
//printf("\ndangerous");
}

if(distance>=251 && distance<=500)
{
PORTDbits.RD3=1; 
}

if (distance>=501 && distance<=1000)
{
PORTDbits.RD0=1; 
}

if(distance>=1001)
{
PORTDbits.RD1=1;
//printf("\nsafe zone");
}
*/
}
}

void Call_Init_PIC (void)
{
TRISBbits.TRISB3=0;
TRISBbits.TRISB2=1;
}


void uart(void)
{
TXSTA=0B00100000;
SPBRG=32;
TRISCbits.RC6=0;
RCSTAbits.SPEN=1;
}


void sendPulse(void)
	{
	Trig=1;
	T0Delay10us();
	Trig=0;
	}


void T0Delay10us()
	{
	T0CON=0x08;
	TMR0H=0XFF;
	TMR0L=0XCE;
	T0CONbits.TMR0ON=1;
	while(INTCONbits.TMR0IF==0);
	T0CONbits.TMR0ON=0;
	INTCONbits.TMR0IF=0;
	}


void T0Delay5ms()
	{
	T0CON=0x08;
	TMR0H=0X9E;
	TMR0L=0X58;
	T0CONbits.TMR0ON=1;
	while(INTCONbits.TMR0IF==0);
	T0CONbits.TMR0ON=0;
	INTCONbits.TMR0IF=0;
	}
 

When I tested this sensor a year ago with STM32 Discovery it shown the same result. About 2-3m range. Possible, china's meters are shorter as always? ))))
 
hello,


Your code is correct but limited to a range of 2229 mm!
Time for sound move (in the air at 20°C) for 1mm is 2.94 µS =>5.88µS aller-retour
With Q=20Mhz Cycle is 4/20=>0.2µS
With timer0 16bits you can count a maxima of
65536*0.2=13107µS
corresponding displacement is 13107/5.88=> 2229 mm
after you have timer0 oveflow..
You need to take account oth this overflow,bit adding 65536 evrey time
Time1r interrupt flag is On..
so use a long int 32bits for time value..

Code:
unsigned long Over;
Unsigned long time;
....


while(1)
{

sendPulse();
T1CONbits.TMR1ON = 0;  
PIR1bits.TMR1IF=0;
TMR1H= 0;
TMR1L= 0;
Over=0;
time=0;

while(Echo == 0);
T1CONbits.TMR1ON = 1;
while(Echo == 1)
{
 if (PIR1bits.TMR1IF==1)
  {
     Over=Over+65536;
     PIR1bits.TMR1IF=0;
  }
}
T1CONbits.TMR1ON = 0;  

time= TMR1H;
time= time<<8;
time= time|TMR1L;
Distance= (time + Over)/58;    // <- don't forget travel of sound in both way forward then reverse

// or you can  use floating point calculus  to get distance in mm  distance/5.88

my application uses Capture CCP1 mode and i add air temperature coorection
you can see it on my web page.
 
hello brother paulfjujo,

thank for enlighthing me, now i understand why my sensor can read up to 2 metres.

I have implemented ur coding in my project but i dun seem to get the result i wanted, now it jus can detect up to 80cm,
i have tested it same way i test previously in open space wit a box as an obstacle.

here i atached my result displayed in hyperterminal
<a title="Capture.PNG" href="http://obrazki.elektroda.pl/9948109700_1419570811.png"><img src="http://obrazki.elektroda.pl/9948109700_1419570811_thumb.jpg" alt="Capture.PNG" /></a>
 

hello,


link to *.png is corrupted !..
add some intermediate printout of raw values to help for debugging
Sorry; if forgetted to tell you ,multiply counts by 0.2 to get result in µS !


Code:
unsigned long Over;
Unsigned int  time;
float Distance;


while(1)
{

sendPulse();
T1CONbits.TMR1ON = 0;  
PIR1bits.TMR1IF=0;
TMR1H= 0;
TMR1L= 0;
Over=0;
time=0;

while(Echo == 0);
T1CONbits.TMR1ON = 1;
while(Echo == 1)
{
 if (PIR1bits.TMR1IF==1)
  {
     Over=Over+65536;
     PIR1bits.TMR1IF=0;
  }
}
T1CONbits.TMR1ON = 0;  
time= TMR1H;
time= time<<8;
time= time|TMR1L;
printf("\r\nTime = %d",time);
printf("\r\nOver = %ul",Over); 
Distance=(float) time + (float) Over;
printf("\r\nTravel duration %4.1f µS",Distance); 
Distance=Distance*0.2/5.88;    // counts * 0,2µS / 5.88 => result in mm
printf("\r\nDistance %4.1f mm",Distance);
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top