Page 1 of 3

ultrasonic hc sr04

PostPosted: Thu Dec 22, 2011 3:15 pm
by captenusop
hy,, i doing my project with hc sr04 ultrasonic sensors..
i check singnal with oscilloscope there nothing..
how do i read the singnal with pic16f877a,,
tq

Re: ultrasonic hc sr04

PostPosted: Thu Dec 22, 2011 10:58 pm
by robosang
what is that ultrasonic? The output is? PWM? Analog? Serial? You need to explain a bit, better link the datasheet here.

Re: ultrasonic hc sr04

PostPosted: Fri Dec 23, 2011 9:30 am
by captenusop

Re: ultrasonic hc sr04

PostPosted: Fri Dec 23, 2011 9:31 am
by captenusop
it's has 2 output that i don't realy understand,, echo and trig

Re: ultrasonic hc sr04

PostPosted: Sat Dec 24, 2011 1:05 pm
by ober
It seem that this ultrasonic sensor require you to send a pulses to start the actual ultrasonic wave out from the sensor and it will wait for echo back by obstacle.

When you want to measure distance, the pin of your micrcocontroller which is connected to Trigpin of ultrasonic sensor will need to give pulse (low to high and high to low) of 10uS width. This is label as initiate in the datasheet.
Wait for echo back signal from the ultrasonic sensor on the echo pin. So basically you need 1 pin connected to Trig of ultrasonic (this is output from your micrcocontroller) and another pin connected to echo pin of ultrasonic (this is input pin to your microcontroller. The distance from ultrasonic sensor to obstacle is being translated to the width (or the period of high level) at echo pin. Use width in Us /58 if you program you will get the distance in cm.

You can try checking out the sample code for Arduino from here

Re: ultrasonic hc sr04

PostPosted: Thu Jan 05, 2012 8:57 am
by captenusop
while(1){ //start endless loop

pulse();
h=RB0 ;
printf(" ULTRASONIC %d" ,h);
__delay_sec(1);
lcd_clear();
h=0;
}

h is my variable,, i try to assign echo to variable h ,,
but it is not working


//************************************
i have made a pulse with 10u sec
void pulse ( void )
{ RB1=1;
__delay_us(10);
RB1=0;
__delay_us(10);
}

help me ,hhehheeh, tq...

Re: ultrasonic hc sr04

PostPosted: Thu Jan 05, 2012 12:43 pm
by ABSF
captenusop WROTE://************************************
i have made a pulse with 10u sec
void pulse ( void )
{ RB1=1;
__delay_us(10);
RB1=0;
__delay_us(10);
}


Your pulse is wrong... Ober said the pulse should be L_H_L, but your pulse is H_L only. Didn't you read the program refered by Ober?

Allen

Re: ultrasonic hc sr04

PostPosted: Thu Jan 05, 2012 2:50 pm
by captenusop
so i have modified to

void pulse ( void )// L-H-L
{ RB1=0;
__delay_us(10);
RB1=1;
__delay_us(10);
RB1=0;
__delay_us(10);
}

but stil not working

Re: ultrasonic hc sr04

PostPosted: Thu Jan 05, 2012 8:50 pm
by robosang
What is not working? How do you know it is not working? Did you measure the pin from Echo as suggested in datasheet?

Re: ultrasonic hc sr04

PostPosted: Fri Jan 06, 2012 6:55 am
by ABSF
captenusop WROTE:so i have modified to

void pulse ( void )// L-H-L
{ RB1=0;
__delay_us(10);
RB1=1;
__delay_us(10);
RB1=0;
__delay_us(10);
}

but stil not working


Your pulse is still wrong :? Take a look at the arduino code
CODE: SELECT_ALL_CODE
pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);


The Trigger pin is brought LOW for 2uS then HIGH for 10uS and then LOW again without any delay. The reason is very clear. After the pulse is sent, you should immediately starts monitoring the echo pin and starts the timer0/1 and then wait for the echo pin to go from H_L to stop the timer0/1. The (stop - start)uS reading of timer0/1 is critical for the calculation of the distance. Use timer0 (8 bit)if the distance you are interested is near and timer1 (16 bit) if object is far.

Not sure if HiTechC has the function PulseIn() like arduino. That would make things so much simpler. Or else, You would need to write a subroutine just to emulate pulsein(). Did you also set the TRISB for your RB0 and RB1? :roll: Your "pulse" pin should be output and the "echo" pin should be input. Now I know why people like to use Arduino so much. They really make designing with arduino much less headache and the codes are so neat.

Take a look at the pulsein() function description here

Arduino - PulseIn
http://arduino.cc/en/Reference/pulseIn

Allen