ULTRASONIC SENSOR HY-SRF05

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
I am using ATmega16 and i want to blink a led when an obstacle is 10cm or less close to my ultrasonic sensor.I have the following code but the led blinks only when the obstacle has a specific distance regardless of the value that i use in the if statement.
CODE:

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 10000000L
#include <util/delay.h>
#include <stdint.h>

static volatile int pulse = 0;

int main(void)
{
	DDRB=0x01;
	PORTB=0x00;

	DDRD = 0xFB;
	_delay_ms(50);

	GICR|=(1<<INT0);  // External Interrupt Request & set (one) and the I-bit in the Status Register

	MCUCR|=(1<<ISC00)|(1<<ISC01); //define whether the External Interrupt is activated on rising
	//and/or falling edge of the INT0 pin or level sensed

	TCCR1A = 0;

	sei();

	while(1)
	{
		PORTD|=(1<<PIND0);
		_delay_us(15);
		PORTD &=~(1<<PIND0);
		_delay_us(150);

		if((pulse/58)<= 10){
			PORTB |= (1<<PINB0);
			pulse=0;
		}
		else{
			PORTB &=~ (1<<PINB0);
			pulse=0;
		}

	}
}
ISR(INT0_vect)
{
	TCCR1B|=(1<<CS10);
	while(PIND&0x04);
	TCCR1B=0; //No clock source
	pulse=TCNT1;
	TCNT1=0;

}

 

This topic has a solution.
Last Edited: Tue. Oct 24, 2017 - 10:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

konis wrote:
only when the obstacle has a specific distance

And what distance would that be?

 

Is your AVR really running at 10MHz?

 

Which LED is "blinking"?  The way I read the code, PB0 changes state depending on "pulse".

 

Don't you need to change from rising edge to falling edge?

Image result for hcsr05 timing diagram

 

There must be many HCSR05 discussions on this forum, including some quite recently.

 

 

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use an 10MHz crystall oscillator on stk500 and i have set the jumpers to use oscillator's frequency.Pulse is the value of the counter witch increases as long as the echo is high. PB0 is the output to the led  and i compare pulse's duration(echo) to find out the distance.

The distance led blins is about 25cm regardless of the value i use in if state.

Last Edited: Tue. Oct 24, 2017 - 02:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

konis wrote:
Pulse is the value of the counter witch increases as long as the echo is high. 

Does it?

 

How have you checked?

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

konis wrote:

static volatile int pulse = 0; 

Shouldn't that be unsigned ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When i debug the programm with the simulator and i check the registers it increases correctly but when i run the programm on board i have the problem i mentioned before. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

konis wrote:
The distance led blins is about 25cm regardless of the value i use in if state.

See the timing diagram.  You have configured your external interrupt for "rising edge", right?  You may be well timing to the start of the return pulse.

 

You are dividing by 58 to get distance in centimeters.  But that assumes that your timing is in microseconds, and your timer units are 100ns.

 

As asked before, which is the "distance LED"?

 

 

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The distance LED is PB0.Do you have any formula from which i can calculate the denominator i have to use? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

.Do you have any formula from which i can calculate the denominator i have to use? 

 

Distance = Speed x Time

 

The Speed is the speed of the ultrasound pulse, look it up on Google.

It varies a bit with pressure, temperature, humidity, etc., but you can ignore all of those factors given the resolution and hardware you are using.

 

Time is the units of time you are counting.

If you know the micro's clock frequency, and how your program is counting time "tics", then this is known.

 

You must, of course, make sure all of your units are the same.

 

JC 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@konis said:

Pulse is the value of the counter witch increases as long as the echo is high.

Really? I am not aware of the MEGA timers having a gated counter mode.  And even if it has one, you are setting the TC to Normal mode.

 

Instead of using an external interrupt, why not use the TC's capture input?  This is precisely the sort of thing it is for.  And, as mentioned above, you need to toggle the edge sense from rising to falling after capturing the rising edge.  Something like this:

 

  1. Output trigger pulse
  2. Set TC to capture rising edge.
  3. When capture interrupt fires, store the rising edge timestamp and switch edge sense to falling edge.
  4. When capture interrupt fires again, store the falling edge timestamp.
  5. The difference between the timestamps is the width of the echo pulse.

 

 

 

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you all! Finally , the problem was that that "pulse" had to be unsigned as awneil said and should have changed the denominator (in the if statement) in order to take the accurate distance.