ultrasonic hc sr04

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

ultrasonic hc sr04

Postby captenusop » Thu Dec 22, 2011 3:15 pm

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
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: ultrasonic hc sr04

Postby robosang » Thu Dec 22, 2011 10:58 pm

what is that ultrasonic? The output is? PWM? Analog? Serial? You need to explain a bit, better link the datasheet here.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: ultrasonic hc sr04

Postby captenusop » Fri Dec 23, 2011 9:30 am

captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: ultrasonic hc sr04

Postby captenusop » Fri Dec 23, 2011 9:31 am

it's has 2 output that i don't realy understand,, echo and trig
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: ultrasonic hc sr04

Postby ober » Sat Dec 24, 2011 1:05 pm

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
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: ultrasonic hc sr04

Postby captenusop » Thu Jan 05, 2012 8:57 am

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...
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: ultrasonic hc sr04

Postby ABSF » Thu Jan 05, 2012 12:43 pm

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
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 captenusop » Thu Jan 05, 2012 2:50 pm

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
captenusop
Novice
 
Posts: 20
Joined: Fri Jul 16, 2010 11:24 am
Location: perlis

Re: ultrasonic hc sr04

Postby robosang » Thu Jan 05, 2012 8:50 pm

What is not working? How do you know it is not working? Did you measure the pin from Echo as suggested in datasheet?
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: ultrasonic hc sr04

Postby ABSF » Fri Jan 06, 2012 6:55 am

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
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

Next

Return to Sensor

Who is online

Users browsing this forum: No registered users and 12 guests

cron