Challenge

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

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

Code :

#include <avr/io.h>
#define F_CPU 16000000L
#include <util/delay.h>
#include "avr/interrupt.h"
#define lcdport PORTB
#define rs_high PORTD|=(1<<0);
#define rs_low PORTD &=~(1<<0);
#define en_high PORTD |=(1<<1);
#define en_low PORTD &=~(1<<1);
unsigned int tov=0, count, distance;
void lcd_command(unsigned char cmd);
void lcd_data(unsigned char data);
void lcd_init();
void cursorpos(char x, char y);
void lcd_out(char x, char y, char *str);
void lcd_print(char row, char coloumn, unsigned int value, int digits);


int main()
{
	DDRB=0xff;   // PORTB as output
	DDRD|=(0<<6); //PORTD pin 6 as input from echo pin of sensor
	DDRC |=(1<<0);  // PORTC. 0 as output
	sei();          // Enable global interrupt
	lcd_init();
	TCCR1A=0x00;     //Normal mode
	TIMSK|=(1<<2);
	
	while(1)
	{
		PORTC |=(1<<0);  //Trigger Sensor
		_delay_us(10);
		PORTC&=~(1<<0);
		
		TCCR1B=0x41;   // ICF Enable
		TCNT1=0;
		TIFR=(1<<ICF1);
		TIFR=(1<<TOV1);
		tov=0;
		while((TIFR&(1<<ICF1))==0);  // check time overflow condition
		TCNT1=0;
		TCCR1B=0x01;
		TIFR=(1<<ICF1);
		TIFR=(1<<TOV1);
		tov=0;
		while((TIFR&(1<<ICF1))==0);
		count = ICR1+ (65535* tov);  // calculate the time for time taken
		distance=(unsigned int)count /932.87;  //sensor data sheet formula
		lcd_print(1,1,distance,4);
		lcd_out(1,6,"cm");
	}
	
}
ISR(TIMER1_OVF_vect)
{
	tov++;
}

void lcd_cmd(unsigned char cmd)
{
	lcdport=cmd; //initializing for command register
	rs_low;
	en_low;
	_delay_ms(1);
	en_high;
}

void lcd_data(unsigned char data)
{
	
	lcdport=data; // initializing data register
	rs_high;
	en_low;
	_delay_ms(1);
	en_high;
	
}
void lcd_init()
{
	lcd_cmd(0x38);_delay_ms(5);
	lcd_cmd(0x02);_delay_ms(5);
	lcd_cmd(0x01);_delay_ms(5);
	lcd_cmd(0x0c);_delay_ms(5);
	lcd_cmd(0x06);_delay_ms(5);
	lcd_cmd(0x80);_delay_ms(5); 
	lcd_cmd(0xc0);_delay_ms(5);
	
}
void cursorpos(char x, char y)
{
	if((x<1||x>2)||(y<1||y>16)) // LCD is 16x2
	{
		x=1;
		y=1;
	}
	if(x==1)
	lcd_cmd(0x7f+y); //specifying points to avoid descrepencies 
	else
	lcd_cmd(0xbf+y);
}
void lcd_out(char x, char y, char *str)
{
	cursorpos(x,y);
	while(*str)
	{
		
		lcd_data(*str);
		str++;
	}
}
void lcd_print(char row, char coloumn, unsigned int value, int digits)
{
	unsigned int temp, unit, tens, hundred,thousand, million;
	unsigned char flag=0;
	if(row==0||coloumn==0)
	{
		
		cursorpos(1,1);
	}
	else
	{
       cursorpos(row,coloumn);
	}
	if(digits==5||flag==1)
	{
		million=value/10000+48; //1st digit on display
		lcd_data(million);
		flag=1;
	}
	if(digits==4||flag==1)
	{
		temp=value/1000; //2nd digit on display
		thousand=temp%10 +48;
		lcd_data(thousand);
		flag=1;
	}
	if(digits==3||flag==1)
	{
		temp=value/100; //3rd digit on display
		hundred=temp%10 +48;
		lcd_data(hundred);
		flag=1;
	}
	if(digits==2||flag==1)
	{
		temp=value/10; //4th digit on display 
		tens= temp%10 +48;
		lcd_data(tens);
		flag=1;
	}
	if(digits==1||flag==1)
	{
		unit=value%10 +48; //5th digit on display
		lcd_data(unit);
	}
	if(digits>5)
	{
		lcd_data('e'); // show error [e] for more than 5 digits
	}
	
}

Outcome :

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.