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

--------------------------------------------------------------------------------------------------------------------------------------------

#include <avr/io.h> // AVR main header file
#define F_CPU 16000000l
#include <util/delay.h> // this is a delay header file
#include <avr/interrupt.h> // interrupt header file
#include <string.h> //string header file
#include <stdlib.h>

#define lcd_data_dir DDRD //using DDRD data port direction
#define lcd_command_dir DDRC //this is command port direction
#define lcd_data_port PORTD
#define lcd_command_port PORTC
#define EN PC2 //lcd enable pin
#define RW PC3 //lcd read and write pin
#define RS PC4
#define trigger_pin PA0 //trigger pin port A0

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

void lcd_command (char cmd)
{
lcd_data_port = cmd; //command to lcd
lcd_command_port&=~((1<<RS)|(1<<RW)); //make RS low and RW low
lcd_command_port|= (1<<EN); // EN pin is high
_delay_ms(1);
lcd_command_port&=~(1<<EN); // in this part EN pin low
_delay_ms(2);

}

void lcd_char(char char_data)
{
lcd_data_port= char_data;
lcd_command_port&=~(1<<RW); //RW pin is to low
lcd_command_port|=((1<<EN)|(1<<RS)); // making the EN pin and RS pin high
_delay_ms(1);
lcd_command_port&=~(1<<EN); //EN pin is low
_delay_ms(1);
}

void lcd_init(void)
{
lcd_command_dir|= (1<<RS)|(1<<RW)|(1<<EN); //making the RS RW EN pins are high
lcd_data_dir = 0xff;

_delay_ms(20);
lcd_command(0x38);//16x2 lcd 8 bit
lcd_command(0x0C);//display on
lcd_command(0x06);//auto incrment cursor
lcd_command(0x01);//clar lcd command
lcd_command(0x80);//8 is the line and postion
}

void lcd_string(char *str){ //string function

int i;
for(i=0;str[i]!=0;i++)
{
lcd_char (str[i]); // calling the lcd data write
}

}
void lcd_string_xy(char row,char pos,char *str)
{
if (row == 1)
lcd_command((pos & 0x0f)|0x80);
else if (row == 2)
lcd_command((pos & 0x0f)|(0xC0));
lcd_string(str); //call lcd string function
}

int TimerOverFlow = 0;
ISR(TIMER1_OVF_vect)
{
TimerOverFlow++; // making the overflow count
}

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

DDRA = 0x01; // pin A0 is output
PORTB = 0xff; //portb all pin are high

lcd_init();
lcd_string_xy(1,0,"obj in ultrasonic"); // this is a position of string

sei(); //enable global interrupt
TIMSK = (1<< TOIE1); //TOIE1 setting high
TCCR1A = 0; //bit is 0 in normal position

while(1)
{
PORTA |= (1<< trigger_pin); //A0 is high
_delay_ms(10);
PORTA &=(~(1<< trigger_pin)); //A0 is low

TCNT1 = 0;
TCCR1B = 0x41;
TIFR = 1<<ICF1; //clear ICP flag
TIFR = 1<<TOV1; //timer overflow flag


while ((TIFR & (1<<ICF1)) == 0);
TCNT1 = 0;
TCCR1B = 0x01;
TIFR = 1<<ICF1; //ICP flag
TIFR = 1<<TOV1; //overflow flag
TimerOverFlow = 0;

while ((TIFR & (1<< ICF1))== 0);
count = ICR1 +(65535 * TimerOverFlow);
distance = (double)count/466.47;

dtostrf(distance,2,2,string);
strcat(string,"cm"); //units of distance
lcd_string_xy(2,0,"dist: "); // dist string
lcd_string_xy(2,7,string);
_delay_ms(200);
}
}

---------------------------------------------------------------------------------------------------------------------------------------------

simu :-

OUTPUT :-

--------------------------------------------------------------------------------------------------------------------------------------------

code :-

***************************************************************************************************

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.