Challenge

Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)

Aim:

To implement code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04).

Objective:

  • Measuring the distance of an object using Ultrasonic Distance Sensor (HC-SR04)
  • Usage of Input Capture Mode

Highlights:

  • Measuring the distance travelled by the wave to the object and gets bounced back from the object to sensor.
  • Measuring Pulse width using Input Capture Mode

Deliverables:

  • The distance travelled by a wave to the object for which we want to measure the distance using Ultrasonic Distance Sensor (HC-SR04).
  • Measuring Pulse width using Input Capture Mode of Timer 1

Code:

#include 
#define F_CPU 16000000ul
#include 
#include 
#include 
#include 


#define lcd_data_dir DDRD         
#define lcd_command_dir DDRC      
#define lcd_data_port PORTD       
#define lcd_command_port PORTC      
#define EN PC2                      
#define RW PC3                       
#define RS PC4                      

void lcd_command(char);                     
void lcd_char(char);                        
void lcd_init(void);                         
void lcd_string(char*);                   
void lcd_string_xy (char,char,char*);          

void lcd_command (char cmd)                    
{
	lcd_data_port = cmd;                          
	lcd_command_port&=~((1<<rs)|(1<<rw)); lcd_command_port|="(1<<EN);" _delay_ms(1);="" lcd_command_port&="~(1<<EN);" _delay_ms(2);="" }="" void="" lcd_char(char="" char_data)="" {="" lcd_data_port="char_data;" lcd_init(void)="" lcd_command_dir|="(1<<RS)|(1<<RW)|(1<<EN);" lcd_data_dir="0xff;" _delay_ms(20);="" lcd_command(0x38);="" lcd_command(0x0c);="" lcd_command(0x06);="" lcd_command(0x01);="" lcd_command(0x80);="" lcd_string(char="" *str)="" int="" i;="" for(i="0;str[i]!=0;i++)" lcd_char="" (str[i]);="" lcd_string_xy(char="" row,char="" pos,char="" if="" (row="=" 1)="" lcd_command((pos="" &="" 0x0f)|0x80);="" else="" 2)="" 0x0f)|(0xc0));="" lcd_string(str);="" #define="" trigger_pin="" pa0="" timeroverflow="0;" isr(timer1_ovf_vect)="" timeroverflow++;="" main="" (void)="" char="" string[10];="" long="" count;="" double="" distance;="" ddra="0x01;" portb="0xff;" lcd_init();="" lcd_string_xy(1,0,"ultrasonic");="" sei();="" timsk="(1<<" toie1);="" tccr1a="0;" while(1)="" porta="" |="(1<<" trigger_pin);="" _delay_ms(10);="" trigger_pin));="" tcnt1="0;" tccr1b="0x41;" tifr="1<<ICF1;" while="" ((tifr="" (1<<icf1))="=" 0);="" (1<<="" icf1))="=" count="ICR1" +(65535="" *="" timeroverflow);="" distance="(double)count/466.47;" dtostrf(distance,2,2,string);="" strcat(string,"cm");="" lcd_string_xy(2,0,"dist=");
		lcd_string_xy(2,7,string);                     
		_delay_ms(200);
	}
}

Program:

Circuit:

  • The program is loaded in the Atmega 32 chip.
  • LCD pins are connected to the Atmega32 as shown above.
  • Osillocope and ultrasonic sensors are connected to view the output.

Output:

  • 60.38 cm is the distance of an object obtained.

Result:

Thus the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04) is done.

</rs)|(1<<rw));>

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.