Interfacing Ultrasonic Sensor with ATMEGA 8

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

I am using a 2 PIN Ultrasonic sensor (One PIN as output and other as input). Something like below:


#define Trigger PINB.7 //o/p Trigger PIN of the Sensor
#define Echo    PINB.6 //i/p Echo PIN of the Sensor


    //Timer Initialization
    TCCR0 = 0x05;
    TCNT0 = 0x00;
    TIMSK = 0x00;

void ultrasonic_function (void)
{
   
    Trigger = 1;            //Trigger Pulse
    delay_ms(100);
    Trigger = 0;
               
    TIMSK = 0x01;           //Timer on
   
    while (Echo == 0);      //waiting for Echo Signal
    u_value = TCNT0;        //25cm 20cm 15cm

    TIMSK = 0x00;

    LCD_cmd(0xc0);
    LCD_string("NU:");
    hextodec(u_value);

}

The issue is that based upon the values displayed in LCD I feel it is not correct. With or without obstacles I get various readings as 164,247,74,157,240,58 ... and so on ..

Please let me know if I have any issues with my code.

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

Problems are numerous!
PIN is used for input, PORT is used for output.
change PINB.7 to PORTB.7
You enable interrupts with TIMSK, you can only start the timer with TCCR0.
change TIMSK = 0x01 to TCCR0 = 0x01
and TIMSK = 0 to TCCR0 = 0

and see if that helps.
JC

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

I don't know C, so perhaps your code is fine in this regard:

Does {...} loop forever?
Obviously you need your program to loop.

How long is Delay ms(100)?
I would guess 100 milliseconds.

If this is an HC-SR04 module, then the trigger pulse is suppose to be 10 microseconds.

I think you are just displaying the timer's count, and not yet converting it to a distance.
That's fine, and a good way to start.

Do you Reset the timer/counter's value to 0 before each time through the loop?

(The other) JC

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

Thanks , please let me know how to calculate distance based upon timer value?

Yes, this function would be called inside a for () loop.

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

The Timer/Counter is used to count units of time.

First, you need to know what the clock frequency is that your uC is running at. This could be based upon an external crystal, or on the internal RC oscillator. You generally have a line at the start of your program that tells the compiler your uC's clock frequency, (1 MHz, 2 MHz, 8 MHz, 14.xxx MHz, etc)

Next, look at how you set up the Prescaler. The clock goes into the Prescaler where it is divided down to a lower frequency. The divisor could be 1, 2, 4...512, 1024, etc. It depends upon which uC you are using, look at the data sheet on the Prescaler section.

If, as an example, you had an 8 MHz clock, and the Prescaler was dividing by 8, then the output of the Prescaler would be a 1 MHz clock.

On each clock cycle the Timer/Counter counts up by 1.

So each count is equal to 1 microsecond.

The time per count depends upon YOUR clock frequency and YOUR Prescaler divisor values.

Next, Google Speed of Sound in Air.
It varies with temperature, humidity, etc., but pick a nice "standard" value.

Distance = Speed * Time

You do the various units conversions to display in whatever you want, eg. cm.

Once you have the basic system working, go back and AVERAGE 16 readings for each reading that you display. It will give you a more stable reading, with less bounce between readings.

JC

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

Thanks a lot DocJC for the lucid explanation and it really helps people like us who are beginning to learn coding in AVR