Challenge

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

Overview of the Project:

In this project, the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04)

Goals:

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

Key Highlights of the Project:

  • 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 of this project:

  • 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

Expected Outcomes:

The students will get the Knowledge on Ultrasonic sensor (HC-SR04) to measure the distance travelled by wave to the object, the hardware connection of Atmega328p and HC-SR04 and how to use input capture mode of Timer1

Program:

#include <avr/io.h> //preprocessor
#define F_CPU 16000000ul //defining cpu clock frequency
#include <util/delay.h> //preprocessor
#include <avr/interrupt.h> //preprocessor
#include <string.h>//preprocessor
#include <stdlib.h>//preprocessor

#define lcd_data_dir DDRD //define lcd data port direction
#define lcd_command_dir DDRC //define lcd command port direction
#define lcd_data_port PORTD //define lcd data port
#define lcd_command_port PORTC //define lcd data port
#define EN PC2 //define enable
#define RW PC3 //define read/write
#define RS PC4 //define register select

void lcd_command(char); //lcd command write function
void lcd_char(char); //lcd data write function
void lcd_init(void); //lcd initialize function
void lcd_string(char*); //send dtring to lcd
void lcd_string_xy (char,char,char*); //send row postion

void lcd_command (char cmd) //lcd command write function
{
	lcd_data_port = cmd; //write command to lcd data port
	lcd_command_port&=~((1<<RS)|(1<<RW)); //make RS low and RW low
	lcd_command_port|= (1<<EN); //high to low transition on EN
	_delay_ms(1); //delay
	lcd_command_port&=~(1<<EN); //clear bit
	_delay_ms(2); //delay provided
}

void lcd_char(char char_data) //lcd data write function
{
	lcd_data_port= char_data; //write data to lcd data port
	lcd_command_port&=~(1<<RW); //make RW low(write)
	lcd_command_port|=((1<<EN)|(1<<RS)); //make RS high and high to low tnasition
	_delay_ms(1); //delay
	lcd_command_port&=~(1<<EN); //clear bit
	_delay_ms(1); //delay
}

void lcd_init(void) //lcd initialize function
{
	lcd_command_dir|= (1<<RS)|(1<<RW)|(1<<EN); //lcd command port direction as output
	lcd_data_dir = 0xff; //make lcd data port direction as output

	_delay_ms(20); //wait
	lcd_command(0x38); //16x2 lcd 8 bit
	lcd_command(0x0C); //display On
	lcd_command(0x06); //auto increment cursor
	lcd_command(0x01); //clear lcd command
	lcd_command(0x80); //8 is the line and 0 is oth position
}

void lcd_string(char *str) //send string to lcd function
{
	int i; //variable declaration
	for(i=0;str[i]!=0;i++) //sending each char string till tje null
	{
		lcd_char (str[i]); //call lcd data write
	}
}

void lcd_string_xy(char row,char pos,char *str) //send string to lcd function
{
	if (row == 1)
	lcd_command((pos & 0x0f)|0x80); //command of first row and required position <16
	else if (row == 2)
	lcd_command((pos & 0x0f)|(0xC0)); //command of second row and required position<16
	lcd_string(str); //call lcd string function
}

#define trigger_pin PA0 //trigger pin
int TimerOverFlow = 0; //variable declaration

ISR(TIMER1_OVF_vect)
{
	TimerOverFlow++; //increment timer overflow count
}

int main (void) //main function
{
	char string[10]; //variable declaration
	long count; //variable declaration
	double distance; //variable declaration

	DDRA = 0x01; //make trigger pin as output
	PORTB = 0xff; //pull-up resister turn on

	lcd_init(); //function called for lcd initialization
	lcd_string_xy(1,0,"ultrasonic"); //function called

	sei(); //enable global interrupt
	TIMSK = (1<< TOIE1); //enable timer 1 overflow interrupt
	TCCR1A = 0; //set all bit to zero normal position

	while(1)
	{
		PORTA |= (1<< trigger_pin); //set bit
		_delay_ms(10); //delay
		PORTA &=(~(1<< trigger_pin)); //clear bit

		TCNT1 = 0; //clear timer counter
		TCCR1B = 0x41; //capture on rising edge,no pre scaler
		TIFR = 1<<ICF1; //clear ICP flag
		TIFR = 1<<TOV1; //clear timer overflow flag

		//calculate width of echo by input capture(icp)/

		while ((TIFR & (1<<ICF1)) == 0); //wait for rising edge
		TCNT1 = 0; //clear timer counter
		TCCR1B = 0x01; //capture on falling edge , no prescaler
		TIFR = 1<<ICF1; //clear ICP flag
		TIFR = 1<<TOV1; //clear timer overflow flag
		TimerOverFlow = 0; //clear timer overflow count

		while ((TIFR & (1<< ICF1))== 0); //wait for falling edge
		count = ICR1 +(65535 * TimerOverFlow); //take count
		distance = (double)count/466.47; //calculate distance

		dtostrf(distance,2,2,string); //distance to string
		strcat(string,"cm"); //count unit
		lcd_string_xy(2,0,"dist= "); //function called
		lcd_string_xy(2,7,string); //print distance
		_delay_ms(200); //delay
	}
}

Output:

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.

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.