Ultrasonic Sensor HC-SR04

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

Using atmega 8 

code is written to glow led at a distance of 3cm or 5 cm but it is doing nothing.

components used are :- atmega8,HC-SR04

 

/*

 * Ultrasonic.c

 *

 * Created: 9/26/2017 5:23:14 PM

 * Author : PC

 */ 

#define F_CPU 1000000UL

#include <avr/io.h>

#include <avr/interrupt.h>

#include <util/delay.h>

 

 int i=0;

 int pulse=0;

 int count;

int main(void)

{   

DDRC= 0xff;

DDRD |= 0x01;         //PIN0 for trig

DDRD &= ~0x04;        //PIN2 for Echo(For INT0)

PORTD &= ~0x04;

GICR |= 1<<INT0;

MCUCR |= 1<<ISC00;

sei();

    while (1) 

    {

PORTD |= 0x01;

_delay_us(15);

PORTD &=~0x01;

if(count == 174)

{

PORTC = 0x01;

}

if(count == 290)

{

PORTC = 0x02;

}

    }

}

ISR(INT0_vect)

if(i==0)

{

TCCR0=0x01;

TCNT0 = 0;

i=1;

}

if(i==1)

{

pulse  = TCNT0;

count = pulse/58;

TCCR0 = 0x00;

TCNT0 = 0;

i=0;

}

}

 

This topic has a solution.

vemohit

Last Edited: Thu. Sep 28, 2017 - 06:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you looked at the 1000 other questions and answers about this same/similar topic on this website> I suspect not!

 

Ross McKenzie ValuSoft Melbourne Australia

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

 

How to properly post source code: http://www.avrfreaks.net/comment...

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

vemohit wrote:
if(count == 174) { PORTC = 0x01; } if(count == 290) { PORTC = 0x02; }

 

What are the odd's that the range will be exactly that value?  and for how long?

Why not use a range of values to test and activate your LED's.  i.e. say 170 to 180, or 280 to 300?  or perhaps using an even wider range to start then narrow it down once it works.

 

 

Also, you need to look up "volatile" and how it is used with variables shared between main() and ISR()!

 

Jim

 

Last Edited: Wed. Sep 27, 2017 - 01:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suspect that this bit of code doesn't do what you think it does...

 

ISR(INT0_vect)
{ 
    if(i==0)
    {
        TCCR0=0x01;
        TCNT0 = 0;
        i=1;
    }
    if(i==1)
    {
        pulse  = TCNT0;
        count = pulse/58;
        TCCR0 = 0x00;
        TCNT0 = 0;
        i=0;
    }
}

 

HINT: trace the execution paying particular attention to the value of i.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

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

vemohit wrote:
 * Author : PC

So have you asked "PC" why his/her code doesn't work?

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

Brian Fairchild wrote:
HINT: trace the execution paying particular attention to the value of i.

 

Well spotted. Why are some people so allergic to switch/case/break? It makes stuff like this so much easier to avoid.

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

valusoft wrote:
Have you looked at the 1000 other questions and answers about this same/similar topic on this website

Including the very recent http://www.avrfreaks.net/forum/h...

 

Indeed:

...which includes a Tutorials forum article.

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

tried it earlier but it didn't workout!!!!

vemohit

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

I think u r suggesting to make 'i' volatile?

vemohit

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

Can anyone suggest a solution? I am new to this forum it will be really helpful.

 

 

vemohit

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

Can anyone suggest a solution? 

See #2 & #8

 

I am new to this forum it will be really helpful.

Do you have any experience with microcontrollers? electronics? programming in general? how to use an internet search engine?

 

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

vemohit wrote:

I think u r suggesting to make 'i' volatile?

 

Not i, but count yes.

volatile int count;

 

Also replace

if(i==1)

in your ISR with

else // if(1==1)

 

Jim

 

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

Nope not working!!!

vemohit

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

Show current code so we can check you have applied the advice given so far. The 'i' thing is vital.

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

Show us your new code, please use <> code buttons this time.

 

 

 

 

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

ki0bk wrote:
 please use <> code buttons 

See #3

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

Another major flaw of your code: you are constantly starting new measurements. I personally have no idea how the module reacts when you give a new start pulse while an ongoing measurement.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define F_CPU 1000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int i=0;
int pulse=0;
volatile int count;
int main(void)
{   
	
	DDRC= 0xff;
	DDRD |= 0x01;         //PIN0 for trig
	DDRD &= ~0x04;        //PIN2 for Echo(For INT0)
	PORTD &= ~0x04;
	GICR |= 1<<INT0;					
	MCUCR |= 1<<ISC00;
	sei();
	
    while (1) 
    {
		PORTD |= 0x01;
		_delay_us(15);
		PORTD &=~0x01;		
		if((count>170)&&(count<180))
		{
			PORTC = 0x01;
			
		}
		if(count == 290)
		{
			PORTC = 0x02;
		}
	
    }
	
}
ISR(INT0_vect)
{ 
	if(i==0)
	{
		TCCR0=0x01;
		TCNT0 = 0;
		i=1;
	}
	else
	{
		pulse  = TCNT0;
		count = pulse/58;
		TCCR0 = 0x00;
		TCNT0 = 0;
		i=0;
	}
	
}

 

vemohit

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

No commentary?

 

You have been told that exact matching isn't going to work well, if at all.

 

TCNT0 is an 8-bit register.  It can only have values 0-255.  Divided by 58, your "count" will only have possible values of 0 1 2 3 4.

 

 

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

Actually I'm testing the Ultrasonic.

so previously i have also tried with

if (count>0)

{ PORTC = 0x01;

}

So the thing is 'count' is not getting any value.

vemohit

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

vemohit wrote:
Actually I'm testing the Ultrasonic.
And one result of this testing seems to be: the ultrasonic module doesn't like to get a new start pulse before finishing the measurement.

Stefan Ernst

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

Surely in the many previous discussions I pointed out above, there are working approaches.

 

The most recent link I gave uses pin-change interrupt, and looks at the state of the pin.  If you are going to use external interrupt, don't you have to wait for rising edge first, start your timer, and then change to look at falling edge?  I see no management of edge selection in the code you posted.  I guess the selection of "any change" might work, >>if<< you never get out of syncronization

 

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

Thank you all for your help!!!

Changing the timer from TIMER0 to TIMER1 works.

Thanks @theusch.

vemohit