title: Interfacing ultrasonic range sensor (HCSR-04) with avr ATmega-32?
body: <p>I am using an ultrasonic range/sonar sensor (HCSR-04) with an AVR ATmega32A. I have written the code understanding the concept of how the sensor works but although the program compiles correctly the device doesn't give any result.</p>
  
  <p>The LCD is showing "out of range" which means PINC1 isn't going high. Is my method for checking if a bit is high or low correct? Should I use the <code>bit_is_set(PORTC,PINC1)</code> function? I am using the default frequency 1 MHz. PINC0 is connected to trig and PINC1 is the echo input. Here is the code: </p>
  
  <pre><code>#include&lt;avr/io.h&gt;
  #include&lt;util/delay.h&gt;
  #include"lcd.h"
  
  int main(void){
      ini_lcd();
      TCCR1B|=1&lt;&lt;CS10;
      DDRC|=1&lt;&lt;PINC0;
      int a=0,b=0;
      while(1){
          TCNT1=0;
          PORTC|=1&lt;&lt;PINC0;
          while(TCNT1&lt;100);
          PORTC&amp;=~(1&lt;&lt;PINC0);
  
          TCNT1=0;
          while(!(PORTC&amp;(1&lt;&lt;PINC1))) &amp;&amp; TCNT1&lt;30000) ;  // checking if echo has become high  and not exceeding the time for max range i.e 5 m
          ///send_string("out");
          b=TCNT1;
          if(b&lt;30000){
              TCNT1=0;
              while(PORTC&amp;(1&lt;&lt;PINC1));    // waiting until the echo become low again
              a=TCNT1;
              go_to_pos(1,5);
              send_int(a/58);
          } else {
              go_to_pos(1,5);
              send_string("out of range");
          }
          _delay_ms(20);
      }
  }
  </code></pre>