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<
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.